-->
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.  [ 3 posts ] 
Author Message
 Post subject: Need help on one-to-one - child data not getting saved
PostPosted: Wed Oct 25, 2006 6:00 pm 
Newbie

Joined: Thu Aug 03, 2006 11:18 pm
Posts: 5
I am trying to run one-to-one Person-address program. The Person data gets saved but Address data is not getting saved. Below is the program that I am running.

I am not sure what I am missing, any pointer/help will be highly appreacited

Code:

Person.hbm.xml
---------------------------

<class name="Person">
<id name="id" column="personId">
<generator class=“identity"/>
</id>

<one-to-one name="address"/>

<property name="name" type="string">
<column name="Name"/>
</property>

</class>

address.hbm.xml
------------------------------------
<class name="Address">

<id name="id" column="personId">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>

<one-to-one name="person" constrained="true"/>

<property name="state" type="string">
<column name="STATE"/>
</property>

</class>



Person.java
-----------------

class Person
{
int id;
String name;
Address address;

setter/getter for id, name and address;

}



Address.java
-----------------
class Address
{

int id;
Person person;
String state;

setter/getter for id, state and person;

}

Client.java
--------------------

class Client
{

SessionFactory sessionFactory = new Configuration().configure ().buildSessionFactory ();
Session session = sessionFactory.openSession ();
Transaction tx = session.beginTransaction();

tx.begin();

Person person = new Person();
person.setName("Test");

Address address = new Address();
address.setState(NY);

person.setAddress(address);

session.save(person); // ********** SHOULD SAVE ADDRESS ALSO

tx.commit();
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 9:46 pm 
Newbie

Joined: Thu Aug 03, 2006 11:18 pm
Posts: 5
**** I really need suggestion on this.

My DB Schema is -

Person table
---------------
id <PK>
name varchar

Address Table
-----------------
id <PK> <FK>
state varchar

I added cascade="all" in Person.hbm.xml <one-to-one name="address" cascade="all" />
Now, I am getting this error
org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property:

Then, I tried to set person on address
person.setAddress(address);
address.setPerson(person); // NEW CODE

After this changes, id null problem went away, and now I am getting this error -
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
ORA-02291: integrity constraint (SCOTT.SYS_C003231) violated - parent key not found


My client code is -
-------------------------

Person person = new Person();
person.setID(100); // i changed from "identity" to "assigned" attributs in mapping file
person.setName("Test");

Address address = new Address();
address.setState(NY);

address.setPerson(person);
person.setAddress(address);

session.save(person);

DB is trying to insert a new record for 100 in both table and there is FK constraint in Address and 100 "might not" be available in Person.

Please look into and share me where I am going wrong.

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 5:40 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
I have managed to get this to work - I seem to have done exactly as you did. Please make sure, before you test everything, that you re-create the db schema.

My Person mapping contains:
Code:
<one-to-one name="address" cascade="all"/>

My address mapping contains:
Code:
<id name="id" column="ADDRESS_ID">
   <generator class="foreign">
   <param name="property">person</param>
   </generator>
</id>
<one-to-one name="person" constrained="true"/>


My init code is:
Code:
person = new Person();
address ad = new Address();
ad.setPerson(person);
person.setAddress(ad);
session.save(person);


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