Note that there are some explanatory texts on larger screens.

plurals
  1. POCaused by: com.sybase.jdbc2.jdbc.SybSQLException: The column detail_id in table exam does not allow null values
    primarykey
    data
    text
    <p>I have two tables between which I am trying to establish a 1-to-1 mapping using Hibernate.</p> <p><strong>ExamDetail.java</strong></p> <pre><code>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; } } </code></pre> <p><strong>Exam.java</strong></p> <pre><code>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; } } </code></pre> <p><strong>TestMapping.java</strong></p> <pre><code>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(); } } </code></pre> <p>When I ran this code, I encounter following exception :-</p> <blockquote> <p><strong>Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [com.hibernate.mapping.Exam]</strong> 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: <strong>com.sybase.jdbc2.jdbc.SybSQLException: The column detail_id in table exam does not allow null values.</strong></p> <p>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</p> </blockquote> <p>What am I doing wrong ? </p> <p>Any missing statement ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload