Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance
[Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554467] Mon, 23 August 2010 09:52 Go to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
Hi,

I am doing a raw copy from one resource to my Teneo Hibernate resource.
When saving the hibernate resource, I get the exception below.

What would be the best way to (deep) copy the content of one resource so
the Hibernate resource and it's references are correctly filled?

Now I use this:

Library dbLib = Util.getDBLibrary();
if (dbLib != null) {
dbLib.getCompanies().addAll(
EcoreUtil.copyAll(srcLib.getCompanies()));
....



Caused by: org.hibernate.TransientObjectException: object references an
unsaved transient instance - save the transient instance before
flushing: Company
at
org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)

Thanks Christophe
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554468 is a reply to message #554467] Mon, 23 August 2010 10:14 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Christophe,
You have to tell Hibernate to cascade the persist operation for the reference on which it fails. Have you set one of
these options?
teneo.mapping.cascade_policy_on_containment
teneo.mapping.cascade_policy_on_non_containment

if it is a non-containment then the default is that MERGE, PERSIST and REFRESH are set. For containment the default is ALL.

In general for many-to-one associations Hibernate will work best with minimal cascade settings. This works fine in
normal production situations (for example in case a sales order references a currency then one can assume that the
currency has already been created in the database). For these large copy action though, this approach does not work well
as you are not sure about the order in which data is committed to the database.

gr. Martin

On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
> Hi,
>
> I am doing a raw copy from one resource to my Teneo Hibernate resource.
> When saving the hibernate resource, I get the exception below.
>
> What would be the best way to (deep) copy the content of one resource so
> the Hibernate resource and it's references are correctly filled?
>
> Now I use this:
>
> Library dbLib = Util.getDBLibrary();
> if (dbLib != null) {
> dbLib.getCompanies().addAll( EcoreUtil.copyAll(srcLib.getCompanies()));
> ....
>
>
>
> Caused by: org.hibernate.TransientObjectException: object references an
> unsaved transient instance - save the transient instance before
> flushing: Company
> at
> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>
> at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>
> Thanks Christophe


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554469 is a reply to message #554467] Mon, 23 August 2010 10:14 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Christophe,
You have to tell Hibernate to cascade the persist operation for the reference on which it fails. Have you set one of
these options?
teneo.mapping.cascade_policy_on_containment
teneo.mapping.cascade_policy_on_non_containment

if it is a non-containment then the default is that MERGE, PERSIST and REFRESH are set. For containment the default is ALL.

In general for many-to-one associations Hibernate will work best with minimal cascade settings. This works fine in
normal production situations (for example in case a sales order references a currency then one can assume that the
currency has already been created in the database). For these large copy action though, this approach does not work well
as you are not sure about the order in which data is committed to the database.

gr. Martin

On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
> Hi,
>
> I am doing a raw copy from one resource to my Teneo Hibernate resource.
> When saving the hibernate resource, I get the exception below.
>
> What would be the best way to (deep) copy the content of one resource so
> the Hibernate resource and it's references are correctly filled?
>
> Now I use this:
>
> Library dbLib = Util.getDBLibrary();
> if (dbLib != null) {
> dbLib.getCompanies().addAll( EcoreUtil.copyAll(srcLib.getCompanies()));
> ....
>
>
>
> Caused by: org.hibernate.TransientObjectException: object references an
> unsaved transient instance - save the transient instance before
> flushing: Company
> at
> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>
> at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>
> Thanks Christophe


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554470 is a reply to message #554468] Mon, 23 August 2010 10:23 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
Thanks Martin,
see inline on some response.

Martin Taal wrote:
> Hi Christophe,
> You have to tell Hibernate to cascade the persist operation for the
> reference on which it fails. Have you set one of these options?
> teneo.mapping.cascade_policy_on_containment
> teneo.mapping.cascade_policy_on_non_containment
>
No, I haven't. I actually suspect my references are not copied, as I
perform a serie of CopyAll(...) actions on various collections of my
model. The references within these collections would not be copied..

I will try with these options.

rgds Christophe



> if it is a non-containment then the default is that MERGE, PERSIST and
> REFRESH are set. For containment the default is ALL.
>
> In general for many-to-one associations Hibernate will work best with
> minimal cascade settings. This works fine in normal production
> situations (for example in case a sales order references a currency then
> one can assume that the currency has already been created in the
> database). For these large copy action though, this approach does not
> work well as you are not sure about the order in which data is committed
> to the database.
>
> gr. Martin
>
> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>> Hi,
>>
>> I am doing a raw copy from one resource to my Teneo Hibernate resource.
>> When saving the hibernate resource, I get the exception below.
>>
>> What would be the best way to (deep) copy the content of one resource so
>> the Hibernate resource and it's references are correctly filled?
>>
>> Now I use this:
>>
>> Library dbLib = Util.getDBLibrary();
>> if (dbLib != null) {
>> dbLib.getCompanies().addAll( EcoreUtil.copyAll(srcLib.getCompanies()));
>> ....
>>
>>
>>
>> Caused by: org.hibernate.TransientObjectException: object references an
>> unsaved transient instance - save the transient instance before
>> flushing: Company
>> at
>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>
>>
>> at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>
>> Thanks Christophe
>
>
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554472 is a reply to message #554470] Mon, 23 August 2010 10:39 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
Christophe Bouhier wrote:

Martin, I have the same issue, even with these options.
So it's a copy issue.

I just don't know how to copy the children of an EObject which consists
of various EList to a peer EObject while maintaining all references....

i.e.


original: Lib
- Companies
- Equipments


DB: Lib (existing)
- Companies (Copy)
- Equipments (Copy


What I do now is


dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));

So references between equipments and companies get lost it seems.


rgds Christophe





> Thanks Martin,
> see inline on some response.
>
> Martin Taal wrote:
>> Hi Christophe,
>> You have to tell Hibernate to cascade the persist operation for the
>> reference on which it fails. Have you set one of these options?
>> teneo.mapping.cascade_policy_on_containment
>> teneo.mapping.cascade_policy_on_non_containment
>>
> No, I haven't. I actually suspect my references are not copied, as I
> perform a serie of CopyAll(...) actions on various collections of my
> model. The references within these collections would not be copied..
>
> I will try with these options.
>
> rgds Christophe
>
>
>
>> if it is a non-containment then the default is that MERGE, PERSIST and
>> REFRESH are set. For containment the default is ALL.
>>
>> In general for many-to-one associations Hibernate will work best with
>> minimal cascade settings. This works fine in normal production
>> situations (for example in case a sales order references a currency
>> then one can assume that the currency has already been created in the
>> database). For these large copy action though, this approach does not
>> work well as you are not sure about the order in which data is
>> committed to the database.
>>
>> gr. Martin
>>
>> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>>> Hi,
>>>
>>> I am doing a raw copy from one resource to my Teneo Hibernate resource.
>>> When saving the hibernate resource, I get the exception below.
>>>
>>> What would be the best way to (deep) copy the content of one resource so
>>> the Hibernate resource and it's references are correctly filled?
>>>
>>> Now I use this:
>>>
>>> Library dbLib = Util.getDBLibrary();
>>> if (dbLib != null) {
>>> dbLib.getCompanies().addAll( EcoreUtil.copyAll(srcLib.getCompanies()));
>>> ....
>>>
>>>
>>>
>>> Caused by: org.hibernate.TransientObjectException: object references an
>>> unsaved transient instance - save the transient instance before
>>> flushing: Company
>>> at
>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>
>>>
>>> at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>>
>>> Thanks Christophe
>>
>>
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554481 is a reply to message #554472] Mon, 23 August 2010 11:01 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
This seems to be more a EcoreUtil question, so I hope Ed can give some good pointers.

gr. Martin

On 08/23/2010 12:39 PM, Christophe Bouhier wrote:
> Christophe Bouhier wrote:
>
> Martin, I have the same issue, even with these options.
> So it's a copy issue.
>
> I just don't know how to copy the children of an EObject which consists
> of various EList to a peer EObject while maintaining all references....
>
> i.e.
>
>
> original: Lib
> - Companies
> - Equipments
>
>
> DB: Lib (existing)
> - Companies (Copy)
> - Equipments (Copy
>
>
> What I do now is
>
>
> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>
> So references between equipments and companies get lost it seems.
>
>
> rgds Christophe
>
>
>
>
>
>> Thanks Martin,
>> see inline on some response.
>>
>> Martin Taal wrote:
>>> Hi Christophe,
>>> You have to tell Hibernate to cascade the persist operation for the
>>> reference on which it fails. Have you set one of these options?
>>> teneo.mapping.cascade_policy_on_containment
>>> teneo.mapping.cascade_policy_on_non_containment
>>>
>> No, I haven't. I actually suspect my references are not copied, as I
>> perform a serie of CopyAll(...) actions on various collections of my
>> model. The references within these collections would not be copied..
>>
>> I will try with these options.
>>
>> rgds Christophe
>>
>>
>>
>>> if it is a non-containment then the default is that MERGE, PERSIST
>>> and REFRESH are set. For containment the default is ALL.
>>>
>>> In general for many-to-one associations Hibernate will work best with
>>> minimal cascade settings. This works fine in normal production
>>> situations (for example in case a sales order references a currency
>>> then one can assume that the currency has already been created in the
>>> database). For these large copy action though, this approach does not
>>> work well as you are not sure about the order in which data is
>>> committed to the database.
>>>
>>> gr. Martin
>>>
>>> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>>>> Hi,
>>>>
>>>> I am doing a raw copy from one resource to my Teneo Hibernate resource.
>>>> When saving the hibernate resource, I get the exception below.
>>>>
>>>> What would be the best way to (deep) copy the content of one
>>>> resource so
>>>> the Hibernate resource and it's references are correctly filled?
>>>>
>>>> Now I use this:
>>>>
>>>> Library dbLib = Util.getDBLibrary();
>>>> if (dbLib != null) {
>>>> dbLib.getCompanies().addAll( EcoreUtil.copyAll(srcLib.getCompanies()));
>>>> ....
>>>>
>>>>
>>>>
>>>> Caused by: org.hibernate.TransientObjectException: object references an
>>>> unsaved transient instance - save the transient instance before
>>>> flushing: Company
>>>> at
>>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>>
>>>>
>>>> at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>>>
>>>> Thanks Christophe
>>>
>>>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554483 is a reply to message #554481] Mon, 23 August 2010 11:12 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
Actually, I think the options you mentioned do have some effect.
I set them like this now:


props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_CONTA INMENT, "ALL");

props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_C ONTAINMENT,
"MERGE,PERSIST,REFRESH");


But now, I get another error:

The referred object Company#Huawei is unique, but likely referenced by
various other objects.

*********

Caused by: org.hibernate.NonUniqueObjectException: a different object
with the same identifier value was already associated with the session:
[Company#Huawei]
at
org.hibernate.event.def.AbstractSaveEventListener.performSav e(AbstractSaveEventListener.java:191)
at
org.hibernate.event.def.AbstractSaveEventListener.saveWithGe neratedId(AbstractSaveEventListener.java:144)


rgds Christophe


Martin Taal wrote:
> This seems to be more a EcoreUtil question, so I hope Ed can give some
> good pointers.
>
> gr. Martin
>
> On 08/23/2010 12:39 PM, Christophe Bouhier wrote:
>> Christophe Bouhier wrote:
>>
>> Martin, I have the same issue, even with these options.
>> So it's a copy issue.
>>
>> I just don't know how to copy the children of an EObject which consists
>> of various EList to a peer EObject while maintaining all references....
>>
>> i.e.
>>
>>
>> original: Lib
>> - Companies
>> - Equipments
>>
>>
>> DB: Lib (existing)
>> - Companies (Copy)
>> - Equipments (Copy
>>
>>
>> What I do now is
>>
>>
>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>
>> So references between equipments and companies get lost it seems.
>>
>>
>> rgds Christophe
>>
>>
>>
>>
>>
>>> Thanks Martin,
>>> see inline on some response.
>>>
>>> Martin Taal wrote:
>>>> Hi Christophe,
>>>> You have to tell Hibernate to cascade the persist operation for the
>>>> reference on which it fails. Have you set one of these options?
>>>> teneo.mapping.cascade_policy_on_containment
>>>> teneo.mapping.cascade_policy_on_non_containment
>>>>
>>> No, I haven't. I actually suspect my references are not copied, as I
>>> perform a serie of CopyAll(...) actions on various collections of my
>>> model. The references within these collections would not be copied..
>>>
>>> I will try with these options.
>>>
>>> rgds Christophe
>>>
>>>
>>>
>>>> if it is a non-containment then the default is that MERGE, PERSIST
>>>> and REFRESH are set. For containment the default is ALL.
>>>>
>>>> In general for many-to-one associations Hibernate will work best with
>>>> minimal cascade settings. This works fine in normal production
>>>> situations (for example in case a sales order references a currency
>>>> then one can assume that the currency has already been created in the
>>>> database). For these large copy action though, this approach does not
>>>> work well as you are not sure about the order in which data is
>>>> committed to the database.
>>>>
>>>> gr. Martin
>>>>
>>>> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>>>>> Hi,
>>>>>
>>>>> I am doing a raw copy from one resource to my Teneo Hibernate
>>>>> resource.
>>>>> When saving the hibernate resource, I get the exception below.
>>>>>
>>>>> What would be the best way to (deep) copy the content of one
>>>>> resource so
>>>>> the Hibernate resource and it's references are correctly filled?
>>>>>
>>>>> Now I use this:
>>>>>
>>>>> Library dbLib = Util.getDBLibrary();
>>>>> if (dbLib != null) {
>>>>> dbLib.getCompanies().addAll(
>>>>> EcoreUtil.copyAll(srcLib.getCompanies()));
>>>>> ....
>>>>>
>>>>>
>>>>>
>>>>> Caused by: org.hibernate.TransientObjectException: object
>>>>> references an
>>>>> unsaved transient instance - save the transient instance before
>>>>> flushing: Company
>>>>> at
>>>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>>>
>>>>>
>>>>>
>>>>> at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>>>>
>>>>> Thanks Christophe
>>>>
>>>>
>
>
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554490 is a reply to message #554483] Mon, 23 August 2010 11:30 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Yes, this is the issue you identified, one object (the Huawei company) is copied twice, so 2 instances exist for
Hibernate (with the same database id, which is not allowed).

gr. Martin

On 08/23/2010 01:12 PM, Christophe Bouhier wrote:
> Actually, I think the options you mentioned do have some effect.
> I set them like this now:
>
>
> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_CONTA INMENT, "ALL");
> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_C ONTAINMENT,
> "MERGE,PERSIST,REFRESH");
>
>
> But now, I get another error:
>
> The referred object Company#Huawei is unique, but likely referenced by
> various other objects.
>
> *********
>
> Caused by: org.hibernate.NonUniqueObjectException: a different object
> with the same identifier value was already associated with the session:
> [Company#Huawei]
> at
> org.hibernate.event.def.AbstractSaveEventListener.performSav e(AbstractSaveEventListener.java:191)
>
> at
> org.hibernate.event.def.AbstractSaveEventListener.saveWithGe neratedId(AbstractSaveEventListener.java:144)
>
>
>
> rgds Christophe
>
>
> Martin Taal wrote:
>> This seems to be more a EcoreUtil question, so I hope Ed can give some
>> good pointers.
>>
>> gr. Martin
>>
>> On 08/23/2010 12:39 PM, Christophe Bouhier wrote:
>>> Christophe Bouhier wrote:
>>>
>>> Martin, I have the same issue, even with these options.
>>> So it's a copy issue.
>>>
>>> I just don't know how to copy the children of an EObject which consists
>>> of various EList to a peer EObject while maintaining all references....
>>>
>>> i.e.
>>>
>>>
>>> original: Lib
>>> - Companies
>>> - Equipments
>>>
>>>
>>> DB: Lib (existing)
>>> - Companies (Copy)
>>> - Equipments (Copy
>>>
>>>
>>> What I do now is
>>>
>>>
>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>
>>> So references between equipments and companies get lost it seems.
>>>
>>>
>>> rgds Christophe
>>>
>>>
>>>
>>>
>>>
>>>> Thanks Martin,
>>>> see inline on some response.
>>>>
>>>> Martin Taal wrote:
>>>>> Hi Christophe,
>>>>> You have to tell Hibernate to cascade the persist operation for the
>>>>> reference on which it fails. Have you set one of these options?
>>>>> teneo.mapping.cascade_policy_on_containment
>>>>> teneo.mapping.cascade_policy_on_non_containment
>>>>>
>>>> No, I haven't. I actually suspect my references are not copied, as I
>>>> perform a serie of CopyAll(...) actions on various collections of my
>>>> model. The references within these collections would not be copied..
>>>>
>>>> I will try with these options.
>>>>
>>>> rgds Christophe
>>>>
>>>>
>>>>
>>>>> if it is a non-containment then the default is that MERGE, PERSIST
>>>>> and REFRESH are set. For containment the default is ALL.
>>>>>
>>>>> In general for many-to-one associations Hibernate will work best with
>>>>> minimal cascade settings. This works fine in normal production
>>>>> situations (for example in case a sales order references a currency
>>>>> then one can assume that the currency has already been created in the
>>>>> database). For these large copy action though, this approach does not
>>>>> work well as you are not sure about the order in which data is
>>>>> committed to the database.
>>>>>
>>>>> gr. Martin
>>>>>
>>>>> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>>>>>> Hi,
>>>>>>
>>>>>> I am doing a raw copy from one resource to my Teneo Hibernate
>>>>>> resource.
>>>>>> When saving the hibernate resource, I get the exception below.
>>>>>>
>>>>>> What would be the best way to (deep) copy the content of one
>>>>>> resource so
>>>>>> the Hibernate resource and it's references are correctly filled?
>>>>>>
>>>>>> Now I use this:
>>>>>>
>>>>>> Library dbLib = Util.getDBLibrary();
>>>>>> if (dbLib != null) {
>>>>>> dbLib.getCompanies().addAll(
>>>>>> EcoreUtil.copyAll(srcLib.getCompanies()));
>>>>>> ....
>>>>>>
>>>>>>
>>>>>>
>>>>>> Caused by: org.hibernate.TransientObjectException: object
>>>>>> references an
>>>>>> unsaved transient instance - save the transient instance before
>>>>>> flushing: Company
>>>>>> at
>>>>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>>>>
>>>>>>
>>>>>>
>>>>>> at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>>>>>
>>>>>> Thanks Christophe
>>>>>
>>>>>
>>
>>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554511 is a reply to message #554490] Mon, 23 August 2010 12:34 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
Martin Taal wrote:
> Yes, this is the issue you identified, one object (the Huawei company)
> is copied twice, so 2 instances exist for Hibernate (with the same
> database id, which is not allowed).
>

Yes, the object indeed already existed in the DB.
I still don't understand, why I am not allowed to create another object
in the DB (Though with the same name)?

Which databse id do you refer to below? (Apologies for the wrapping).


+------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
| name | dtype | e_version | rssurl | shortname |
symbol | website | library_companies_e_id |
library_companies_idx | econtainer_class | e_container |
e_container_feature_name
|
+------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
| 3GPP | Company | 0 | NULL | NULL |
NULL | www.3gpp.org | 1 |
4 | Library | 1 |
http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
|
| Cisco | Company | 0 | NULL | NULL |
NULL | www.cisco.com | 1 |
3 | Library | 1 |
http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
|
| Huawei | Company | 0 | NULL | NULL |
NULL | www.huawei.com | 1 |
0 | Library | 1 |
http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
|
| Nokia Siemens Networks | Company | 0 | NULL | NULL |
NULL | www.nsn.com | 1 |
1 | Library | 1 |
http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
|
| Tekelec | Company | 0 | NULL | NULL |
NULL | www.tekelec.com | 1 |
2 | Library | 1 |
http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
|
+------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+



> gr. Martin
>
> On 08/23/2010 01:12 PM, Christophe Bouhier wrote:
>> Actually, I think the options you mentioned do have some effect.
>> I set them like this now:
>>
>>
>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_CONTA INMENT,
>> "ALL");
>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_C ONTAINMENT,
>> "MERGE,PERSIST,REFRESH");
>>
>>
>> But now, I get another error:
>>
>> The referred object Company#Huawei is unique, but likely referenced by
>> various other objects.
>>
>> *********
>>
>> Caused by: org.hibernate.NonUniqueObjectException: a different object
>> with the same identifier value was already associated with the session:
>> [Company#Huawei]
>> at
>> org.hibernate.event.def.AbstractSaveEventListener.performSav e(AbstractSaveEventListener.java:191)
>>
>>
>> at
>> org.hibernate.event.def.AbstractSaveEventListener.saveWithGe neratedId(AbstractSaveEventListener.java:144)
>>
>>
>>
>>
>> rgds Christophe
>>
>>
>> Martin Taal wrote:
>>> This seems to be more a EcoreUtil question, so I hope Ed can give some
>>> good pointers.
>>>
>>> gr. Martin
>>>
>>> On 08/23/2010 12:39 PM, Christophe Bouhier wrote:
>>>> Christophe Bouhier wrote:
>>>>
>>>> Martin, I have the same issue, even with these options.
>>>> So it's a copy issue.
>>>>
>>>> I just don't know how to copy the children of an EObject which consists
>>>> of various EList to a peer EObject while maintaining all references....
>>>>
>>>> i.e.
>>>>
>>>>
>>>> original: Lib
>>>> - Companies
>>>> - Equipments
>>>>
>>>>
>>>> DB: Lib (existing)
>>>> - Companies (Copy)
>>>> - Equipments (Copy
>>>>
>>>>
>>>> What I do now is
>>>>
>>>>
>>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>>
>>>> So references between equipments and companies get lost it seems.
>>>>
>>>>
>>>> rgds Christophe
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>> Thanks Martin,
>>>>> see inline on some response.
>>>>>
>>>>> Martin Taal wrote:
>>>>>> Hi Christophe,
>>>>>> You have to tell Hibernate to cascade the persist operation for the
>>>>>> reference on which it fails. Have you set one of these options?
>>>>>> teneo.mapping.cascade_policy_on_containment
>>>>>> teneo.mapping.cascade_policy_on_non_containment
>>>>>>
>>>>> No, I haven't. I actually suspect my references are not copied, as I
>>>>> perform a serie of CopyAll(...) actions on various collections of my
>>>>> model. The references within these collections would not be copied..
>>>>>
>>>>> I will try with these options.
>>>>>
>>>>> rgds Christophe
>>>>>
>>>>>
>>>>>
>>>>>> if it is a non-containment then the default is that MERGE, PERSIST
>>>>>> and REFRESH are set. For containment the default is ALL.
>>>>>>
>>>>>> In general for many-to-one associations Hibernate will work best with
>>>>>> minimal cascade settings. This works fine in normal production
>>>>>> situations (for example in case a sales order references a currency
>>>>>> then one can assume that the currency has already been created in the
>>>>>> database). For these large copy action though, this approach does not
>>>>>> work well as you are not sure about the order in which data is
>>>>>> committed to the database.
>>>>>>
>>>>>> gr. Martin
>>>>>>
>>>>>> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>>>>>>> Hi,
>>>>>>>
>>>>>>> I am doing a raw copy from one resource to my Teneo Hibernate
>>>>>>> resource.
>>>>>>> When saving the hibernate resource, I get the exception below.
>>>>>>>
>>>>>>> What would be the best way to (deep) copy the content of one
>>>>>>> resource so
>>>>>>> the Hibernate resource and it's references are correctly filled?
>>>>>>>
>>>>>>> Now I use this:
>>>>>>>
>>>>>>> Library dbLib = Util.getDBLibrary();
>>>>>>> if (dbLib != null) {
>>>>>>> dbLib.getCompanies().addAll(
>>>>>>> EcoreUtil.copyAll(srcLib.getCompanies()));
>>>>>>> ....
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> Caused by: org.hibernate.TransientObjectException: object
>>>>>>> references an
>>>>>>> unsaved transient instance - save the transient instance before
>>>>>>> flushing: Company
>>>>>>> at
>>>>>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>>>>>>
>>>>>>> Thanks Christophe
>>>>>>
>>>>>>
>>>
>>>
>
>
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554512 is a reply to message #554511] Mon, 23 August 2010 12:43 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Christophe,
I understand that you execute these lines:
dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));

The first line will copy the companies, as there are references from equipments to companies, the second line will again
copy the referenced companies. So one company will then be copied twice (and exist twice in memory), when you then
persist the two sets (companies and equipments), these 2 instances are persisted by Hibernate resulting in the error.
This because Hibernate only allows one instance for a record to be present in one session.

At least this is what I understand from this situation.

gr. Martin

On 08/23/2010 02:34 PM, Christophe Bouhier wrote:
> Martin Taal wrote:
>> Yes, this is the issue you identified, one object (the Huawei company)
>> is copied twice, so 2 instances exist for Hibernate (with the same
>> database id, which is not allowed).
>>
>
> Yes, the object indeed already existed in the DB.
> I still don't understand, why I am not allowed to create another object
> in the DB (Though with the same name)?
>
> Which databse id do you refer to below? (Apologies for the wrapping).
>
>
> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>
> | name | dtype | e_version | rssurl | shortname | symbol | website |
> library_companies_e_id | library_companies_idx | econtainer_class |
> e_container | e_container_feature_name |
> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>
> | 3GPP | Company | 0 | NULL | NULL | NULL | www.3gpp.org | 1 | 4 |
> Library | 1 |
> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
> |
> | Cisco | Company | 0 | NULL | NULL | NULL | www.cisco.com | 1 | 3 |
> Library | 1 |
> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
> |
> | Huawei | Company | 0 | NULL | NULL | NULL | www.huawei.com | 1 | 0 |
> Library | 1 |
> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
> |
> | Nokia Siemens Networks | Company | 0 | NULL | NULL | NULL |
> www.nsn.com | 1 | 1 | Library | 1 |
> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
> |
> | Tekelec | Company | 0 | NULL | NULL | NULL | www.tekelec.com | 1 | 2 |
> Library | 1 |
> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
> |
> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>
>
>
>
>> gr. Martin
>>
>> On 08/23/2010 01:12 PM, Christophe Bouhier wrote:
>>> Actually, I think the options you mentioned do have some effect.
>>> I set them like this now:
>>>
>>>
>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_CONTA INMENT,
>>> "ALL");
>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_C ONTAINMENT,
>>> "MERGE,PERSIST,REFRESH");
>>>
>>>
>>> But now, I get another error:
>>>
>>> The referred object Company#Huawei is unique, but likely referenced by
>>> various other objects.
>>>
>>> *********
>>>
>>> Caused by: org.hibernate.NonUniqueObjectException: a different object
>>> with the same identifier value was already associated with the session:
>>> [Company#Huawei]
>>> at
>>> org.hibernate.event.def.AbstractSaveEventListener.performSav e(AbstractSaveEventListener.java:191)
>>>
>>>
>>> at
>>> org.hibernate.event.def.AbstractSaveEventListener.saveWithGe neratedId(AbstractSaveEventListener.java:144)
>>>
>>>
>>>
>>>
>>> rgds Christophe
>>>
>>>
>>> Martin Taal wrote:
>>>> This seems to be more a EcoreUtil question, so I hope Ed can give some
>>>> good pointers.
>>>>
>>>> gr. Martin
>>>>
>>>> On 08/23/2010 12:39 PM, Christophe Bouhier wrote:
>>>>> Christophe Bouhier wrote:
>>>>>
>>>>> Martin, I have the same issue, even with these options.
>>>>> So it's a copy issue.
>>>>>
>>>>> I just don't know how to copy the children of an EObject which
>>>>> consists
>>>>> of various EList to a peer EObject while maintaining all
>>>>> references....
>>>>>
>>>>> i.e.
>>>>>
>>>>>
>>>>> original: Lib
>>>>> - Companies
>>>>> - Equipments
>>>>>
>>>>>
>>>>> DB: Lib (existing)
>>>>> - Companies (Copy)
>>>>> - Equipments (Copy
>>>>>
>>>>>
>>>>> What I do now is
>>>>>
>>>>>
>>>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>>>
>>>>> So references between equipments and companies get lost it seems.
>>>>>
>>>>>
>>>>> rgds Christophe
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>> Thanks Martin,
>>>>>> see inline on some response.
>>>>>>
>>>>>> Martin Taal wrote:
>>>>>>> Hi Christophe,
>>>>>>> You have to tell Hibernate to cascade the persist operation for the
>>>>>>> reference on which it fails. Have you set one of these options?
>>>>>>> teneo.mapping.cascade_policy_on_containment
>>>>>>> teneo.mapping.cascade_policy_on_non_containment
>>>>>>>
>>>>>> No, I haven't. I actually suspect my references are not copied, as I
>>>>>> perform a serie of CopyAll(...) actions on various collections of my
>>>>>> model. The references within these collections would not be copied..
>>>>>>
>>>>>> I will try with these options.
>>>>>>
>>>>>> rgds Christophe
>>>>>>
>>>>>>
>>>>>>
>>>>>>> if it is a non-containment then the default is that MERGE, PERSIST
>>>>>>> and REFRESH are set. For containment the default is ALL.
>>>>>>>
>>>>>>> In general for many-to-one associations Hibernate will work best
>>>>>>> with
>>>>>>> minimal cascade settings. This works fine in normal production
>>>>>>> situations (for example in case a sales order references a currency
>>>>>>> then one can assume that the currency has already been created in
>>>>>>> the
>>>>>>> database). For these large copy action though, this approach does
>>>>>>> not
>>>>>>> work well as you are not sure about the order in which data is
>>>>>>> committed to the database.
>>>>>>>
>>>>>>> gr. Martin
>>>>>>>
>>>>>>> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>>>>>>>> Hi,
>>>>>>>>
>>>>>>>> I am doing a raw copy from one resource to my Teneo Hibernate
>>>>>>>> resource.
>>>>>>>> When saving the hibernate resource, I get the exception below.
>>>>>>>>
>>>>>>>> What would be the best way to (deep) copy the content of one
>>>>>>>> resource so
>>>>>>>> the Hibernate resource and it's references are correctly filled?
>>>>>>>>
>>>>>>>> Now I use this:
>>>>>>>>
>>>>>>>> Library dbLib = Util.getDBLibrary();
>>>>>>>> if (dbLib != null) {
>>>>>>>> dbLib.getCompanies().addAll(
>>>>>>>> EcoreUtil.copyAll(srcLib.getCompanies()));
>>>>>>>> ....
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> Caused by: org.hibernate.TransientObjectException: object
>>>>>>>> references an
>>>>>>>> unsaved transient instance - save the transient instance before
>>>>>>>> flushing: Company
>>>>>>>> at
>>>>>>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>>>>>>>
>>>>>>>> Thanks Christophe
>>>>>>>
>>>>>>>
>>>>
>>>>
>>
>>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554513 is a reply to message #554512] Mon, 23 August 2010 12:54 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
Martin Taal wrote:
> Hi Christophe,
> I understand that you execute these lines:
> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>
> The first line will copy the companies, as there are references from
> equipments to companies, the second line will again copy the referenced
> companies. So one company will then be copied twice (and exist twice in
> memory), when you then persist the two sets (companies and equipments),
> these 2 instances are persisted by Hibernate resulting in the error.
> This because Hibernate only allows one instance for a record to be
> present in one session.

Ahah... so it seems copyReferences in EcoreUtil, actually also creates a
copy of the referenced non-containment object. mmmh, that's not what I
want. I expected only the reference like below the BodyRef would be copied:

<Elements BodyRef="#Huawei" etc.....


rgds Christophe



>
> At least this is what I understand from this situation.
>
> gr. Martin
>
> On 08/23/2010 02:34 PM, Christophe Bouhier wrote:
>> Martin Taal wrote:
>>> Yes, this is the issue you identified, one object (the Huawei company)
>>> is copied twice, so 2 instances exist for Hibernate (with the same
>>> database id, which is not allowed).
>>>
>>
>> Yes, the object indeed already existed in the DB.
>> I still don't understand, why I am not allowed to create another object
>> in the DB (Though with the same name)?
>>
>> Which databse id do you refer to below? (Apologies for the wrapping).
>>
>>
>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>
>>
>> | name | dtype | e_version | rssurl | shortname | symbol | website |
>> library_companies_e_id | library_companies_idx | econtainer_class |
>> e_container | e_container_feature_name |
>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>
>>
>> | 3GPP | Company | 0 | NULL | NULL | NULL | www.3gpp.org | 1 | 4 |
>> Library | 1 |
>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>
>> |
>> | Cisco | Company | 0 | NULL | NULL | NULL | www.cisco.com | 1 | 3 |
>> Library | 1 |
>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>
>> |
>> | Huawei | Company | 0 | NULL | NULL | NULL | www.huawei.com | 1 | 0 |
>> Library | 1 |
>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>
>> |
>> | Nokia Siemens Networks | Company | 0 | NULL | NULL | NULL |
>> www.nsn.com | 1 | 1 | Library | 1 |
>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>
>> |
>> | Tekelec | Company | 0 | NULL | NULL | NULL | www.tekelec.com | 1 | 2 |
>> Library | 1 |
>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>
>> |
>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>
>>
>>
>>
>>
>>> gr. Martin
>>>
>>> On 08/23/2010 01:12 PM, Christophe Bouhier wrote:
>>>> Actually, I think the options you mentioned do have some effect.
>>>> I set them like this now:
>>>>
>>>>
>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_CONTA INMENT,
>>>> "ALL");
>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_C ONTAINMENT,
>>>> "MERGE,PERSIST,REFRESH");
>>>>
>>>>
>>>> But now, I get another error:
>>>>
>>>> The referred object Company#Huawei is unique, but likely referenced by
>>>> various other objects.
>>>>
>>>> *********
>>>>
>>>> Caused by: org.hibernate.NonUniqueObjectException: a different object
>>>> with the same identifier value was already associated with the session:
>>>> [Company#Huawei]
>>>> at
>>>> org.hibernate.event.def.AbstractSaveEventListener.performSav e(AbstractSaveEventListener.java:191)
>>>>
>>>>
>>>>
>>>> at
>>>> org.hibernate.event.def.AbstractSaveEventListener.saveWithGe neratedId(AbstractSaveEventListener.java:144)
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> rgds Christophe
>>>>
>>>>
>>>> Martin Taal wrote:
>>>>> This seems to be more a EcoreUtil question, so I hope Ed can give some
>>>>> good pointers.
>>>>>
>>>>> gr. Martin
>>>>>
>>>>> On 08/23/2010 12:39 PM, Christophe Bouhier wrote:
>>>>>> Christophe Bouhier wrote:
>>>>>>
>>>>>> Martin, I have the same issue, even with these options.
>>>>>> So it's a copy issue.
>>>>>>
>>>>>> I just don't know how to copy the children of an EObject which
>>>>>> consists
>>>>>> of various EList to a peer EObject while maintaining all
>>>>>> references....
>>>>>>
>>>>>> i.e.
>>>>>>
>>>>>>
>>>>>> original: Lib
>>>>>> - Companies
>>>>>> - Equipments
>>>>>>
>>>>>>
>>>>>> DB: Lib (existing)
>>>>>> - Companies (Copy)
>>>>>> - Equipments (Copy
>>>>>>
>>>>>>
>>>>>> What I do now is
>>>>>>
>>>>>>
>>>>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>>>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>>>>
>>>>>>
>>>>>> So references between equipments and companies get lost it seems.
>>>>>>
>>>>>>
>>>>>> rgds Christophe
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>> Thanks Martin,
>>>>>>> see inline on some response.
>>>>>>>
>>>>>>> Martin Taal wrote:
>>>>>>>> Hi Christophe,
>>>>>>>> You have to tell Hibernate to cascade the persist operation for the
>>>>>>>> reference on which it fails. Have you set one of these options?
>>>>>>>> teneo.mapping.cascade_policy_on_containment
>>>>>>>> teneo.mapping.cascade_policy_on_non_containment
>>>>>>>>
>>>>>>> No, I haven't. I actually suspect my references are not copied, as I
>>>>>>> perform a serie of CopyAll(...) actions on various collections of my
>>>>>>> model. The references within these collections would not be copied..
>>>>>>>
>>>>>>> I will try with these options.
>>>>>>>
>>>>>>> rgds Christophe
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>> if it is a non-containment then the default is that MERGE, PERSIST
>>>>>>>> and REFRESH are set. For containment the default is ALL.
>>>>>>>>
>>>>>>>> In general for many-to-one associations Hibernate will work best
>>>>>>>> with
>>>>>>>> minimal cascade settings. This works fine in normal production
>>>>>>>> situations (for example in case a sales order references a currency
>>>>>>>> then one can assume that the currency has already been created in
>>>>>>>> the
>>>>>>>> database). For these large copy action though, this approach does
>>>>>>>> not
>>>>>>>> work well as you are not sure about the order in which data is
>>>>>>>> committed to the database.
>>>>>>>>
>>>>>>>> gr. Martin
>>>>>>>>
>>>>>>>> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>>>>>>>>> Hi,
>>>>>>>>>
>>>>>>>>> I am doing a raw copy from one resource to my Teneo Hibernate
>>>>>>>>> resource.
>>>>>>>>> When saving the hibernate resource, I get the exception below.
>>>>>>>>>
>>>>>>>>> What would be the best way to (deep) copy the content of one
>>>>>>>>> resource so
>>>>>>>>> the Hibernate resource and it's references are correctly filled?
>>>>>>>>>
>>>>>>>>> Now I use this:
>>>>>>>>>
>>>>>>>>> Library dbLib = Util.getDBLibrary();
>>>>>>>>> if (dbLib != null) {
>>>>>>>>> dbLib.getCompanies().addAll(
>>>>>>>>> EcoreUtil.copyAll(srcLib.getCompanies()));
>>>>>>>>> ....
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Caused by: org.hibernate.TransientObjectException: object
>>>>>>>>> references an
>>>>>>>>> unsaved transient instance - save the transient instance before
>>>>>>>>> flushing: Company
>>>>>>>>> at
>>>>>>>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>>>>>>>>
>>>>>>>>> Thanks Christophe
>>>>>>>>
>>>>>>>>
>>>>>
>>>>>
>>>
>>>
>
>
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554528 is a reply to message #554513] Mon, 23 August 2010 13:40 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Christophe,
It depends if the reference is a containment or not (the first should be copied, the second not). Is it a containment
association (the naming suggests no)?

gr. Martin

On 08/23/2010 02:54 PM, Christophe Bouhier wrote:
> Martin Taal wrote:
>> Hi Christophe,
>> I understand that you execute these lines:
>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>
>> The first line will copy the companies, as there are references from
>> equipments to companies, the second line will again copy the
>> referenced companies. So one company will then be copied twice (and
>> exist twice in memory), when you then persist the two sets (companies
>> and equipments), these 2 instances are persisted by Hibernate
>> resulting in the error. This because Hibernate only allows one
>> instance for a record to be present in one session.
>
> Ahah... so it seems copyReferences in EcoreUtil, actually also creates a
> copy of the referenced non-containment object. mmmh, that's not what I
> want. I expected only the reference like below the BodyRef would be copied:
>
> <Elements BodyRef="#Huawei" etc.....
>
>
> rgds Christophe
>
>
>
>>
>> At least this is what I understand from this situation.
>>
>> gr. Martin
>>
>> On 08/23/2010 02:34 PM, Christophe Bouhier wrote:
>>> Martin Taal wrote:
>>>> Yes, this is the issue you identified, one object (the Huawei company)
>>>> is copied twice, so 2 instances exist for Hibernate (with the same
>>>> database id, which is not allowed).
>>>>
>>>
>>> Yes, the object indeed already existed in the DB.
>>> I still don't understand, why I am not allowed to create another object
>>> in the DB (Though with the same name)?
>>>
>>> Which databse id do you refer to below? (Apologies for the wrapping).
>>>
>>>
>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>
>>>
>>> | name | dtype | e_version | rssurl | shortname | symbol | website |
>>> library_companies_e_id | library_companies_idx | econtainer_class |
>>> e_container | e_container_feature_name |
>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>
>>>
>>> | 3GPP | Company | 0 | NULL | NULL | NULL | www.3gpp.org | 1 | 4 |
>>> Library | 1 |
>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>
>>> |
>>> | Cisco | Company | 0 | NULL | NULL | NULL | www.cisco.com | 1 | 3 |
>>> Library | 1 |
>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>
>>> |
>>> | Huawei | Company | 0 | NULL | NULL | NULL | www.huawei.com | 1 | 0 |
>>> Library | 1 |
>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>
>>> |
>>> | Nokia Siemens Networks | Company | 0 | NULL | NULL | NULL |
>>> www.nsn.com | 1 | 1 | Library | 1 |
>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>
>>> |
>>> | Tekelec | Company | 0 | NULL | NULL | NULL | www.tekelec.com | 1 | 2 |
>>> Library | 1 |
>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>
>>> |
>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>
>>>
>>>
>>>
>>>
>>>> gr. Martin
>>>>
>>>> On 08/23/2010 01:12 PM, Christophe Bouhier wrote:
>>>>> Actually, I think the options you mentioned do have some effect.
>>>>> I set them like this now:
>>>>>
>>>>>
>>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_CONTA INMENT,
>>>>> "ALL");
>>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_C ONTAINMENT,
>>>>>
>>>>> "MERGE,PERSIST,REFRESH");
>>>>>
>>>>>
>>>>> But now, I get another error:
>>>>>
>>>>> The referred object Company#Huawei is unique, but likely referenced by
>>>>> various other objects.
>>>>>
>>>>> *********
>>>>>
>>>>> Caused by: org.hibernate.NonUniqueObjectException: a different object
>>>>> with the same identifier value was already associated with the
>>>>> session:
>>>>> [Company#Huawei]
>>>>> at
>>>>> org.hibernate.event.def.AbstractSaveEventListener.performSav e(AbstractSaveEventListener.java:191)
>>>>>
>>>>>
>>>>>
>>>>> at
>>>>> org.hibernate.event.def.AbstractSaveEventListener.saveWithGe neratedId(AbstractSaveEventListener.java:144)
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> rgds Christophe
>>>>>
>>>>>
>>>>> Martin Taal wrote:
>>>>>> This seems to be more a EcoreUtil question, so I hope Ed can give
>>>>>> some
>>>>>> good pointers.
>>>>>>
>>>>>> gr. Martin
>>>>>>
>>>>>> On 08/23/2010 12:39 PM, Christophe Bouhier wrote:
>>>>>>> Christophe Bouhier wrote:
>>>>>>>
>>>>>>> Martin, I have the same issue, even with these options.
>>>>>>> So it's a copy issue.
>>>>>>>
>>>>>>> I just don't know how to copy the children of an EObject which
>>>>>>> consists
>>>>>>> of various EList to a peer EObject while maintaining all
>>>>>>> references....
>>>>>>>
>>>>>>> i.e.
>>>>>>>
>>>>>>>
>>>>>>> original: Lib
>>>>>>> - Companies
>>>>>>> - Equipments
>>>>>>>
>>>>>>>
>>>>>>> DB: Lib (existing)
>>>>>>> - Companies (Copy)
>>>>>>> - Equipments (Copy
>>>>>>>
>>>>>>>
>>>>>>> What I do now is
>>>>>>>
>>>>>>>
>>>>>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>>>>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>>>>>
>>>>>>>
>>>>>>> So references between equipments and companies get lost it seems.
>>>>>>>
>>>>>>>
>>>>>>> rgds Christophe
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>> Thanks Martin,
>>>>>>>> see inline on some response.
>>>>>>>>
>>>>>>>> Martin Taal wrote:
>>>>>>>>> Hi Christophe,
>>>>>>>>> You have to tell Hibernate to cascade the persist operation for
>>>>>>>>> the
>>>>>>>>> reference on which it fails. Have you set one of these options?
>>>>>>>>> teneo.mapping.cascade_policy_on_containment
>>>>>>>>> teneo.mapping.cascade_policy_on_non_containment
>>>>>>>>>
>>>>>>>> No, I haven't. I actually suspect my references are not copied,
>>>>>>>> as I
>>>>>>>> perform a serie of CopyAll(...) actions on various collections
>>>>>>>> of my
>>>>>>>> model. The references within these collections would not be
>>>>>>>> copied..
>>>>>>>>
>>>>>>>> I will try with these options.
>>>>>>>>
>>>>>>>> rgds Christophe
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>> if it is a non-containment then the default is that MERGE, PERSIST
>>>>>>>>> and REFRESH are set. For containment the default is ALL.
>>>>>>>>>
>>>>>>>>> In general for many-to-one associations Hibernate will work best
>>>>>>>>> with
>>>>>>>>> minimal cascade settings. This works fine in normal production
>>>>>>>>> situations (for example in case a sales order references a
>>>>>>>>> currency
>>>>>>>>> then one can assume that the currency has already been created in
>>>>>>>>> the
>>>>>>>>> database). For these large copy action though, this approach does
>>>>>>>>> not
>>>>>>>>> work well as you are not sure about the order in which data is
>>>>>>>>> committed to the database.
>>>>>>>>>
>>>>>>>>> gr. Martin
>>>>>>>>>
>>>>>>>>> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>>>>>>>>>> Hi,
>>>>>>>>>>
>>>>>>>>>> I am doing a raw copy from one resource to my Teneo Hibernate
>>>>>>>>>> resource.
>>>>>>>>>> When saving the hibernate resource, I get the exception below.
>>>>>>>>>>
>>>>>>>>>> What would be the best way to (deep) copy the content of one
>>>>>>>>>> resource so
>>>>>>>>>> the Hibernate resource and it's references are correctly filled?
>>>>>>>>>>
>>>>>>>>>> Now I use this:
>>>>>>>>>>
>>>>>>>>>> Library dbLib = Util.getDBLibrary();
>>>>>>>>>> if (dbLib != null) {
>>>>>>>>>> dbLib.getCompanies().addAll(
>>>>>>>>>> EcoreUtil.copyAll(srcLib.getCompanies()));
>>>>>>>>>> ....
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Caused by: org.hibernate.TransientObjectException: object
>>>>>>>>>> references an
>>>>>>>>>> unsaved transient instance - save the transient instance before
>>>>>>>>>> flushing: Company
>>>>>>>>>> at
>>>>>>>>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> at
>>>>>>>>>> org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>>>>>>>>>
>>>>>>>>>> Thanks Christophe
>>>>>>>>>
>>>>>>>>>
>>>>>>
>>>>>>
>>>>
>>>>
>>
>>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554548 is a reply to message #554528] Mon, 23 August 2010 14:02 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
Martin Taal wrote:
> Hi Christophe,
> It depends if the reference is a containment or not (the first should be
> copied, the second not). Is it a containment association (the naming
> suggests no)?

No, it's a non-containment.

>
> gr. Martin
>
> On 08/23/2010 02:54 PM, Christophe Bouhier wrote:
>> Martin Taal wrote:
>>> Hi Christophe,
>>> I understand that you execute these lines:
>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>
>>> The first line will copy the companies, as there are references from
>>> equipments to companies, the second line will again copy the
>>> referenced companies. So one company will then be copied twice (and
>>> exist twice in memory), when you then persist the two sets (companies
>>> and equipments), these 2 instances are persisted by Hibernate
>>> resulting in the error. This because Hibernate only allows one
>>> instance for a record to be present in one session.
>>
>> Ahah... so it seems copyReferences in EcoreUtil, actually also creates a
>> copy of the referenced non-containment object. mmmh, that's not what I
>> want. I expected only the reference like below the BodyRef would be
>> copied:
>>
>> <Elements BodyRef="#Huawei" etc.....
>>
>>
>> rgds Christophe
>>
>>
>>
>>>
>>> At least this is what I understand from this situation.
>>>
>>> gr. Martin
>>>
>>> On 08/23/2010 02:34 PM, Christophe Bouhier wrote:
>>>> Martin Taal wrote:
>>>>> Yes, this is the issue you identified, one object (the Huawei company)
>>>>> is copied twice, so 2 instances exist for Hibernate (with the same
>>>>> database id, which is not allowed).
>>>>>
>>>>
>>>> Yes, the object indeed already existed in the DB.
>>>> I still don't understand, why I am not allowed to create another object
>>>> in the DB (Though with the same name)?
>>>>
>>>> Which databse id do you refer to below? (Apologies for the wrapping).
>>>>
>>>>
>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>
>>>>
>>>>
>>>> | name | dtype | e_version | rssurl | shortname | symbol | website |
>>>> library_companies_e_id | library_companies_idx | econtainer_class |
>>>> e_container | e_container_feature_name |
>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>
>>>>
>>>>
>>>> | 3GPP | Company | 0 | NULL | NULL | NULL | www.3gpp.org | 1 | 4 |
>>>> Library | 1 |
>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>
>>>>
>>>> |
>>>> | Cisco | Company | 0 | NULL | NULL | NULL | www.cisco.com | 1 | 3 |
>>>> Library | 1 |
>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>
>>>>
>>>> |
>>>> | Huawei | Company | 0 | NULL | NULL | NULL | www.huawei.com | 1 | 0 |
>>>> Library | 1 |
>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>
>>>>
>>>> |
>>>> | Nokia Siemens Networks | Company | 0 | NULL | NULL | NULL |
>>>> www.nsn.com | 1 | 1 | Library | 1 |
>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>
>>>>
>>>> |
>>>> | Tekelec | Company | 0 | NULL | NULL | NULL | www.tekelec.com | 1 |
>>>> 2 |
>>>> Library | 1 |
>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>
>>>>
>>>> |
>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>> gr. Martin
>>>>>
>>>>> On 08/23/2010 01:12 PM, Christophe Bouhier wrote:
>>>>>> Actually, I think the options you mentioned do have some effect.
>>>>>> I set them like this now:
>>>>>>
>>>>>>
>>>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_CONTA INMENT,
>>>>>> "ALL");
>>>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_C ONTAINMENT,
>>>>>>
>>>>>>
>>>>>> "MERGE,PERSIST,REFRESH");
>>>>>>
>>>>>>
>>>>>> But now, I get another error:
>>>>>>
>>>>>> The referred object Company#Huawei is unique, but likely
>>>>>> referenced by
>>>>>> various other objects.
>>>>>>
>>>>>> *********
>>>>>>
>>>>>> Caused by: org.hibernate.NonUniqueObjectException: a different object
>>>>>> with the same identifier value was already associated with the
>>>>>> session:
>>>>>> [Company#Huawei]
>>>>>> at
>>>>>> org.hibernate.event.def.AbstractSaveEventListener.performSav e(AbstractSaveEventListener.java:191)
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> at
>>>>>> org.hibernate.event.def.AbstractSaveEventListener.saveWithGe neratedId(AbstractSaveEventListener.java:144)
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> rgds Christophe
>>>>>>
>>>>>>
>>>>>> Martin Taal wrote:
>>>>>>> This seems to be more a EcoreUtil question, so I hope Ed can give
>>>>>>> some
>>>>>>> good pointers.
>>>>>>>
>>>>>>> gr. Martin
>>>>>>>
>>>>>>> On 08/23/2010 12:39 PM, Christophe Bouhier wrote:
>>>>>>>> Christophe Bouhier wrote:
>>>>>>>>
>>>>>>>> Martin, I have the same issue, even with these options.
>>>>>>>> So it's a copy issue.
>>>>>>>>
>>>>>>>> I just don't know how to copy the children of an EObject which
>>>>>>>> consists
>>>>>>>> of various EList to a peer EObject while maintaining all
>>>>>>>> references....
>>>>>>>>
>>>>>>>> i.e.
>>>>>>>>
>>>>>>>>
>>>>>>>> original: Lib
>>>>>>>> - Companies
>>>>>>>> - Equipments
>>>>>>>>
>>>>>>>>
>>>>>>>> DB: Lib (existing)
>>>>>>>> - Companies (Copy)
>>>>>>>> - Equipments (Copy
>>>>>>>>
>>>>>>>>
>>>>>>>> What I do now is
>>>>>>>>
>>>>>>>>
>>>>>>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>>>>>>>
>>>>>>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> So references between equipments and companies get lost it seems.
>>>>>>>>
>>>>>>>>
>>>>>>>> rgds Christophe
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>> Thanks Martin,
>>>>>>>>> see inline on some response.
>>>>>>>>>
>>>>>>>>> Martin Taal wrote:
>>>>>>>>>> Hi Christophe,
>>>>>>>>>> You have to tell Hibernate to cascade the persist operation for
>>>>>>>>>> the
>>>>>>>>>> reference on which it fails. Have you set one of these options?
>>>>>>>>>> teneo.mapping.cascade_policy_on_containment
>>>>>>>>>> teneo.mapping.cascade_policy_on_non_containment
>>>>>>>>>>
>>>>>>>>> No, I haven't. I actually suspect my references are not copied,
>>>>>>>>> as I
>>>>>>>>> perform a serie of CopyAll(...) actions on various collections
>>>>>>>>> of my
>>>>>>>>> model. The references within these collections would not be
>>>>>>>>> copied..
>>>>>>>>>
>>>>>>>>> I will try with these options.
>>>>>>>>>
>>>>>>>>> rgds Christophe
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>> if it is a non-containment then the default is that MERGE,
>>>>>>>>>> PERSIST
>>>>>>>>>> and REFRESH are set. For containment the default is ALL.
>>>>>>>>>>
>>>>>>>>>> In general for many-to-one associations Hibernate will work best
>>>>>>>>>> with
>>>>>>>>>> minimal cascade settings. This works fine in normal production
>>>>>>>>>> situations (for example in case a sales order references a
>>>>>>>>>> currency
>>>>>>>>>> then one can assume that the currency has already been created in
>>>>>>>>>> the
>>>>>>>>>> database). For these large copy action though, this approach does
>>>>>>>>>> not
>>>>>>>>>> work well as you are not sure about the order in which data is
>>>>>>>>>> committed to the database.
>>>>>>>>>>
>>>>>>>>>> gr. Martin
>>>>>>>>>>
>>>>>>>>>> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>>>>>>>>>>> Hi,
>>>>>>>>>>>
>>>>>>>>>>> I am doing a raw copy from one resource to my Teneo Hibernate
>>>>>>>>>>> resource.
>>>>>>>>>>> When saving the hibernate resource, I get the exception below.
>>>>>>>>>>>
>>>>>>>>>>> What would be the best way to (deep) copy the content of one
>>>>>>>>>>> resource so
>>>>>>>>>>> the Hibernate resource and it's references are correctly filled?
>>>>>>>>>>>
>>>>>>>>>>> Now I use this:
>>>>>>>>>>>
>>>>>>>>>>> Library dbLib = Util.getDBLibrary();
>>>>>>>>>>> if (dbLib != null) {
>>>>>>>>>>> dbLib.getCompanies().addAll(
>>>>>>>>>>> EcoreUtil.copyAll(srcLib.getCompanies()));
>>>>>>>>>>> ....
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> Caused by: org.hibernate.TransientObjectException: object
>>>>>>>>>>> references an
>>>>>>>>>>> unsaved transient instance - save the transient instance before
>>>>>>>>>>> flushing: Company
>>>>>>>>>>> at
>>>>>>>>>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> at
>>>>>>>>>>> org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>>>>>>>>>>
>>>>>>>>>>> Thanks Christophe
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>
>>>>>
>>>
>>>
>
>
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554567 is a reply to message #554548] Mon, 23 August 2010 14:20 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Is the dbid stored in the object using an efeature?

gr. Martin

On 08/23/2010 04:02 PM, Christophe Bouhier wrote:
> Martin Taal wrote:
>> Hi Christophe,
>> It depends if the reference is a containment or not (the first should
>> be copied, the second not). Is it a containment association (the
>> naming suggests no)?
>
> No, it's a non-containment.
>
>>
>> gr. Martin
>>
>> On 08/23/2010 02:54 PM, Christophe Bouhier wrote:
>>> Martin Taal wrote:
>>>> Hi Christophe,
>>>> I understand that you execute these lines:
>>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>>
>>>> The first line will copy the companies, as there are references from
>>>> equipments to companies, the second line will again copy the
>>>> referenced companies. So one company will then be copied twice (and
>>>> exist twice in memory), when you then persist the two sets (companies
>>>> and equipments), these 2 instances are persisted by Hibernate
>>>> resulting in the error. This because Hibernate only allows one
>>>> instance for a record to be present in one session.
>>>
>>> Ahah... so it seems copyReferences in EcoreUtil, actually also creates a
>>> copy of the referenced non-containment object. mmmh, that's not what I
>>> want. I expected only the reference like below the BodyRef would be
>>> copied:
>>>
>>> <Elements BodyRef="#Huawei" etc.....
>>>
>>>
>>> rgds Christophe
>>>
>>>
>>>
>>>>
>>>> At least this is what I understand from this situation.
>>>>
>>>> gr. Martin
>>>>
>>>> On 08/23/2010 02:34 PM, Christophe Bouhier wrote:
>>>>> Martin Taal wrote:
>>>>>> Yes, this is the issue you identified, one object (the Huawei
>>>>>> company)
>>>>>> is copied twice, so 2 instances exist for Hibernate (with the same
>>>>>> database id, which is not allowed).
>>>>>>
>>>>>
>>>>> Yes, the object indeed already existed in the DB.
>>>>> I still don't understand, why I am not allowed to create another
>>>>> object
>>>>> in the DB (Though with the same name)?
>>>>>
>>>>> Which databse id do you refer to below? (Apologies for the wrapping).
>>>>>
>>>>>
>>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>>
>>>>>
>>>>>
>>>>> | name | dtype | e_version | rssurl | shortname | symbol | website |
>>>>> library_companies_e_id | library_companies_idx | econtainer_class |
>>>>> e_container | e_container_feature_name |
>>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>>
>>>>>
>>>>>
>>>>> | 3GPP | Company | 0 | NULL | NULL | NULL | www.3gpp.org | 1 | 4 |
>>>>> Library | 1 |
>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>
>>>>>
>>>>> |
>>>>> | Cisco | Company | 0 | NULL | NULL | NULL | www.cisco.com | 1 | 3 |
>>>>> Library | 1 |
>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>
>>>>>
>>>>> |
>>>>> | Huawei | Company | 0 | NULL | NULL | NULL | www.huawei.com | 1 | 0 |
>>>>> Library | 1 |
>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>
>>>>>
>>>>> |
>>>>> | Nokia Siemens Networks | Company | 0 | NULL | NULL | NULL |
>>>>> www.nsn.com | 1 | 1 | Library | 1 |
>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>
>>>>>
>>>>> |
>>>>> | Tekelec | Company | 0 | NULL | NULL | NULL | www.tekelec.com | 1
>>>>> | 2 |
>>>>> Library | 1 |
>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>
>>>>>
>>>>> |
>>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>> gr. Martin
>>>>>>
>>>>>> On 08/23/2010 01:12 PM, Christophe Bouhier wrote:
>>>>>>> Actually, I think the options you mentioned do have some effect.
>>>>>>> I set them like this now:
>>>>>>>
>>>>>>>
>>>>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_CONTA INMENT,
>>>>>>> "ALL");
>>>>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_C ONTAINMENT,
>>>>>>>
>>>>>>>
>>>>>>> "MERGE,PERSIST,REFRESH");
>>>>>>>
>>>>>>>
>>>>>>> But now, I get another error:
>>>>>>>
>>>>>>> The referred object Company#Huawei is unique, but likely
>>>>>>> referenced by
>>>>>>> various other objects.
>>>>>>>
>>>>>>> *********
>>>>>>>
>>>>>>> Caused by: org.hibernate.NonUniqueObjectException: a different
>>>>>>> object
>>>>>>> with the same identifier value was already associated with the
>>>>>>> session:
>>>>>>> [Company#Huawei]
>>>>>>> at
>>>>>>> org.hibernate.event.def.AbstractSaveEventListener.performSav e(AbstractSaveEventListener.java:191)
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> at
>>>>>>> org.hibernate.event.def.AbstractSaveEventListener.saveWithGe neratedId(AbstractSaveEventListener.java:144)
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> rgds Christophe
>>>>>>>
>>>>>>>
>>>>>>> Martin Taal wrote:
>>>>>>>> This seems to be more a EcoreUtil question, so I hope Ed can give
>>>>>>>> some
>>>>>>>> good pointers.
>>>>>>>>
>>>>>>>> gr. Martin
>>>>>>>>
>>>>>>>> On 08/23/2010 12:39 PM, Christophe Bouhier wrote:
>>>>>>>>> Christophe Bouhier wrote:
>>>>>>>>>
>>>>>>>>> Martin, I have the same issue, even with these options.
>>>>>>>>> So it's a copy issue.
>>>>>>>>>
>>>>>>>>> I just don't know how to copy the children of an EObject which
>>>>>>>>> consists
>>>>>>>>> of various EList to a peer EObject while maintaining all
>>>>>>>>> references....
>>>>>>>>>
>>>>>>>>> i.e.
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> original: Lib
>>>>>>>>> - Companies
>>>>>>>>> - Equipments
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> DB: Lib (existing)
>>>>>>>>> - Companies (Copy)
>>>>>>>>> - Equipments (Copy
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> What I do now is
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>>>>>>>>
>>>>>>>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> So references between equipments and companies get lost it seems.
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> rgds Christophe
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>> Thanks Martin,
>>>>>>>>>> see inline on some response.
>>>>>>>>>>
>>>>>>>>>> Martin Taal wrote:
>>>>>>>>>>> Hi Christophe,
>>>>>>>>>>> You have to tell Hibernate to cascade the persist operation for
>>>>>>>>>>> the
>>>>>>>>>>> reference on which it fails. Have you set one of these options?
>>>>>>>>>>> teneo.mapping.cascade_policy_on_containment
>>>>>>>>>>> teneo.mapping.cascade_policy_on_non_containment
>>>>>>>>>>>
>>>>>>>>>> No, I haven't. I actually suspect my references are not copied,
>>>>>>>>>> as I
>>>>>>>>>> perform a serie of CopyAll(...) actions on various collections
>>>>>>>>>> of my
>>>>>>>>>> model. The references within these collections would not be
>>>>>>>>>> copied..
>>>>>>>>>>
>>>>>>>>>> I will try with these options.
>>>>>>>>>>
>>>>>>>>>> rgds Christophe
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>> if it is a non-containment then the default is that MERGE,
>>>>>>>>>>> PERSIST
>>>>>>>>>>> and REFRESH are set. For containment the default is ALL.
>>>>>>>>>>>
>>>>>>>>>>> In general for many-to-one associations Hibernate will work best
>>>>>>>>>>> with
>>>>>>>>>>> minimal cascade settings. This works fine in normal production
>>>>>>>>>>> situations (for example in case a sales order references a
>>>>>>>>>>> currency
>>>>>>>>>>> then one can assume that the currency has already been
>>>>>>>>>>> created in
>>>>>>>>>>> the
>>>>>>>>>>> database). For these large copy action though, this approach
>>>>>>>>>>> does
>>>>>>>>>>> not
>>>>>>>>>>> work well as you are not sure about the order in which data is
>>>>>>>>>>> committed to the database.
>>>>>>>>>>>
>>>>>>>>>>> gr. Martin
>>>>>>>>>>>
>>>>>>>>>>> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>>>>>>>>>>>> Hi,
>>>>>>>>>>>>
>>>>>>>>>>>> I am doing a raw copy from one resource to my Teneo Hibernate
>>>>>>>>>>>> resource.
>>>>>>>>>>>> When saving the hibernate resource, I get the exception below.
>>>>>>>>>>>>
>>>>>>>>>>>> What would be the best way to (deep) copy the content of one
>>>>>>>>>>>> resource so
>>>>>>>>>>>> the Hibernate resource and it's references are correctly
>>>>>>>>>>>> filled?
>>>>>>>>>>>>
>>>>>>>>>>>> Now I use this:
>>>>>>>>>>>>
>>>>>>>>>>>> Library dbLib = Util.getDBLibrary();
>>>>>>>>>>>> if (dbLib != null) {
>>>>>>>>>>>> dbLib.getCompanies().addAll(
>>>>>>>>>>>> EcoreUtil.copyAll(srcLib.getCompanies()));
>>>>>>>>>>>> ....
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> Caused by: org.hibernate.TransientObjectException: object
>>>>>>>>>>>> references an
>>>>>>>>>>>> unsaved transient instance - save the transient instance before
>>>>>>>>>>>> flushing: Company
>>>>>>>>>>>> at
>>>>>>>>>>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> at
>>>>>>>>>>>> org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> Thanks Christophe
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>
>>>>>>
>>>>
>>>>
>>
>>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554568 is a reply to message #554567] Mon, 23 August 2010 14:39 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
Martin Taal wrote:
> Is the dbid stored in the object using an efeature?

Sorry, dbid in the object?


> gr. Martin
>
> On 08/23/2010 04:02 PM, Christophe Bouhier wrote:
>> Martin Taal wrote:
>>> Hi Christophe,
>>> It depends if the reference is a containment or not (the first should
>>> be copied, the second not). Is it a containment association (the
>>> naming suggests no)?
>>
>> No, it's a non-containment.
>>
>>>
>>> gr. Martin
>>>
>>> On 08/23/2010 02:54 PM, Christophe Bouhier wrote:
>>>> Martin Taal wrote:
>>>>> Hi Christophe,
>>>>> I understand that you execute these lines:
>>>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>>>
>>>>> The first line will copy the companies, as there are references from
>>>>> equipments to companies, the second line will again copy the
>>>>> referenced companies. So one company will then be copied twice (and
>>>>> exist twice in memory), when you then persist the two sets (companies
>>>>> and equipments), these 2 instances are persisted by Hibernate
>>>>> resulting in the error. This because Hibernate only allows one
>>>>> instance for a record to be present in one session.
>>>>
>>>> Ahah... so it seems copyReferences in EcoreUtil, actually also
>>>> creates a
>>>> copy of the referenced non-containment object. mmmh, that's not what I
>>>> want. I expected only the reference like below the BodyRef would be
>>>> copied:
>>>>
>>>> <Elements BodyRef="#Huawei" etc.....
>>>>
>>>>
>>>> rgds Christophe
>>>>
>>>>
>>>>
>>>>>
>>>>> At least this is what I understand from this situation.
>>>>>
>>>>> gr. Martin
>>>>>
>>>>> On 08/23/2010 02:34 PM, Christophe Bouhier wrote:
>>>>>> Martin Taal wrote:
>>>>>>> Yes, this is the issue you identified, one object (the Huawei
>>>>>>> company)
>>>>>>> is copied twice, so 2 instances exist for Hibernate (with the same
>>>>>>> database id, which is not allowed).
>>>>>>>
>>>>>>
>>>>>> Yes, the object indeed already existed in the DB.
>>>>>> I still don't understand, why I am not allowed to create another
>>>>>> object
>>>>>> in the DB (Though with the same name)?
>>>>>>
>>>>>> Which databse id do you refer to below? (Apologies for the wrapping).
>>>>>>
>>>>>>
>>>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> | name | dtype | e_version | rssurl | shortname | symbol | website |
>>>>>> library_companies_e_id | library_companies_idx | econtainer_class |
>>>>>> e_container | e_container_feature_name |
>>>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> | 3GPP | Company | 0 | NULL | NULL | NULL | www.3gpp.org | 1 | 4 |
>>>>>> Library | 1 |
>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>
>>>>>>
>>>>>>
>>>>>> |
>>>>>> | Cisco | Company | 0 | NULL | NULL | NULL | www.cisco.com | 1 | 3 |
>>>>>> Library | 1 |
>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>
>>>>>>
>>>>>>
>>>>>> |
>>>>>> | Huawei | Company | 0 | NULL | NULL | NULL | www.huawei.com | 1 |
>>>>>> 0 |
>>>>>> Library | 1 |
>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>
>>>>>>
>>>>>>
>>>>>> |
>>>>>> | Nokia Siemens Networks | Company | 0 | NULL | NULL | NULL |
>>>>>> www.nsn.com | 1 | 1 | Library | 1 |
>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>
>>>>>>
>>>>>>
>>>>>> |
>>>>>> | Tekelec | Company | 0 | NULL | NULL | NULL | www.tekelec.com | 1
>>>>>> | 2 |
>>>>>> Library | 1 |
>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>
>>>>>>
>>>>>>
>>>>>> |
>>>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>> gr. Martin
>>>>>>>
>>>>>>> On 08/23/2010 01:12 PM, Christophe Bouhier wrote:
>>>>>>>> Actually, I think the options you mentioned do have some effect.
>>>>>>>> I set them like this now:
>>>>>>>>
>>>>>>>>
>>>>>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_CONTA INMENT,
>>>>>>>> "ALL");
>>>>>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_C ONTAINMENT,
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> "MERGE,PERSIST,REFRESH");
>>>>>>>>
>>>>>>>>
>>>>>>>> But now, I get another error:
>>>>>>>>
>>>>>>>> The referred object Company#Huawei is unique, but likely
>>>>>>>> referenced by
>>>>>>>> various other objects.
>>>>>>>>
>>>>>>>> *********
>>>>>>>>
>>>>>>>> Caused by: org.hibernate.NonUniqueObjectException: a different
>>>>>>>> object
>>>>>>>> with the same identifier value was already associated with the
>>>>>>>> session:
>>>>>>>> [Company#Huawei]
>>>>>>>> at
>>>>>>>> org.hibernate.event.def.AbstractSaveEventListener.performSav e(AbstractSaveEventListener.java:191)
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.hibernate.event.def.AbstractSaveEventListener.saveWithGe neratedId(AbstractSaveEventListener.java:144)
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> rgds Christophe
>>>>>>>>
>>>>>>>>
>>>>>>>> Martin Taal wrote:
>>>>>>>>> This seems to be more a EcoreUtil question, so I hope Ed can give
>>>>>>>>> some
>>>>>>>>> good pointers.
>>>>>>>>>
>>>>>>>>> gr. Martin
>>>>>>>>>
>>>>>>>>> On 08/23/2010 12:39 PM, Christophe Bouhier wrote:
>>>>>>>>>> Christophe Bouhier wrote:
>>>>>>>>>>
>>>>>>>>>> Martin, I have the same issue, even with these options.
>>>>>>>>>> So it's a copy issue.
>>>>>>>>>>
>>>>>>>>>> I just don't know how to copy the children of an EObject which
>>>>>>>>>> consists
>>>>>>>>>> of various EList to a peer EObject while maintaining all
>>>>>>>>>> references....
>>>>>>>>>>
>>>>>>>>>> i.e.
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> original: Lib
>>>>>>>>>> - Companies
>>>>>>>>>> - Equipments
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> DB: Lib (existing)
>>>>>>>>>> - Companies (Copy)
>>>>>>>>>> - Equipments (Copy
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> What I do now is
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> So references between equipments and companies get lost it seems.
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> rgds Christophe
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>> Thanks Martin,
>>>>>>>>>>> see inline on some response.
>>>>>>>>>>>
>>>>>>>>>>> Martin Taal wrote:
>>>>>>>>>>>> Hi Christophe,
>>>>>>>>>>>> You have to tell Hibernate to cascade the persist operation for
>>>>>>>>>>>> the
>>>>>>>>>>>> reference on which it fails. Have you set one of these options?
>>>>>>>>>>>> teneo.mapping.cascade_policy_on_containment
>>>>>>>>>>>> teneo.mapping.cascade_policy_on_non_containment
>>>>>>>>>>>>
>>>>>>>>>>> No, I haven't. I actually suspect my references are not copied,
>>>>>>>>>>> as I
>>>>>>>>>>> perform a serie of CopyAll(...) actions on various collections
>>>>>>>>>>> of my
>>>>>>>>>>> model. The references within these collections would not be
>>>>>>>>>>> copied..
>>>>>>>>>>>
>>>>>>>>>>> I will try with these options.
>>>>>>>>>>>
>>>>>>>>>>> rgds Christophe
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>> if it is a non-containment then the default is that MERGE,
>>>>>>>>>>>> PERSIST
>>>>>>>>>>>> and REFRESH are set. For containment the default is ALL.
>>>>>>>>>>>>
>>>>>>>>>>>> In general for many-to-one associations Hibernate will work
>>>>>>>>>>>> best
>>>>>>>>>>>> with
>>>>>>>>>>>> minimal cascade settings. This works fine in normal production
>>>>>>>>>>>> situations (for example in case a sales order references a
>>>>>>>>>>>> currency
>>>>>>>>>>>> then one can assume that the currency has already been
>>>>>>>>>>>> created in
>>>>>>>>>>>> the
>>>>>>>>>>>> database). For these large copy action though, this approach
>>>>>>>>>>>> does
>>>>>>>>>>>> not
>>>>>>>>>>>> work well as you are not sure about the order in which data is
>>>>>>>>>>>> committed to the database.
>>>>>>>>>>>>
>>>>>>>>>>>> gr. Martin
>>>>>>>>>>>>
>>>>>>>>>>>> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>>>>>>>>>>>>> Hi,
>>>>>>>>>>>>>
>>>>>>>>>>>>> I am doing a raw copy from one resource to my Teneo Hibernate
>>>>>>>>>>>>> resource.
>>>>>>>>>>>>> When saving the hibernate resource, I get the exception below.
>>>>>>>>>>>>>
>>>>>>>>>>>>> What would be the best way to (deep) copy the content of one
>>>>>>>>>>>>> resource so
>>>>>>>>>>>>> the Hibernate resource and it's references are correctly
>>>>>>>>>>>>> filled?
>>>>>>>>>>>>>
>>>>>>>>>>>>> Now I use this:
>>>>>>>>>>>>>
>>>>>>>>>>>>> Library dbLib = Util.getDBLibrary();
>>>>>>>>>>>>> if (dbLib != null) {
>>>>>>>>>>>>> dbLib.getCompanies().addAll(
>>>>>>>>>>>>> EcoreUtil.copyAll(srcLib.getCompanies()));
>>>>>>>>>>>>> ....
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>> Caused by: org.hibernate.TransientObjectException: object
>>>>>>>>>>>>> references an
>>>>>>>>>>>>> unsaved transient instance - save the transient instance
>>>>>>>>>>>>> before
>>>>>>>>>>>>> flushing: Company
>>>>>>>>>>>>> at
>>>>>>>>>>>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>> at
>>>>>>>>>>>>> org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>> Thanks Christophe
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>
>>>>>
>>>
>>>
>
>
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #554569 is a reply to message #554568] Mon, 23 August 2010 14:43 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Yes, if the dbid is an efeature then after copying, a second instance, with the same dbid, exists in-memory. So when
copying you have to clear the dbid so that Hibernate knows it is a new object.

gr. Martin

On 08/23/2010 04:39 PM, Christophe Bouhier wrote:
> Martin Taal wrote:
>> Is the dbid stored in the object using an efeature?
>
> Sorry, dbid in the object?
>
>
>> gr. Martin
>>
>> On 08/23/2010 04:02 PM, Christophe Bouhier wrote:
>>> Martin Taal wrote:
>>>> Hi Christophe,
>>>> It depends if the reference is a containment or not (the first should
>>>> be copied, the second not). Is it a containment association (the
>>>> naming suggests no)?
>>>
>>> No, it's a non-containment.
>>>
>>>>
>>>> gr. Martin
>>>>
>>>> On 08/23/2010 02:54 PM, Christophe Bouhier wrote:
>>>>> Martin Taal wrote:
>>>>>> Hi Christophe,
>>>>>> I understand that you execute these lines:
>>>>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>>>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>>>>
>>>>>>
>>>>>> The first line will copy the companies, as there are references from
>>>>>> equipments to companies, the second line will again copy the
>>>>>> referenced companies. So one company will then be copied twice (and
>>>>>> exist twice in memory), when you then persist the two sets (companies
>>>>>> and equipments), these 2 instances are persisted by Hibernate
>>>>>> resulting in the error. This because Hibernate only allows one
>>>>>> instance for a record to be present in one session.
>>>>>
>>>>> Ahah... so it seems copyReferences in EcoreUtil, actually also
>>>>> creates a
>>>>> copy of the referenced non-containment object. mmmh, that's not what I
>>>>> want. I expected only the reference like below the BodyRef would be
>>>>> copied:
>>>>>
>>>>> <Elements BodyRef="#Huawei" etc.....
>>>>>
>>>>>
>>>>> rgds Christophe
>>>>>
>>>>>
>>>>>
>>>>>>
>>>>>> At least this is what I understand from this situation.
>>>>>>
>>>>>> gr. Martin
>>>>>>
>>>>>> On 08/23/2010 02:34 PM, Christophe Bouhier wrote:
>>>>>>> Martin Taal wrote:
>>>>>>>> Yes, this is the issue you identified, one object (the Huawei
>>>>>>>> company)
>>>>>>>> is copied twice, so 2 instances exist for Hibernate (with the same
>>>>>>>> database id, which is not allowed).
>>>>>>>>
>>>>>>>
>>>>>>> Yes, the object indeed already existed in the DB.
>>>>>>> I still don't understand, why I am not allowed to create another
>>>>>>> object
>>>>>>> in the DB (Though with the same name)?
>>>>>>>
>>>>>>> Which databse id do you refer to below? (Apologies for the
>>>>>>> wrapping).
>>>>>>>
>>>>>>>
>>>>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> | name | dtype | e_version | rssurl | shortname | symbol | website |
>>>>>>> library_companies_e_id | library_companies_idx | econtainer_class |
>>>>>>> e_container | e_container_feature_name |
>>>>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> | 3GPP | Company | 0 | NULL | NULL | NULL | www.3gpp.org | 1 | 4 |
>>>>>>> Library | 1 |
>>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> |
>>>>>>> | Cisco | Company | 0 | NULL | NULL | NULL | www.cisco.com | 1 | 3 |
>>>>>>> Library | 1 |
>>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> |
>>>>>>> | Huawei | Company | 0 | NULL | NULL | NULL | www.huawei.com | 1
>>>>>>> | 0 |
>>>>>>> Library | 1 |
>>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> |
>>>>>>> | Nokia Siemens Networks | Company | 0 | NULL | NULL | NULL |
>>>>>>> www.nsn.com | 1 | 1 | Library | 1 |
>>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> |
>>>>>>> | Tekelec | Company | 0 | NULL | NULL | NULL | www.tekelec.com | 1
>>>>>>> | 2 |
>>>>>>> Library | 1 |
>>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> |
>>>>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>> gr. Martin
>>>>>>>>
>>>>>>>> On 08/23/2010 01:12 PM, Christophe Bouhier wrote:
>>>>>>>>> Actually, I think the options you mentioned do have some effect.
>>>>>>>>> I set them like this now:
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_CONTA INMENT,
>>>>>>>>>
>>>>>>>>> "ALL");
>>>>>>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_C ONTAINMENT,
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> "MERGE,PERSIST,REFRESH");
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> But now, I get another error:
>>>>>>>>>
>>>>>>>>> The referred object Company#Huawei is unique, but likely
>>>>>>>>> referenced by
>>>>>>>>> various other objects.
>>>>>>>>>
>>>>>>>>> *********
>>>>>>>>>
>>>>>>>>> Caused by: org.hibernate.NonUniqueObjectException: a different
>>>>>>>>> object
>>>>>>>>> with the same identifier value was already associated with the
>>>>>>>>> session:
>>>>>>>>> [Company#Huawei]
>>>>>>>>> at
>>>>>>>>> org.hibernate.event.def.AbstractSaveEventListener.performSav e(AbstractSaveEventListener.java:191)
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.hibernate.event.def.AbstractSaveEventListener.saveWithGe neratedId(AbstractSaveEventListener.java:144)
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> rgds Christophe
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Martin Taal wrote:
>>>>>>>>>> This seems to be more a EcoreUtil question, so I hope Ed can give
>>>>>>>>>> some
>>>>>>>>>> good pointers.
>>>>>>>>>>
>>>>>>>>>> gr. Martin
>>>>>>>>>>
>>>>>>>>>> On 08/23/2010 12:39 PM, Christophe Bouhier wrote:
>>>>>>>>>>> Christophe Bouhier wrote:
>>>>>>>>>>>
>>>>>>>>>>> Martin, I have the same issue, even with these options.
>>>>>>>>>>> So it's a copy issue.
>>>>>>>>>>>
>>>>>>>>>>> I just don't know how to copy the children of an EObject which
>>>>>>>>>>> consists
>>>>>>>>>>> of various EList to a peer EObject while maintaining all
>>>>>>>>>>> references....
>>>>>>>>>>>
>>>>>>>>>>> i.e.
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> original: Lib
>>>>>>>>>>> - Companies
>>>>>>>>>>> - Equipments
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> DB: Lib (existing)
>>>>>>>>>>> - Companies (Copy)
>>>>>>>>>>> - Equipments (Copy
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> What I do now is
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> So references between equipments and companies get lost it
>>>>>>>>>>> seems.
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> rgds Christophe
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>> Thanks Martin,
>>>>>>>>>>>> see inline on some response.
>>>>>>>>>>>>
>>>>>>>>>>>> Martin Taal wrote:
>>>>>>>>>>>>> Hi Christophe,
>>>>>>>>>>>>> You have to tell Hibernate to cascade the persist operation
>>>>>>>>>>>>> for
>>>>>>>>>>>>> the
>>>>>>>>>>>>> reference on which it fails. Have you set one of these
>>>>>>>>>>>>> options?
>>>>>>>>>>>>> teneo.mapping.cascade_policy_on_containment
>>>>>>>>>>>>> teneo.mapping.cascade_policy_on_non_containment
>>>>>>>>>>>>>
>>>>>>>>>>>> No, I haven't. I actually suspect my references are not copied,
>>>>>>>>>>>> as I
>>>>>>>>>>>> perform a serie of CopyAll(...) actions on various collections
>>>>>>>>>>>> of my
>>>>>>>>>>>> model. The references within these collections would not be
>>>>>>>>>>>> copied..
>>>>>>>>>>>>
>>>>>>>>>>>> I will try with these options.
>>>>>>>>>>>>
>>>>>>>>>>>> rgds Christophe
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>> if it is a non-containment then the default is that MERGE,
>>>>>>>>>>>>> PERSIST
>>>>>>>>>>>>> and REFRESH are set. For containment the default is ALL.
>>>>>>>>>>>>>
>>>>>>>>>>>>> In general for many-to-one associations Hibernate will work
>>>>>>>>>>>>> best
>>>>>>>>>>>>> with
>>>>>>>>>>>>> minimal cascade settings. This works fine in normal production
>>>>>>>>>>>>> situations (for example in case a sales order references a
>>>>>>>>>>>>> currency
>>>>>>>>>>>>> then one can assume that the currency has already been
>>>>>>>>>>>>> created in
>>>>>>>>>>>>> the
>>>>>>>>>>>>> database). For these large copy action though, this approach
>>>>>>>>>>>>> does
>>>>>>>>>>>>> not
>>>>>>>>>>>>> work well as you are not sure about the order in which data is
>>>>>>>>>>>>> committed to the database.
>>>>>>>>>>>>>
>>>>>>>>>>>>> gr. Martin
>>>>>>>>>>>>>
>>>>>>>>>>>>> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>>>>>>>>>>>>>> Hi,
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> I am doing a raw copy from one resource to my Teneo Hibernate
>>>>>>>>>>>>>> resource.
>>>>>>>>>>>>>> When saving the hibernate resource, I get the exception
>>>>>>>>>>>>>> below.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> What would be the best way to (deep) copy the content of one
>>>>>>>>>>>>>> resource so
>>>>>>>>>>>>>> the Hibernate resource and it's references are correctly
>>>>>>>>>>>>>> filled?
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Now I use this:
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Library dbLib = Util.getDBLibrary();
>>>>>>>>>>>>>> if (dbLib != null) {
>>>>>>>>>>>>>> dbLib.getCompanies().addAll(
>>>>>>>>>>>>>> EcoreUtil.copyAll(srcLib.getCompanies()));
>>>>>>>>>>>>>> ....
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Caused by: org.hibernate.TransientObjectException: object
>>>>>>>>>>>>>> references an
>>>>>>>>>>>>>> unsaved transient instance - save the transient instance
>>>>>>>>>>>>>> before
>>>>>>>>>>>>>> flushing: Company
>>>>>>>>>>>>>> at
>>>>>>>>>>>>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> at
>>>>>>>>>>>>>> org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Thanks Christophe
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>
>>>>>>
>>>>
>>>>
>>
>>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] org.hibernate.TransientObjectException: object references an unsaved transient instance [message #555191 is a reply to message #554569] Wed, 25 August 2010 18:38 Go to previous message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
On 23-08-10 16:43, Martin Taal wrote:
> Yes, if the dbid is an efeature then after copying, a second instance,
> with the same dbid, exists in-memory. So when copying you have to clear
> the dbid so that Hibernate knows it is a new object.
>

Hi Martin,

I gave up on this for now, I just don't understand why copying objects
from A to B should be such hassle...I don't really need duplicates, but
it seems these are created with ECoreUtil.copy...It's not my intention
to clear what I didn't want in the first place.

Thanks for your help anyways!

rgds Christophe



> gr. Martin
>
> On 08/23/2010 04:39 PM, Christophe Bouhier wrote:
>> Martin Taal wrote:
>>> Is the dbid stored in the object using an efeature?
>>
>> Sorry, dbid in the object?
>>
>>
>>> gr. Martin
>>>
>>> On 08/23/2010 04:02 PM, Christophe Bouhier wrote:
>>>> Martin Taal wrote:
>>>>> Hi Christophe,
>>>>> It depends if the reference is a containment or not (the first should
>>>>> be copied, the second not). Is it a containment association (the
>>>>> naming suggests no)?
>>>>
>>>> No, it's a non-containment.
>>>>
>>>>>
>>>>> gr. Martin
>>>>>
>>>>> On 08/23/2010 02:54 PM, Christophe Bouhier wrote:
>>>>>> Martin Taal wrote:
>>>>>>> Hi Christophe,
>>>>>>> I understand that you execute these lines:
>>>>>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>>>>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> The first line will copy the companies, as there are references from
>>>>>>> equipments to companies, the second line will again copy the
>>>>>>> referenced companies. So one company will then be copied twice (and
>>>>>>> exist twice in memory), when you then persist the two sets
>>>>>>> (companies
>>>>>>> and equipments), these 2 instances are persisted by Hibernate
>>>>>>> resulting in the error. This because Hibernate only allows one
>>>>>>> instance for a record to be present in one session.
>>>>>>
>>>>>> Ahah... so it seems copyReferences in EcoreUtil, actually also
>>>>>> creates a
>>>>>> copy of the referenced non-containment object. mmmh, that's not
>>>>>> what I
>>>>>> want. I expected only the reference like below the BodyRef would be
>>>>>> copied:
>>>>>>
>>>>>> <Elements BodyRef="#Huawei" etc.....
>>>>>>
>>>>>>
>>>>>> rgds Christophe
>>>>>>
>>>>>>
>>>>>>
>>>>>>>
>>>>>>> At least this is what I understand from this situation.
>>>>>>>
>>>>>>> gr. Martin
>>>>>>>
>>>>>>> On 08/23/2010 02:34 PM, Christophe Bouhier wrote:
>>>>>>>> Martin Taal wrote:
>>>>>>>>> Yes, this is the issue you identified, one object (the Huawei
>>>>>>>>> company)
>>>>>>>>> is copied twice, so 2 instances exist for Hibernate (with the same
>>>>>>>>> database id, which is not allowed).
>>>>>>>>>
>>>>>>>>
>>>>>>>> Yes, the object indeed already existed in the DB.
>>>>>>>> I still don't understand, why I am not allowed to create another
>>>>>>>> object
>>>>>>>> in the DB (Though with the same name)?
>>>>>>>>
>>>>>>>> Which databse id do you refer to below? (Apologies for the
>>>>>>>> wrapping).
>>>>>>>>
>>>>>>>>
>>>>>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> | name | dtype | e_version | rssurl | shortname | symbol |
>>>>>>>> website |
>>>>>>>> library_companies_e_id | library_companies_idx | econtainer_class |
>>>>>>>> e_container | e_container_feature_name |
>>>>>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> | 3GPP | Company | 0 | NULL | NULL | NULL | www.3gpp.org | 1 | 4 |
>>>>>>>> Library | 1 |
>>>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> |
>>>>>>>> | Cisco | Company | 0 | NULL | NULL | NULL | www.cisco.com | 1 |
>>>>>>>> 3 |
>>>>>>>> Library | 1 |
>>>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> |
>>>>>>>> | Huawei | Company | 0 | NULL | NULL | NULL | www.huawei.com | 1
>>>>>>>> | 0 |
>>>>>>>> Library | 1 |
>>>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> |
>>>>>>>> | Nokia Siemens Networks | Company | 0 | NULL | NULL | NULL |
>>>>>>>> www.nsn.com | 1 | 1 | Library | 1 |
>>>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> |
>>>>>>>> | Tekelec | Company | 0 | NULL | NULL | NULL | www.tekelec.com | 1
>>>>>>>> | 2 |
>>>>>>>> Library | 1 |
>>>>>>>> http://www.netxforge.com/15072010/netxstudio_;_Library_;_http://www.netxforge.com/15072010/netxstudio_;_Library_;_companies
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> |
>>>>>>>> +------------------------+---------+-----------+--------+--- --------+--------+-----------------+------------------------ +-----------------------+------------------+-------------+-- ------------------------------------------------------------ ------------------------------------------------------------ ---+
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>> gr. Martin
>>>>>>>>>
>>>>>>>>> On 08/23/2010 01:12 PM, Christophe Bouhier wrote:
>>>>>>>>>> Actually, I think the options you mentioned do have some effect.
>>>>>>>>>> I set them like this now:
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_CONTA INMENT,
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> "ALL");
>>>>>>>>>> props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_C ONTAINMENT,
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> "MERGE,PERSIST,REFRESH");
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> But now, I get another error:
>>>>>>>>>>
>>>>>>>>>> The referred object Company#Huawei is unique, but likely
>>>>>>>>>> referenced by
>>>>>>>>>> various other objects.
>>>>>>>>>>
>>>>>>>>>> *********
>>>>>>>>>>
>>>>>>>>>> Caused by: org.hibernate.NonUniqueObjectException: a different
>>>>>>>>>> object
>>>>>>>>>> with the same identifier value was already associated with the
>>>>>>>>>> session:
>>>>>>>>>> [Company#Huawei]
>>>>>>>>>> at
>>>>>>>>>> org.hibernate.event.def.AbstractSaveEventListener.performSav e(AbstractSaveEventListener.java:191)
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> at
>>>>>>>>>> org.hibernate.event.def.AbstractSaveEventListener.saveWithGe neratedId(AbstractSaveEventListener.java:144)
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> rgds Christophe
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Martin Taal wrote:
>>>>>>>>>>> This seems to be more a EcoreUtil question, so I hope Ed can
>>>>>>>>>>> give
>>>>>>>>>>> some
>>>>>>>>>>> good pointers.
>>>>>>>>>>>
>>>>>>>>>>> gr. Martin
>>>>>>>>>>>
>>>>>>>>>>> On 08/23/2010 12:39 PM, Christophe Bouhier wrote:
>>>>>>>>>>>> Christophe Bouhier wrote:
>>>>>>>>>>>>
>>>>>>>>>>>> Martin, I have the same issue, even with these options.
>>>>>>>>>>>> So it's a copy issue.
>>>>>>>>>>>>
>>>>>>>>>>>> I just don't know how to copy the children of an EObject which
>>>>>>>>>>>> consists
>>>>>>>>>>>> of various EList to a peer EObject while maintaining all
>>>>>>>>>>>> references....
>>>>>>>>>>>>
>>>>>>>>>>>> i.e.
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> original: Lib
>>>>>>>>>>>> - Companies
>>>>>>>>>>>> - Equipments
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> DB: Lib (existing)
>>>>>>>>>>>> - Companies (Copy)
>>>>>>>>>>>> - Equipments (Copy
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> What I do now is
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> dblib.getCompanies().addAll(EcoreUtil.copyAll(srcLib.getComp anies));
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> dblib.getEquipments().addAll(EcoreUtil.copyAll(srcLib.getEqu ipments));
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> So references between equipments and companies get lost it
>>>>>>>>>>>> seems.
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> rgds Christophe
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>> Thanks Martin,
>>>>>>>>>>>>> see inline on some response.
>>>>>>>>>>>>>
>>>>>>>>>>>>> Martin Taal wrote:
>>>>>>>>>>>>>> Hi Christophe,
>>>>>>>>>>>>>> You have to tell Hibernate to cascade the persist operation
>>>>>>>>>>>>>> for
>>>>>>>>>>>>>> the
>>>>>>>>>>>>>> reference on which it fails. Have you set one of these
>>>>>>>>>>>>>> options?
>>>>>>>>>>>>>> teneo.mapping.cascade_policy_on_containment
>>>>>>>>>>>>>> teneo.mapping.cascade_policy_on_non_containment
>>>>>>>>>>>>>>
>>>>>>>>>>>>> No, I haven't. I actually suspect my references are not
>>>>>>>>>>>>> copied,
>>>>>>>>>>>>> as I
>>>>>>>>>>>>> perform a serie of CopyAll(...) actions on various collections
>>>>>>>>>>>>> of my
>>>>>>>>>>>>> model. The references within these collections would not be
>>>>>>>>>>>>> copied..
>>>>>>>>>>>>>
>>>>>>>>>>>>> I will try with these options.
>>>>>>>>>>>>>
>>>>>>>>>>>>> rgds Christophe
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>> if it is a non-containment then the default is that MERGE,
>>>>>>>>>>>>>> PERSIST
>>>>>>>>>>>>>> and REFRESH are set. For containment the default is ALL.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> In general for many-to-one associations Hibernate will work
>>>>>>>>>>>>>> best
>>>>>>>>>>>>>> with
>>>>>>>>>>>>>> minimal cascade settings. This works fine in normal
>>>>>>>>>>>>>> production
>>>>>>>>>>>>>> situations (for example in case a sales order references a
>>>>>>>>>>>>>> currency
>>>>>>>>>>>>>> then one can assume that the currency has already been
>>>>>>>>>>>>>> created in
>>>>>>>>>>>>>> the
>>>>>>>>>>>>>> database). For these large copy action though, this approach
>>>>>>>>>>>>>> does
>>>>>>>>>>>>>> not
>>>>>>>>>>>>>> work well as you are not sure about the order in which
>>>>>>>>>>>>>> data is
>>>>>>>>>>>>>> committed to the database.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> gr. Martin
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> On 08/23/2010 11:52 AM, Christophe Bouhier wrote:
>>>>>>>>>>>>>>> Hi,
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> I am doing a raw copy from one resource to my Teneo
>>>>>>>>>>>>>>> Hibernate
>>>>>>>>>>>>>>> resource.
>>>>>>>>>>>>>>> When saving the hibernate resource, I get the exception
>>>>>>>>>>>>>>> below.
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> What would be the best way to (deep) copy the content of one
>>>>>>>>>>>>>>> resource so
>>>>>>>>>>>>>>> the Hibernate resource and it's references are correctly
>>>>>>>>>>>>>>> filled?
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Now I use this:
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Library dbLib = Util.getDBLibrary();
>>>>>>>>>>>>>>> if (dbLib != null) {
>>>>>>>>>>>>>>> dbLib.getCompanies().addAll(
>>>>>>>>>>>>>>> EcoreUtil.copyAll(srcLib.getCompanies()));
>>>>>>>>>>>>>>> ....
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Caused by: org.hibernate.TransientObjectException: object
>>>>>>>>>>>>>>> references an
>>>>>>>>>>>>>>> unsaved transient instance - save the transient instance
>>>>>>>>>>>>>>> before
>>>>>>>>>>>>>>> flushing: Company
>>>>>>>>>>>>>>> at
>>>>>>>>>>>>>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> at
>>>>>>>>>>>>>>> org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Thanks Christophe
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>
>>>>>
>>>
>>>
>
>
Previous Topic:[CDO] Transactions
Next Topic:xmi id being overwritten by href
Goto Forum:
  


Current Time: Thu Mar 28 17:12:50 GMT 2024

Powered by FUDForum. Page generated in 0.08306 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top