This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help on one-to-one - child data not getting saved

 
Manju Singh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

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);

tx.commit();
}
 
Manju Singh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
**** 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.
 
tom collin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess your project must be run on Sql Server. you could try this:
----------------------------
<generator class="native"/>

and

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


then before the object of address setter for person , you must be persistent
the person object .


PS: very sorry for my English ~
 
Halcon Guatemala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all, I'm curious, is true the Tom comment:
I guess your project must be run on Sql Server.

And how could Tom know it through the error message?

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic