2016-05-31 25 views
0

我嘗試了一個示例程序,其中我使用save()方法對其標識符具有序列生成值的實體對象。但我沒有找到save()生成插入查詢。插入查詢是隻產生在事務提交()不知道在哪裏的問題is.Below是代碼的細節:在hibernate中保存()真的發出一個插入語句而不是persist()?

實體類:

@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配置文件:

<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> 

的方法從我調用保存:

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(); 
    } 

Integer empTest =(Integer)session.save(emp);執行,下面是所生成的日誌:(無插入查詢)

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 [[email protected][sql=[call next value for hibernate_sequence]]] 
14:16:11,153 TRACE JdbcCoordinatorImpl:437 - Registering result set [[email protected]] 
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 [[email protected]] 
14:16:11,169 TRACE JdbcCoordinatorImpl:573 - Closing result set [[email protected]] 
14:16:11,169 TRACE JdbcCoordinatorImpl:412 - Releasing statement [[email protected][sql=[call next value for hibernate_sequence]]] 
14:16:11,169 TRACE JdbcCoordinatorImpl:525 - Closing prepared statement [[email protected][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. 

我不明確設置的ID列,但使用該序列,以生成的ID。

回答

1

這是一個事務的正常用法,您可以在其中排列所有語句。

insert語句只在提交時發出,如果發生了什麼(通常返回HibernateException),則可以使用rollback()撤消所有操作。

+1

即使在插入後也可以執行'rollback()'。爲了激發插入語句,可以使用'flush()'方法。 –

相關問題