I've got a problem with my mapping (I think) that is causing me no end of heartache. I have 2 classes, Section and Course that are mapped as so:
Course:
Code:
@Entity
public class Course implements Serializable, Comparable<Course>
{
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column
private Long id;
@Column(name = "COURSE_NAME")
private String name;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "COURSE_INSTRUCTOR",
joinColumns= {@JoinColumn(name = "course_id")},
inverseJoinColumns = {@JoinColumn(name = "instructor_id")}
)
@IndexColumn(name = "Course_Instructor_Num", base = 0)
private List<Instructor> instructors = new ArrayList<Instructor>();
@OneToMany(mappedBy="course", fetch = FetchType.EAGER)
@org.hibernate.annotations.Cascade(CascadeType.SAVE_UPDATE)
@IndexColumn(
name = "Course_Section_Num",
base = 0,
nullable=true
)
private List<Section> sections = new ArrayList<Section>();
...
Section:
Code:
@Entity
@Table(name="CLASS_SECTION")
public class Section implements Serializable, Comparable<Section>
{
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column
private Long id;
@ManyToOne(fetch = FetchType.EAGER,
cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(
name = "SECTION_COURSE",
joinColumns= {@JoinColumn(name = "section_id")},
inverseJoinColumns = {@JoinColumn(name = "course_id")}
)
private Course course;
@Column(name = "SECTION_NAME")
private String name;
My access code looks up the course and the instructor from the database and saves them to dbCourse and dbInstructor respectively. I then have in my code:
Code:
Section dbSection = new Section(dbCourse, section.getName(), dbInstructor);
tx.begin();
entityManager.persist(dbSection);
dbCourse = entityManager.merge(dbCourse);
dbInstructor = entityManager.merge(dbInstructor);
tx.commit();
which fails miserably with the following exception:
Code:
org.hibernate.exception.GenericJDBCException: could not insert: [com.darkhonor.rage.model.Section]
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not insert: [com.darkhonor.rage.model.Section]
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:637)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:226)
at com.darkhonor.rage.libs.dataimport.CourseDAO.persist(CourseDAO.java:226)
at com.darkhonor.rage.libs.dataimport.CourseDAOTest.testPersist(CourseDAOTest.java:243)
Caused by: org.hibernate.exception.GenericJDBCException: could not insert: [com.darkhonor.rage.model.Section]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2295)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2668)
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.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:49)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:154)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:110)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:645)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:619)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:623)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:220)
Caused by: java.sql.SQLException: Field 'Course_Section_Num' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2278)
However, the following code doesn't throw an exception, but also doesn't give values for Course_Section_Num:
Code:
Section dbSection = new Section();
dbSection.setName(section.getName());
tx.begin();
entityManager.persist(dbSection);
tx.commit();
dbInstructor.addSection(dbSection);
dbCourse.addSection(dbSection);
tx.begin();
dbCourse = entityManager.merge(dbCourse);
dbInstructor = entityManager.merge(dbInstructor);
tx.commit();
Any ideas? This is placing a lot of stress as this is a deliverable for work in the very near future. Thanks in advance.
Alex