2012-07-12 56 views
2

模型類:爲什麼Hibernate包含帶有插入的標識字段?

@Entity 
@Table(name = "CSU_AUDIT") 
@NamedQuery(name = "findAllEventsForObjectId", query = "from AuditEvent e where e.objectId = :objectId order by e.auditTimestamp desc") 
public class AuditEvent implements Serializable { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 577051869176721340L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int auditId; 

    @Column(name = "INSERT_TS") 
    private Date auditTimestamp; 

    @Column(name = "OBJECT_ID") 
    private String objectId; 

    @Column(name = "ACTION_CODE") 
    private String actionCode; 

    @Column(name = "USER_ID") 
    private String userId; 

    @Column(name = "FIELD_NAME") 
    private String fieldName; 

    @Column(name = "OLD_VALUE") 
    private String oldValue; 

    @Column(name = "NEW_VALUE") 
    private String newValue; 

簡單的DAO方法:

public void insertEvent(AuditEvent event) { 
     final Session session = sessionFactory.getCurrentSession(); 
     session.beginTransaction(); 
     log.info("Saving audit event..."); 
     log.debug(event); 
     session.save(event); 
     session.getTransaction().commit(); 
    } 

會話工廠是由Spring的AnnotationSessionFactoryBean管理。

相關的異常信息(更多可用的,如果需要的話):

2012-07-12 09:01:41,049 DEBUG org.hibernate.event.def.AbstractSaveEventListener - executing identity-insert immediately 
2012-07-12 09:01:41,053 DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 
2012-07-12 09:01:41,066 DEBUG org.hibernate.SQL - insert into CSU_AUDIT (auditId, ACTION_CODE, INSERT_TS, FIELD_NAME, NEW_VALUE, OBJECT_ID, OLD_VALUE, USER_ID) values (default, ?, ?, ?, ?, ?, ?, ?) 
2012-07-12 09:01:41,100 DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 
2012-07-12 09:01:41,117 DEBUG org.hibernate.util.JDBCExceptionReporter - could not insert: [com.cinfin.ecm.csudocmgr.audit.AuditEvent] [insert into CSU_AUDIT (auditId, ACTION_CODE, INSERT_TS, FIELD_NAME, NEW_VALUE, OBJECT_ID, OLD_VALUE, USER_ID) values (default, ?, ?, ?, ?, ?, ?, ?)] 
com.ibm.db2.jcc.c.SqlException: DB2 SQL error: SQLCODE: -206, SQLSTATE: 42703, SQLERRMC: AUDITID 

我不明白爲什麼被包含在插入註釋爲@ID@GeneratedValue(strategy = GenerationType.IDENTITY)領域。我讀過的一切都表明,hibernate應該完全忽略這個字段。在數據庫方面,這個字段是作爲身份生成的,儘管我沒有代碼來確定它是如何聲明的。

回答

2

正如您在日誌中看到的那樣,它會傳遞default作爲生成列的值,這意味着:使用此列的默認值。 DB2's documentation說:

DEFAULT

指定的默認值將被使用。指定DEFAULT的結果取決於列是如何定義的,如下所示:

[...]

  • 如果IDENTITY子句​​用於,由數據庫管理器生成的值。
+0

是的,我認爲是。雖然我從來沒有使用過DB2,但無法破譯這個神祕的錯誤信息。 – 2012-07-12 14:25:12

相關問題