-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: OneToMany relationship not persisting properly
PostPosted: Sun Jan 03, 2010 9:44 am 
Newbie

Joined: Sun Jan 03, 2010 9:34 am
Posts: 2
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


Top
 Profile  
 
 Post subject: Re: OneToMany relationship not persisting properly
PostPosted: Wed Jan 20, 2010 10:17 pm 
Newbie

Joined: Sun Jan 03, 2010 9:34 am
Posts: 2
bump. Anyone have any thoughts? There have been about 5 posts so far on similar topics and no one has answered. Thanks!


Top
 Profile  
 
 Post subject: Re: OneToMany relationship not persisting properly
PostPosted: Thu Jan 21, 2010 11:55 am 
Newbie

Joined: Fri Jul 11, 2008 10:45 am
Posts: 5
--


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.