• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ~
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic