I have make one to one mapping according primary key. I have two table,such as student and score
student table
Code:
create table STUDENT
(
ID INTEGER not null,
NAME VARCHAR2(10),
SNUMBER VARCHAR2(20),
CLASSID INTEGER
)
alter table STUDENT add constraint SID primary key (ID)
score table
Code:
create table SCORE
(
ID INTEGER not null,
SCORE INTEGER,
TYPE VARCHAR2(20)
)
alter table SCORE add constraint CID primary key (ID)
alter table SCORE add constraint CCID foreign key (ID) references STUDENT (ID);
There are two configure file,like follows:
Student.hbm.xml
Code:
<hibernate-mapping>
<class name="student.Student" table="student">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="assigned"/>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" not-null="true"/>
</property>
<property name="number" type="java.lang.String">
<column name="snumber" length="20" not-null="true"/>
</property>
<property name="classid" type="java.lang.Integer">
<column name="classid" not-null="true"/>
</property>
</class>
</hibernate-mapping>
Score.hbm.xml
Code:
<hibernate-mapping>
<class name="score.Score" table="score">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="foreign">
<param name="property">student</param>
</generator>
</id>
<one-to-one name="student" constrained="true"/>
<property name="score" type="java.lang.Integer">
<column name="score" not-null="true"/>
</property>
<property name="type" type="java.lang.String">
<column name="type" length="20" not-null="true"/>
</property>
</class>
</hibernate-mapping>
The I test one-to-one relation,like follows:
Code:
Student st=new Student();
st.setId(111);
st.setName("onetoone");
st.setNumber("518");
st.setClassid(300);
Score sc=new Score();
sc.setScore(199);
sc.setType("Q");
sc.setStudent(st);
save(sc);
When I run above code,it raise following error:
Code:
Warn: SQL Error: 2291, SQLState: 23000
2011-5-18 0:14:44 org.hibernate.util.JDBCExceptionReporter logExceptions
Fatal: ORA-02291: integrity constraint (SCOTT.CCID) violated - parent key not found
2011-5-18 0:14:44 org.hibernate.util.JDBCExceptionReporter logExceptions
Warn: SQL Error: 2291, SQLState: 23000
2011-5-18 0:14:44 org.hibernate.util.JDBCExceptionReporter logExceptions
Fatal: ORA-02291: integrity constraint (SCOTT.CCID) violated - parent key not found
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC bat
ch update
Where is wrong with my code? How to correct above code?
Thanks