Java Hibernate - 复合主键问题

时间:2011-04-08 04:31:50

标签: java hibernate jpa

我有以下两个表

CREATE TABLE event_type_master(   Event_Type_Code varchar(128)NOT NULL,   PRIMARY KEY(Event_Type_Code) )

CREATE TABLE event_master(   Event_Code varchar(128)NOT NULL,   Event_Type_Code varchar(128)NOT NULL,   PRIMARY KEY(Event_Code,Event_Type_Code),   CONSTRAINT FK1 FOREIGN KEY(Event_Type_Code)REFERENCES event_type_master(Event_Type_Code) )

现在我已经为上述关系创建了模型类,如下所示 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", column=@Column(name="Event_Code", nullable=false, length=128) ), 
@AttributeOverride(name="eventTypeCode", column=@Column(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(Object other){        ........    }

public int 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;
}

}

设置All之后我使用Netbeans创建了一个HebernateUtil类连接到HibernateSession Factory并尝试测试将记录添加到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();
    }

但我收到了错误

Hibernate:插入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错误:1048,SQLState:23000 1473 [main] ERROR org.hibernate.util.JDBCExceptionReporter - 列'Event_Type_Code'不能为null 1474 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - 无法将数据库状态与会话同步 org.hibernate.exception.ConstraintViolationException:无法执行JDBC批量更新 org.hibernate.exception.ConstraintViolationException:无法执行JDBC批量更新         在org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)         在org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)         在org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)         在org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)         在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)         在org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)         在org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)         在test.NewMain.main(NewMain.java:46) 引起:java.sql.BatchUpdateException:列'Event_Type_Code'不能为null         在com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1666)         在com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1082)         在org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)         在org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

我认为你有一个错字。

key1.setEventCode(eTypeMaster1.getEventTypeCode());
key1.setEventCode("Test_Event_Code");

第一行应该是key1.setEventTypeCode。