Detached object and GenericModel.merge() problem

100 views
Skip to first unread message

Cyrille37

unread,
Apr 12, 2011, 5:21:52 AM4/12/11
to play-framework
Hi,
For few days I try to well understand Play & JPA stuff. Not easy for
me ... So I come to you for some help on the pretty simple example:

@Entity
public class ClassA extends GenericModel {
@Id
public int id;
public String code;
}

public class JPAGenericModelTest01 extends UnitTest {
@Test
public void Test01() {

ClassA a = new ClassA();
a.id = 1;
a.code = "abc";
a.save();

JPA.em().flush();
JPA.em().clear();

ClassA b = new ClassA();
b.id = 1 ;
b.code = "def";
b.merge();
b.save();

JPA.em().clear();

ClassA c = ClassA.findById(1);
assertEquals("def", c.code);
}
}

The run of Test01() throw a NonUniqueObjectException at b.save() :
javax.persistence.PersistenceException:
org.hibernate.NonUniqueObjectException: a different object with the
same identifier value was already associated with the session.

If I only do the merge(), don't call b.save() after the merge(), the
change on ClassA.code property is not updated: assertEquals("def",
c.code) failed.

Please, could you give me some light on that case ?

Thanks
Cyrille.

Cyrille37

unread,
Apr 12, 2011, 6:23:10 AM4/12/11
to play-framework

I do not understand because when I use merge() in Controller with
detached object created by the httpBinding, the merge() then save()
process works !

The problem is come with the following simple test...

Cyrille, lost in dark space...

Guillaume Bort

unread,
Apr 12, 2011, 6:43:55 AM4/12/11
to play-fr...@googlegroups.com
This is because your test runs in a single transaction.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.




--
Guillaume Bort, http://guillaume.bort.fr

Cyrille37

unread,
Apr 12, 2011, 6:52:28 AM4/12/11
to play-framework
On 12 avr, 12:43, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> This is because your test runs in a single transaction.

So, is the rule is : only and only one call to save() by entity in the
same transaction ?

Is it a JPA.persist() behavior or GenericModel.save() behavior ?

Thanks
Cyrille.
> Guillaume Bort,http://guillaume.bort.fr
Reply all
Reply to author
Forward
0 new messages