-->
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.  [ 2 posts ] 
Author Message
 Post subject: one to one mapping question
PostPosted: Tue May 17, 2011 12:30 pm 
Newbie

Joined: Mon Feb 18, 2008 10:44 am
Posts: 10
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


Top
 Profile  
 
 Post subject: Re: one to one mapping question
PostPosted: Wed May 18, 2011 8:47 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 5:28 am
Posts: 42
add class="student.Student" cascade="save-update" <one-to-one> tag

_________________
Warm Regards,
Tarun Walia


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.