我有以下兩個表Java的休眠 - 複合主鍵問題
CREATE TABLE event_type_master( Event_Type_Code VARCHAR(128)NOT NULL, PRIMARY KEY(Event_Type_Code) )
CREATE TABLE event_master( EVENT_CODE PREVARY KEY(Event_Code,Event_Type_Code), CONSTRAINT FK1 FOREIGN KEY(Event_Type_Code)REFERENCES event_type_master(Event_Type_Code)varchar(128)NOT NULL, Event_Type_Code varchar(128)NOT NULL, 0)
現在我對上述關係如下 EventMaster類創建模型類: -
@Entity
@Table(name="event_master")
public class EventMaster implements java.io.Serializable {
private EventMasterId id;
private EventTypeMaster eventTypeMaster;
private String eventName;
public EventMaster() {
}
public EventMaster(EventMasterId id, EventTypeMaster eventTypeMaster) {
this.id = id;
this.eventTypeMaster = eventTypeMaster;
}
public EventMaster(EventMasterId id, EventTypeMaster eventTypeMaster) {
this.id = id;
this.eventTypeMaster = eventTypeMaster;
this.eventName = eventName;
}
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name="eventCode", [email protected](name="Event_Code", nullable=false, length=128)),
@AttributeOverride(name="eventTypeCode", [email protected](name="Event_Type_Code", nullable=false, length=128)) })
public EventMasterId getId() {
return this.id;
}
public void setId(EventMasterId id) {
this.id = id;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="Event_Type_Code",referencedColumnName = "Event_Type_Code", nullable=false, insertable=false, updatable=false)
public EventTypeMaster getEventTypeMaster()
{
return this.eventTypeMaster;
}
public void setEventTypeMaster(EventTypeMaster eventTypeMaster) {
this.eventTypeMaster = eventTypeMaster;
}
}
EventMasterId類的複合主鍵設置: -
@Embeddable
public class EventMasterId implements java.io.Serializable {
private String eventCode;
private String eventTypeCode;
public EventMasterId() {
}
public EventMasterId(String eventCode, String eventTypeCode)
{
this.eventCode = eventCode;
this.eventTypeCode = eventTypeCode;
}
@Column(name="Event_Code", nullable=false, length=128)
public String getEventCode() {
return this.eventCode;
}
public void setEventCode(String eventCode) {
this.eventCode = eventCode;
}
@Column(name="Event_Type_Code", nullable=false, length=128)
public String getEventTypeCode() {
return this.eventTypeCode;
}
public void setEventTypeCode(String eventTypeCode) {
this.eventTypeCode = eventTypeCode;
}
public boolean equals(Obje克拉其他){ ........ }
公衆詮釋的hashCode(){ .......... }
}
EventTypeMaster類
@Entity
@Table(name="event_type_master")
public class EventTypeMaster implements java.io.Serializable {
private String eventTypeCode;
private String eventTypeName;
private Set<EventMaster> eventMasters = new HashSet<EventMaster>(0);
public EventTypeMaster() {
}
public EventTypeMaster(String eventTypeCode) {
this.eventTypeCode = eventTypeCode;
}
public EventTypeMaster(String eventTypeCode, String eventTypeName, Set eventMasters) {
this.eventTypeCode = eventTypeCode;
this.eventTypeName = eventTypeName;
this.eventMasters = eventMasters;
}
@Id
@Column(name="Event_Type_Code", unique=true, nullable=false, length=128)
public String getEventTypeCode() {
return this.eventTypeCode;
}
public void setEventTypeCode(String eventTypeCode) {
this.eventTypeCode = eventTypeCode;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="eventTypeMaster")
@JoinColumn(name="Event_Type_Code", referencedColumnName = "Event_Type_Code")
public Set<EventMaster> getEventMasters() {
return this.eventMasters;
}
public void setEventMasters(Set<EventMaster> eventMasters) {
this.eventMasters = eventMasters;
}
}
所有設置後,我創建了一個HebernateUtil柯樂s,使用NetBeans中連接到HibernateSession廠,並試圖測試記錄添加到event_master表如下
Session session = null;
session = NewHibernateUtil.getSessionFactory().getCurrentSession();
try {
org.hibernate.Transaction tx = session.beginTransaction();
EventMasterId key1=new EventMasterId();
EventTypeMaster eTypeMaster1=new EventTypeMaster();
eTypeMaster1=(EventTypeMaster)session.load(EventTypeMaster.class, "e1");
key1.setEventCode(eTypeMaster1.getEventTypeCode());
key1.setEventCode("Test_Event_Code");
EventMaster em=new EventMaster();
em.setId(key1);
em.setEventTypeMaster(eTypeMaster1);
em.setEventDesc("Event Description");
session.save(em);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
但我得到以下錯誤
休眠:插入event_master(Create_DTTM,CREATED_BY, Event_Desc,Event_Name,Event_Short_Name,Last_Mod_By,Last_Mod_DTTM,Event_Code,Event_Type_Code)值(?,?,?,?,?,?,?,?,?) 1473 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error :1048,SQLState:23000 1473 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Column' Event_Type_Code」不能爲空 1474 [主]錯誤org.hibernate.event.def.AbstractFlushingEventListener - 無法同步與會話 org.hibernate.exception.ConstraintViolationException數據庫狀態:無法執行JDBC批處理更新 org.hibernate.exception。 ConstraintViolationException:無法在org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94) 處執行JDBC批處理更新 at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate。 jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue。的java:167) 在org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) 在org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50) 在org.hibernate作爲。 impl.SessionImpl.flush(SessionImpl.java:1028) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137) at test.NewMain.main(NewMain.java:46) 引起:java.sql.BatchUpdateException:列'Event_Type_Code'不能爲空 at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1666) at com .mysql.jdbc.PreparedStatement.executeBatch(PreparedSt atement.java:1082) 在org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) 在org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
請幫我解決此問題。
謝謝。我找不到這個愚蠢的錯誤。 – user3678674 2011-04-08 05:01:51
如果有效,請標記答案。 – 2011-04-08 05:13:58