Example of @Generated in Hibernate
May 28, 2013
Hibernate provides the facility to update the entity for those database values which are generated at insert or row update time. @Generated belongs to org.hibernate.annotations. @Generated is used at property level. There are different GenerationTime.
GenerationTime.INSERT
GenerationTime.INSERT updates the entity at insert time. For GenerationTime.INSERT, the property should not be insetable.@Generated(GenerationTime.INSERT) @Column(name="create_date", insertable=false)
GenerationTime.ALWAYS
GenerationTime.ALWAYS updates the entity at both time insert and update. In the case of GenerationTime.ALWAYS, the property should not be insertable and updatable.@Generated(GenerationTime.ALWAYS) @Column(name="create_date", insertable=false,updatable=false)
State.java
package com.concretepage.persistence; import java.io.Serializable; import java.util.Calendar; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import org.hibernate.annotations.Generated; import org.hibernate.annotations.GenerationTime; import org.hibernate.annotations.Index; @Entity @Table(name = "state") public class State implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue @Column(name = "id") private int id; @Temporal(TemporalType.TIMESTAMP) @Generated(GenerationTime.ALWAYS) @Column(name="create_date", insertable=false,updatable=false) private Calendar createDate; @Column(name="name") @Index(name="nameIndex") private String name; public Calendar getCreateDate() { return createDate; } public void setCreateDate(Calendar createDate) { this.createDate = createDate; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
HibernateUtil.java
package com.concretepage.util; import java.util.Properties; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import com.concretepage.persistence.State; public class HibernateUtil { private static final SessionFactory concreteSessionFactory; static { try { Properties prop= new Properties(); prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate"); prop.setProperty("hibernate.connection.username", "root"); prop.setProperty("hibernate.connection.password", ""); prop.setProperty("hibernate.hbm2ddl.auto", "update"); prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect"); concreteSessionFactory = new AnnotationConfiguration() .addPackage("com.concretepage.persistence") .addProperties(prop) .addAnnotatedClass(State.class) .buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static Session getSession() throws HibernateException { return concreteSessionFactory.openSession(); } public static void main(String... args){ Session session=getSession(); session.beginTransaction(); State s= new State(); s.setName("Delhi"); session.save(s); session.getTransaction().commit(); System.out.println("Create Date:"+s.getCreateDate().getTime()); session.close(); } }
CREATE TABLE `state` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `create_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `name` VARCHAR(255) NOT NULL, PRIMARY KEY (`id`), INDEX `nameIndex` (`name`) ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB AUTO_INCREMENT=2;