Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc) » [Teneo] Option cascade_policy_on_non_containment
|
Re: [Teneo] Option cascade_policy_on_non_containment [message #1095940 is a reply to message #1095838] |
Tue, 27 August 2013 18:56 |
Martin Taal Messages: 5468 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 #1096388 is a reply to message #1096382] |
Wed, 28 August 2013 10:16 |
Martin Taal Messages: 5468 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
|
|
| | |
Goto Forum:
Current Time: Thu Sep 19 10:09:24 GMT 2024
Powered by FUDForum. Page generated in 0.16108 seconds
|