-->
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.  [ 6 posts ] 
Author Message
 Post subject: NEWBIE: problem saving with one-to-one
PostPosted: Mon May 08, 2006 1:58 am 
Newbie

Joined: Mon May 08, 2006 1:49 am
Posts: 3
Location: Fremont, California
Hello all

I have 2 tables in DB
desc Employee
EMP_ID NUMBER<AUTO>
EMP_NAME VARCHAR

desc Salary
EMP_ID NUMBER<AUTO>
EMP_SALARY NUMBER

Both of them get mapped 2 class named
company.Employee
company.Salary

Now in company.Employee I want to keep reference of its Salary object, so now Employee.hbm.xml looks like

<hibernate-mapping>
<class name="company.Employee" table="EMPLOYEE">
<id name="id" column="EMP_ID">
<generator class="native"/>
</id>
<property name="name" type="text" column="EMP_NAME"/>
<one-to-one name="salary" class="company.Salary" outer-join="auto" />
</class></hibernate-mapping>

This works perfectly if I read from database, but If I save in database, than it won't save anything in Salary table, but only employee details are saved.,
Can anyone PLEASE point me out how to do this???

Following is Salary.hbm.xml file
<hibernate-mapping>
<class name="company.Salary" table="SALARY">
<id name="id" column="EMP_ID">
<generator class="native"/>
</id>
<property name="salary" type="java.lang.Integer" column="EMP_SALARY"/></class>
</hibernate-mapping>


TIA


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 2:20 am 
Beginner
Beginner

Joined: Fri Apr 28, 2006 7:48 am
Posts: 20
Hi,
I had a similar problem.

You mapping file should have the following for an one-to one mapping
<class name="Salary" table="Salary">
<id name="id column="EMP_ID" type="java.lang.Long" unsaved-value="null">
<generator class="foreign">
<param name="property">salary</param>
</generator>
</id>
<one-to-one name="employee" class="Employee" constrained="true" cascade="all"/>
</class>


Your Employee.hbm.xml file should have the following
<one-to-one name="salary" cascade="all" class="Salary" />

I hoping that the emp_id column in the salary table is a foreign key.
Also, have the following attributes in your Employee and Salary class respectively and generate the getter/setters

Employee.java
Salary salary;

Salary.java
Employee employee


Also your insert code should have the following

Employee empl=new Employee();
Salary sal=new Salary()

tx=session.beginTransaction();
// your code

do not forget to add these to statements

empl.setSalary(sal);
sal.setEmployee(empl);

tx.commit();


Also do not forget to add the cascade attribute in both your mapping files as mentioned in the above mapping file.


Hope this helps!!!

Regards
falgunipm


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 12:34 pm 
Newbie

Joined: Mon May 08, 2006 1:49 am
Posts: 3
Location: Fremont, California
falgunipm,
Thanks alot, it helps.
But while saving I am getting this exception

Hibernate: insert into EMPLOYEE (EMP_NAME) values (?)
Hibernate: update Salary set EMP_SALARY=? where EMP_ID=?
09:28:56,343 ERROR AbstractBatcher:61 - Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row count from
update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)


I am doing exactly same as what you told and Seems like instead of doing insert, it is tryg to update in salary and it fails there...
Not really sure what is causing it, can you put any insight...?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 12:57 am 
Beginner
Beginner

Joined: Fri Apr 28, 2006 7:48 am
Posts: 20
Hi,
Can you please paste your mapping files and your insert code.
Hope I can help.


Regards,
falgunipm


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 9:42 am 
Newbie

Joined: Mon May 08, 2006 1:49 am
Posts: 3
Location: Fremont, California
Here it is ...

Employee.hbm.xml

<hibernate-mapping>
<class name="company.Employee" table="EMPLOYEE">
<id name="id" column="EMP_ID">
<generator class="native"
/>
</id>
<property name="name" type="text" column="EMP_NAME"/>

<one-to-one name="salary" cascade="all" class="company.Salary" />
<!-- <one-to-one name="salary" class="company.Salary" outer-join="auto" /> -->

</class>
</hibernate-mapping>



Salary.hbm.xml

<hibernate-mapping>
<class name="company.Salary" table="Salary">
<id name="id" column="EMP_ID" type="java.lang.Integer" unsaved-value="null">
<generator class="foreign">
<param name="property">salary</param>
</generator>
</id>
<one-to-one name="employee" class="company.Employee" constrained="true" cascade="all"/>
<property name="salary" type="java.lang.Integer" column="EMP_SALARY"/>
</class>
</hibernate-mapping>


Insert code
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
System.out.println("####T-Class: " + session.beginTransaction().getClass().getName());
Employee theEvent = new Employee();
theEvent.setId(id);
theEvent.setName(name);
Salary sal = new Salary();
sal.setId(id);
sal.setSalary(salary);
theEvent.setSalary(sal);
sal.setEmployee(theEvent);
session.beginTransaction();
session.save(theEvent);
session.getTransaction().commit();


Please lemme know if you need any more info
Thanks alot....


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 1:47 am 
Beginner
Beginner

Joined: Fri Apr 28, 2006 7:48 am
Posts: 20
I think there was a small typo from myside.


Salary.hbm.xml

<hibernate-mapping>
<class name="company.Salary" table="Salary">
<id name="id" column="EMP_ID" type="java.lang.Integer" unsaved-value="null">
<generator class="foreign">
<param name="property">salary</param>
</generator>
</id>
<one-to-one name="employee" class="company.Employee" constrained="true" cascade="all"/>
<property name="salary" type="java.lang.Integer" column="EMP_SALARY"/>
</class>
</hibernate-mapping>

change the highlighted (the one in bold) line to

<param name="property">employee</param>

As the value of the property has to be the name of the relation.



Sorry for that.

Hope this help!!!


Regards,
falgunipm


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