请帮助我找到编码的问题。
主要类别:
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();
豆类
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>
异常
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)
答案 0 :(得分:1)
如果你可以发布它,看看模式会有所帮助。
我首先想到的可能是它与Student-&gt; Class级联设置有关。由于您明确地在学生面前保存了课程,因此请尝试将其删除,因为不需要。