• 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

one-to-one mapping issue.

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm not able to save data using one-to-one mapping and not able to make out where am I going wrong.

This is my schema.
Emp
-----------------
EMPNO number <PK>
ENAME varchar(30)
------------------

Salary
------------------
EMPID <PK><FK>
BASIC NUMBER
HRA NUMBER
TAX NUMBER
-------------------


And my class and mapping files are as follows:

Salary.hbm.xml
-----------------------------------------------------------
<hibernate-mapping package="Formula">
<class name="Salary" table="EMP_SALARIES" >
<id name="id" column="EMPID">
<generator class="foreign">
<param name="property">emp</param>
</generator>
</id>
<property name="salary" column="BASIC"/>
<property name="hra" column="HRA"/>
<property name="tax" column="TAX"/>
<property name="income" formula="BASIC+HRA-TAX" />

<one-to-one name="emp" class="OneToManyRelationship.Emp" constrained="true"/>
</class>
</hibernate-mapping>

Emp.hbm.xml
-----------------------------------------------------------
<class name="Emp" table="EMP">
<id name="empno" column="EMPNO" unsaved-value="-1">
<generator class="increment"/>
</id>
<property name="ename" column="ENAME"/>
<one-to-one name="salaryDetails" class="Formula.Salary"/>
</class>
</hibernate-mapping>


and my java file is

Emp emp=new Emp();
emp.setEname("Salaried Employee");
emp.setComm(new Integer(1));
emp.setDeptno(new Integer(10));
Salary sal=new Salary();
sal.setId(emp.getEmpno());
sal.setSalary(new Float("2322.34"));
sal.setHra(new Float(343.45));
sal.setTax(new Float(398.332));

emp.setSalaryDetails(sal);
session.save(emp);

When I try to run it, it insert record in Emp table only. On adding cascade="all" in one-to-one mapping of emp.hbm.xml file, an exception is thrown 'attempted to assign id from null one-to-one property: emp'.

Can you pls help me to resolve it ASAP.

Thanks,
Ruchika
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the mapping for Emp says : you are making use of increment generator for assigning ids.

But in your java code, you are assigning empNo in the Salary.id. With such type of mapping you are only suppose to do mapping. Hibernate will take care of populating the value in the Emp_Salary.empno column from the primary key of Emp table.

So comment the line of code where you are assigning the empNo in the Salary object. It should work.

Naresh Waswani
+91-9986461501
 
Ruchika Kapoor
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naresh,

I commented that line, but still its not working. Only inserting record in Emp table.

Any other idea ?

Thanks,
Ruchika
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per mapping xml file the salary class requires to have a property called emp which i suppose is not given in the code. So the child object (salary) is not getting persisted.
 
Ruchika Kapoor
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reason of that issue:

If we create the child object and set the parent object in it and save the child object using session. It works perfectly fine.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the issue has to do with both sides having the <one-to-one> tags. Only one side really can have this.

Here's the documentation example
http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html#assoc-bidirectional-121

See how one side is mapped as <many-to-one>

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic