I have two tables between which I am trying to establish a 1-to-1 mapping using 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();
}
}
When I ran this code, I encounter following exception :-
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [com.hibernate.mapping.Exam] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2186) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2666) at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at 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(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342) at $Proxy14.save(Unknown Source) at com.hibernate.mapping.TestMapping.main(TestMapping.java:22) Caused by: com.sybase.jdbc2.jdbc.SybSQLException: The column detail_id in table exam does not allow null values.
at com.sybase.jdbc2.tds.Tds.processEed(Tds.java:2535) at com.sybase.jdbc2.tds.Tds.nextResult(Tds.java:1916) at com.sybase.jdbc2.jdbc.ResultGetter.nextResult(ResultGetter.java:69) at com.sybase.jdbc2.jdbc.SybStatement.nextResult(SybStatement.java:201) at com.sybase.jdbc2.jdbc.SybStatement.nextResult(SybStatement.java:182) at com.sybase.jdbc2.jdbc.SybStatement.executeLoop(SybStatement.java:1596) at com.sybase.jdbc2.jdbc.SybStatement.execute(SybStatement.java:1588) at com.sybase.jdbc2.jdbc.SybPreparedStatement.execute(SybPreparedStatement.java:580) at org.hibernate.id.IdentityGenerator$InsertSelectDelegate.executeAndExtract(IdentityGenerator.java:138) at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57) ... 22 more
What am I doing wrong ?
Any missing statement ?