sunleo
sunleo

Reputation: 10947

One to Many Mapping in Hibernate exception

Please help to find what is the problem with my coding.

Main Class :

SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();        

Classes clazz = new Classes();
clazz.setName("MCA");
session.save(clazz);    

Student s1 = new Student();
s1.setName("student1");
s1.setClazz(clazz);

Student s2 = new Student();
s2.setName("student2");
s2.setClazz(clazz);    

session.save(s1);
session.save(s2);

session.getTransaction().commit();
session.close();

Beans :

public class Student {
private int id;
private String name;
private Classes clazz;

 //getter and setters
}

public class Classes {
private int id;
private String name;
private Set<Student> students;

  //getter and setters
}

<class name="leo.beans.Classes" table="Clazzes">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="string">
            <column name="NAME" length="100" />
        </property>
        <set name="students" table="student" inverse="true" lazy="true"
            fetch="select">
            <key>
                <column name="clazz" not-null="true" />
            </key>
            <one-to-many class="leo.beans.Student" />
        </set>
    </class>
    <class name="leo.beans.Student" table="STUDENT">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="string">
            <column name="NAME" length="10" />
        </property>
        <many-to-one name="clazz" class="leo.beans.Classes"
            cascade="save-update">
            <column name="clazz" not-null="true" />
        </many-to-one>
    </class>

Exception

NFO: schema update complete
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Clazzes (NAME, ID) values (?, ?)
Hibernate: insert into STUDENT (NAME, clazz, ID) values (?, ?, ?)
Hibernate: insert into STUDENT (NAME, clazz, ID) values (?, ?, ?)
Dec 12, 2013 10:11:15 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 2291, SQLState: 23000
Dec 12, 2013 10:11:15 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ORA-02291: integrity constraint (SYSTEM.FKBACA0E1B26F65B1E) violated - parent key not found

Dec 12, 2013 10:11:15 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 2291, SQLState: 23000
Dec 12, 2013 10:11:15 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ORA-02291: integrity constraint (SYSTEM.FKBACA0E1B26F65B1E) violated - parent key not found

Dec 12, 2013 10:11:15 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)

Upvotes: 1

Views: 218

Answers (1)

cf6423
cf6423

Reputation: 31

Seeing the schema would be helpful if you could post it.

My first thought it maybe it has to do with the Student->Class cascade setting. Since you're explicitly saving the Class before the students, try taking that out since it isn't needed.

Upvotes: 1

Related Questions