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: Foreign Key relation problem
PostPosted: Thu Jun 28, 2007 11:06 am 
Newbie

Joined: Thu Jun 21, 2007 9:52 am
Posts: 5
I am facing problem while inserting data into Database using Foreign Key relation. Have a look in my mapping and please suggest where I doing wrong

TABLE PERSON

create TABLE PERSON(PERSON_ID number,FIRSTNAME varchar2(50),LASTNAME varchar2(50),AGE number);
alter TABLE PERSON ADD PRIMARY KEY(PERSON_ID);

TABLE ADDRESS

create TABLE ADDRESS(ADDRESS_ID number,PERSON_ID number,STREET varchar2(50), CITY varchar2(50), ZIP number);
alter TABLE ADDRESS ADD PRIMARY KEY(ADDRESS_ID);
alter table ADDRESS ADD FOREIGN KEY(PERSON_ID) REFERENCES PERSON(PERSON_ID);

Hibernate Mapping file

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.coxauto.HibernateMapping">
<class name="Person" table="PERSON">

<id name="personID" column="PERSON_ID">
<generator class="assigned"/>
</id>

<property name="firstname">
<column name="FIRSTNAME"/>
</property>

<property name="lastname">
<column name="LASTNAME"/>
</property>

<property name="age">
<column name="AGE"/>
</property>


</class>

<class name="Address" table="ADDRESS">
<id name="addressid" column="assigned">
<generator class="increment"/>
</id>

<property name="street">
<column name="STREET"/>
</property>

<property name="city">
<column name="CITY"/>
</property>

<property name="zip">
<column name="ZIP"/>
</property>

<many-to-one name="person" column="PERSON_ID" class="Person" cascade="all" not-null="true"/>

</class>

</hibernate-mapping>



Code

Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();

Person p = new Person();
p.setPersonID(10);
p.setAge(30);
p.setFirstname("Upendra Kumar");
p.setLastname("Guggilam");

Address add = new Address();
add.setPersonid(12);
add.setCity("Norfolk");
add.setStreet("1049West,49th Street");
add.setZip(23508);
add.setPersonid(10);
add.setPerson(p);

session.save(add);
tx.commit();
HibernateUtil.closeSession();

Error

Hibernate: insert into ADDRESS (STREET, CITY, ZIP, PERSON_ID, ADDRESS_ID) values (?, ?, ?, ?, ?)
Jun 28, 2007 11:01:36 AM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 2291, SQLState: 23000
Jun 28, 2007 11:01:36 AM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ORA-02291: integrity constraint (SYSTEM.SYS_C004054) violated - parent key not found

Jun 28, 2007 11:01:36 AM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 2291, SQLState: 23000
Jun 28, 2007 11:01:36 AM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ORA-02291: integrity constraint (SYSTEM.SYS_C004054) violated - parent key not found

Jun 28, 2007 11:01:36 AM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 2291, SQLState: 23000
Jun 28, 2007 11:01:36 AM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ORA-02291: integrity constraint (SYSTEM.SYS_C004054) violated - parent key not found


Top
 Profile  
 
 Post subject: Save Person first
PostPosted: Thu Jun 28, 2007 11:52 am 
Newbie

Joined: Thu Jun 28, 2007 11:48 am
Posts: 1
You have to save person first,then you can have person_id and that can be referenced in address.
Google "SQL Error: 2291, SQLState: 23000" for more details.
or you can do p.setPersonID(12); in this example


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.