Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » [Teneo] Option cascade_policy_on_non_containment
[Teneo] Option cascade_policy_on_non_containment [message #1095838] Tue, 27 August 2013 11:46 Go to next message
Nahuel V is currently offline Nahuel V
Messages: 4
Registered: August 2013
Junior Member
Hi everyone,

I am working with Teneo/EMF/Hibernate and I've made a simple test.

- I create 2 objects A and B. A has a non-containment relation one-to-many with B (B1).
- This is my test code:


// LOAD A
Session session = this.hbds.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
A loadedA = (A)session.createQuery("FROM A a LEFT JOIN FETCH a.b1").list().get(0);
transaction.commit();
session.flush();
session.close();

// LOAD B
session = this.hbds.getSessionFactory().openSession();
transaction = session.beginTransaction();
B loadedB = (B)session.get("B", (Long)6L);
transaction.commit();
session.flush();
session.close();

// ADD B TO A
session = this.hbds.getSessionFactory().openSession();
transaction = session.beginTransaction();
loadedA.getB1().add(loadedB);
//loadedA.getB1().add(b);
session.saveOrUpdate(loadedA);
transaction.commit();
session.flush();
session.close();

this.hbds.close();



- The following ones are my different configurations for the option CASCADE_POLICY_ON_NON_CONTAINMENT in Teneo and the respective error messages I get:

-- REFRESH, MERGE, PERSIST: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [B#6]

-- REFRESH, MERGE: works, but if I create a new object "b" that is not in the DB and try to add it to A, I get the following error: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: B


Do you know what is the right configuration to work with saveOrUpdate and to avoid these errors? Or any solution to avoid this problem?
Re: [Teneo] Option cascade_policy_on_non_containment [message #1095940 is a reply to message #1095838] Tue, 27 August 2013 14:56 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Hi Nahuel,
To prevent the first exception (another object with...) you should use one session and transaction to load A and B and
then add B to A. That's also more a 'standard' approach, to open one session and transaction, do your thing and then
commit.

The second exception also occurs because of this, because you load B in a different session than A, Hibernate thinks
that B is new and no cascade happens.

gr. Martin

On 08/27/2013 08:01 PM, Nahuel V wrote:
> Hi everyone,
>
> I am working with Teneo/EMF/Hibernate and I've made a simple test.
>
> - I create 2 objects A and B. A has a non-containment relation one-to-many with B (B1).
> - This is my test code:
>
>
>
> // LOAD A
> Session session = this.hbds.getSessionFactory().openSession();
> Transaction transaction = session.beginTransaction();
> A loadedA = (A)session.createQuery("FROM A a LEFT JOIN FETCH a.b1").list().get(0);
> transaction.commit();
> session.flush();
> session.close();
>
> // LOAD B
> session = this.hbds.getSessionFactory().openSession();
> transaction = session.beginTransaction();
> B loadedB = (B)session.get("B", (Long)6L);
> transaction.commit();
> session.flush();
> session.close();
>
> // ADD B TO A
> session = this.hbds.getSessionFactory().openSession();
> transaction = session.beginTransaction();
> loadedA.getB1().add(loadedB);
> //loadedA.getB1().add(b);
> session.saveOrUpdate(loadedA);
> transaction.commit();
> session.flush();
> session.close();
>
> this.hbds.close();
>
>
>
> - The following ones are my different configurations for the option CASCADE_POLICY_ON_NON_CONTAINMENT in Teneo and the
> respective error messages I get:
>
> -- REFRESH, MERGE, PERSIST: org.hibernate.NonUniqueObjectException: a different object with the same identifier
> value was already associated with the session: [B#6]
>
> -- REFRESH, MERGE: works, but if I create a new object "b" that is not in the DB and try to add it to A, I get the
> following error: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the
> transient instance before flushing: B
>
>
> Do you know what is the right configuration to work with saveOrUpdate and to avoid these errors? Or any solution to
> avoid this problem?
>


--

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@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [Teneo] Option cascade_policy_on_non_containment [message #1096382 is a reply to message #1095940] Wed, 28 August 2013 06:08 Go to previous messageGo to next message
Nahuel V is currently offline Nahuel V
Messages: 4
Registered: August 2013
Junior Member
Hi Martin,

First thank you for your help. I understand this problem, but i have another question..

I am working with a web application where I need to edit an object. For example this case:

1. I retrieve an object A from the DB to edit it, with its respective list of B objects. (loaded in a session)

Some stuff happens in between, e.g. user interaction.

2. I load a list of objects of type B in a combo box that could be associated to the object A, then I can create a new B object. (this objects are loaded in a different session)

Some stuff happens in between, e.g. user interaction.

3. I add a new B object to A, and I save the object A. (this save is made in a different session)

The thing is that I cannot load A and B in the same session because B is loaded after doing some stuff so there is some time between the 2 processes. This works if I have only REFRESH, MERGE in my teneo configuration. But when I need to create a new B and add it to A it's raised the TransientObjectException. What is the right way to manage this situation?

Thank you in advance.
Re: [Teneo] Option cascade_policy_on_non_containment [message #1096388 is a reply to message #1096382] Wed, 28 August 2013 06:16 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Hi Nahuel,
For web application I tend to read all the information again with every request. In my cases (and I think many cases)
this is fast enough. I am a big fan of the session in view pattern.
https://community.jboss.org/wiki/OpenSessionInView

So recreate the session and transaction at every request and commit/rollback using a http request filter.

Another option is to keep the hibernate session alive between requests and re-use, cache it in the http session for
example. However you have to be somewhat carefull with multi-threading and memory usage (as the hibernate session keeps
his own cache).

Btw, if you use EMF in a web environment it could be better to look at the Texo project, I specifically did that project
for model-driven development in web environments:
http://wiki.eclipse.org/Texo

gr. Martin


On 08/28/2013 12:08 PM, Nahuel V wrote:
> Hi Martin,
>
> First thank you for your help. I understand this problem, but i have another question..
>
> I am working with a web application where I need to edit an object. For example this case:
>
> 1. I retrieve an object A from the DB to edit it, with its respective list of B objects. (loaded in a session)
>
> Some stuff happens in between, e.g. user interaction.
>
> 2. I load a list of objects of type B in a combo box that could be associated to the object A, then I can create a new B
> object. (this objects are loaded in a different session)
>
> Some stuff happens in between, e.g. user interaction.
>
> 3. I add a new B object to A, and I save the object A. (this save is made in a different session)
>
> The thing is that I cannot load A and B in the same session because B is loaded after doing some stuff so there is some
> time between the 2 processes. This works if I have only REFRESH, MERGE in my teneo configuration. But when I need to
> create a new B and add it to A it's raised the TransientObjectException. What is the right way to manage this situation?
> Thank you in advance.


--

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@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [Teneo] Option cascade_policy_on_non_containment [message #1096426 is a reply to message #1096388] Wed, 28 August 2013 07:30 Go to previous messageGo to next message
Nahuel V is currently offline Nahuel V
Messages: 4
Registered: August 2013
Junior Member
Hi Martin,

I think that this solution might not fit my case where I am building a RAP application instead of one using servlets. Do you have any advice for my parituclar case?

Kind regards.
Re: [Teneo] Option cascade_policy_on_non_containment [message #1096488 is a reply to message #1096426] Wed, 28 August 2013 09:05 Go to previous message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Hmm, no other suggestion I am afraid, so either you should store the session in the http session/user level or reread
the data at every request. I don't know RAP so I can't give a lot of tips here I am afraid...

gr. Martin

On 08/28/2013 01:30 PM, Nahuel V wrote:
> Hi Martin,
>
> I think that this solution might not fit my case where I am building a RAP application instead of one using servlets. Do
> you have any advice for my parituclar case?
>
> Kind regards.
>


--

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@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Previous Topic:[CDO 4.2] Server cannot find MysqlDataSource
Next Topic:[Texo] Getting started, JEE/JTA-Transactions & Persistence
Goto Forum:
  


Current Time: Fri Apr 18 16:45:47 EDT 2014

Powered by FUDForum. Page generated in 0.01765 seconds