我有两个表,我正在尝试使用Hibernate建立一对一映射。
ExamDetail.java
package com.hibernate.mapping;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="exam_detail")
public class ExamDetail {
private int id;
private String fullName;
private int numberOfQuestions;
private int passingPercentage;
@Id
@GeneratedValue
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public int getNumberOfQuestions() {
return numberOfQuestions;
}
public void setNumberOfQuestions(int numberOfQuestions) {
this.numberOfQuestions = numberOfQuestions;
}
public int getPassingPercentage() {
return passingPercentage;
}
public void setPassingPercentage(int passingPercentage) {
this.passingPercentage = passingPercentage;
}
}
Exam.java
package com.hibernate.mapping;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="exam")
public class Exam {
private int id;
private String shortName;
private ExamDetail detail;
@Id
@GeneratedValue
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="detail_id")
public ExamDetail getDetail() {
return detail;
}
public void setDetail(ExamDetail detail) {
this.detail = detail;
}
}
TestMapping.java
package com.hibernate.mapping;
import org.hibernate.Session;
import com.hibernate.util.HibernateUtil;
public class TestMapping {
public static void main(String[] args) {
Session session = HibernateUtil.beginTransaction();
Exam exam = new Exam();
exam.setShortName("SCJA");
ExamDetail detail = new ExamDetail();
detail.setFullName("Sun Certified Associate");
detail.setNumberOfQuestions(50);
detail.setPassingPercentage(60);
exam.setDetail(detail);
session.save(exam);
HibernateUtil.commitTransaction();
}
}
当我运行此代码时,遇到以下异常: -
主题“主要”中的异常 org.hibernate.exception.ConstraintViolationException:不能 插入:[com.hibernate.mapping.Exam] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94) 在 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 在 org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64) 在 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2186) 在 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2666) 在 org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71) 在org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)at at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321) 在 org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204) 在 org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130) 在 org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) 在 org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) 在 org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) 在 org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) 在 org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) 在org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at java.lang.reflect.Method.invoke(未知来源)at org.hibernate.context.ThreadLocalSessionContext $ TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342) 在$ Proxy14.save(未知来源)at com.hibernate.mapping.TestMapping.main(TestMapping.java:22)引起: com.sybase.jdbc2.jdbc.SybSQLException:表格中的detail_id列 考试不允许空值。
at com.sybase.jdbc2.tds.Tds.processEed(Tds.java:2535)at at com.sybase.jdbc2.tds.Tds.nextResult(Tds.java:1916)at at com.sybase.jdbc2.jdbc.ResultGetter.nextResult(ResultGetter.java:69) 在 com.sybase.jdbc2.jdbc.SybStatement.nextResult(SybStatement.java:201) 在 com.sybase.jdbc2.jdbc.SybStatement.nextResult(SybStatement.java:182) 在 com.sybase.jdbc2.jdbc.SybStatement.executeLoop(SybStatement.java:1596) 在com.sybase.jdbc2.jdbc.SybStatement.execute(SybStatement.java:1588) 在 com.sybase.jdbc2.jdbc.SybPreparedStatement.execute(SybPreparedStatement.java:580) 在 org.hibernate.id.IdentityGenerator $ InsertSelectDelegate.executeAndExtract(IdentityGenerator.java:138) 在 org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57) ......还有22个
我做错了什么?
任何遗漏的陈述?