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

one-to-one mapping issue.

 
Ruchika Kapoor
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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
 
Waswani Naresh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Saravanan Thoppae
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic