-->
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.  [ 4 posts ] 
Author Message
 Post subject: Save object that contains a list of objects to be also saved
PostPosted: Wed May 21, 2008 11:51 pm 
Newbie

Joined: Tue Apr 22, 2008 12:48 am
Posts: 9
Hi all,
I am not sure whether this issue is normal or not.

The thing is have an object, let's say: Company that has a list of objects, let's say a list of Employee. Each Employee object has an attribute that is a reference to the Company. It would be something like:

Company
{
Set<Employee> employees;
int code =-1;
}

where the code for the Company is like:
<id
name="code"
column="cod_cia"
type="int"
unsaved-value="null"
>
<generator class="sequence"/>
</id>

Employee
{
Company company;
}

public testMethod
{
Company cia;
Employee emp1;
emp1.company = cia;

Set empList;
employees.add(emp1);

cia.employees = empList;

Transaction tx = HibernateUtil.getCurrentSession().beginTransaction();
HibernateUtil.getCurrentSession().save(cia);
tx.commit();

}


My idea was that when i 'save' cia object, the hibernate engine would also store in the database the object employee in its table... but unlucky i am getting this exception:
Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1


If i try to save just a 'plain' Company, without any Employee in the list, then it is fine and the Company is saved.

I feel (is just a feeling) that the exception is thrown because when hibernate tries to save the object Employee, the reference it has to the Company still has not the code, that is primary key and then it fails...

Any ideas?
Dani.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 22, 2008 2:05 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
Hi,

What u are asking is exactly doable.. but need to put proper config or annotations.

Look at Bid and Item example in JDBC Persistance with Hibernate or
https://glassfish.dev.java.net/javaee5/ ... ample.html.

Regards,
Raja Nagendra Kumar,
C.T.O
www.tejasoft.com
Competant and Reliable Java, J2EE, J2ME, Code Audit and Refactoring Offshore Development Partners.

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 22, 2008 5:53 am 
Newbie

Joined: Tue Apr 22, 2008 12:48 am
Posts: 9
Actually i found where's the error...

both Company and Employee has an attribute 'code'.
both codes in the hbm files are mapped in their files as:

<id
name="code"
column="cod_xxxxx"
type="int"
>
<generator class="sequence" />
</id>

where 'xxxxx' is company or employee accordingly.

The thing is, if i comment the line '<generator class="sequence" /> ' from employee and i give a code from the code like:

Company cia = new Company();
Employee emp = new Employee();
emp.setCode(69);
cia.addEmp(emp);

everything works fine, but when i try to use also the <generator class="sequence" /> to give a code to the employee when i get that exception...

Is like if i try to fill the code of the company and the employee with the sequence, then it fails...

Any ideas?
Dani.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 22, 2008 6:14 am 
Newbie

Joined: Tue Apr 22, 2008 12:48 am
Posts: 9
I fixed it...

Don't know why, but if you use:

<generator class="hilo" />

instead of:

<generator class="sequence" />

it all works well...

Thanks,
Dani.


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