Reputation: 688
I tried out a sample program,in which I use the save() method on my entity object for which the identifier has a sequence generated value.But i don't find the save() generating an insert query.Insert query is generated only on transaction commit().Not sure where the issue is.Below are the code details:
Entity class:
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "Employee", uniqueConstraints = { @UniqueConstraint(columnNames = "ID"),
@UniqueConstraint(columnNames = "EMAIL") })
@SequenceGenerator(name="seq", initialValue=1, allocationSize=100)
public class EmployeeEntity implements Serializable {
@Id
@Column(name = "ID", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
private Integer employeeId;
@Column(name = "EMAIL", unique = true, nullable = false, length = 100)
private String email;
@Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100)
private String firstName;
@Column(name = "LAST_NAME", unique = false, nullable = false, length = 100)
private String lastName;
public Integer getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
hibernate configuration file:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.url">jdbc:hsqldb:mem:test</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="com.test.hibernate.entity.EmployeeEntity"></mapping>
</session-factory>
</hibernate-configuration>
The method from which i invoke save:
public static void checkSave_Persist(){
Session session = HibernateUtil.getSessionFactory().openSession();
System.out.println("Default flush mode:" + session.getFlushMode());
session.beginTransaction();
EmployeeEntity emp = new EmployeeEntity();
emp.setEmail("[email protected]");
emp.setFirstName("demo");
emp.setLastName("user");
Integer empTest =(Integer)session.save(emp);
session.getTransaction().commit(); //insert query is generated only at this point
session.close();
HibernateUtil.shutdown();
}
On execution of Integer empTest =(Integer)session.save(emp);
, below is the generated log:(no insert query)
14:16:11,113 TRACE DefaultSaveOrUpdateEventListener:180 - Saving transient instance
14:16:11,129 DEBUG SQL:109 - call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
14:16:11,149 TRACE JdbcCoordinatorImpl:371 - Registering statement [org.hsqldb.jdbc.jdbcPreparedStatement@75bf0a2[sql=[call next value for hibernate_sequence]]]
14:16:11,153 TRACE JdbcCoordinatorImpl:437 - Registering result set [org.hsqldb.jdbc.jdbcResultSet@2c231468]
14:16:11,153 DEBUG SequenceGenerator:127 - Sequence identifier generated: BasicHolder[java.lang.Integer[1]]
14:16:11,169 TRACE JdbcCoordinatorImpl:455 - Releasing result set [org.hsqldb.jdbc.jdbcResultSet@2c231468]
14:16:11,169 TRACE JdbcCoordinatorImpl:573 - Closing result set [org.hsqldb.jdbc.jdbcResultSet@2c231468]
14:16:11,169 TRACE JdbcCoordinatorImpl:412 - Releasing statement [org.hsqldb.jdbc.jdbcPreparedStatement@75bf0a2[sql=[call next value for hibernate_sequence]]]
14:16:11,169 TRACE JdbcCoordinatorImpl:525 - Closing prepared statement [org.hsqldb.jdbc.jdbcPreparedStatement@75bf0a2[sql=[call next value for hibernate_sequence]]]
14:16:11,185 TRACE JdbcCoordinatorImpl:278 - Starting after statement execution processing [ON_CLOSE]
14:16:11,185 DEBUG AbstractSaveEventListener:130 - Generated identifier: 100, using strategy: org.hibernate.id.SequenceHiLoGenerator
14:16:11,185 TRACE AbstractSaveEventListener:169 - Saving [com.test.hibernate.entity.EmployeeEntity#100]
14:16:11,216 TRACE ActionQueue:165 - Adding an EntityInsertAction for [com.test.hibernate.entity.EmployeeEntity] object
14:16:11,231 TRACE ActionQueue:178 - Adding insert with no non-nullable, transient entities: [EntityInsertAction[com.test.hibernate.entity.EmployeeEntity#100]]
14:16:11,231 TRACE ActionQueue:198 - Adding resolved non-early insert action.
14:16:11,231 TRACE UnresolvedEntityInsertActions:213 - No unresolved entity inserts that depended on [[com.test.hibernate.entity.EmployeeEntity#100]]
14:16:11,247 TRACE UnresolvedEntityInsertActions:122 - No entity insert actions have non-nullable, transient entity dependencies.
I am not explicitly setting the ID column but using the sequence to generate the ID.
Upvotes: 0
Views: 1103
Reputation: 31
That's the normal usage of a transaction, where you queue all your statements.
The insert statement is only issued at commit, and if something happened (usually returns an HibernateException), you can undo all the operations with a rollback().
Upvotes: 1