(bug?) Play 1.2: call to merge() saving the entity

183 views
Skip to first unread message

Cyrille37

unread,
Apr 15, 2011, 9:19:03 AM4/15/11
to play-framework
Hi,

With Play 1.2

In documentation it's no write that a call to merge() will save new
entity.
http://www.playframework.org/documentation/1.2/jpa#save

Here is the test case :

package models.essais;
import javax.persistence.*;
import play.data.validation.*;
import play.db.jpa.GenericModel;
@Entity
public class Person extends GenericModel {
@Id
@GeneratedValue
public int id ;
@Required
@MinSize(2)
public String name ;
}

package controllers;
import models.essais.Person;
import play.Logger;
import play.mvc.*;
public class Essais02 extends Controller {

public static void index() {

Logger.debug("Person count %d", Person.count());

Person p = new Person();
p.name = "blabla";

p.merge();

render("Essais/essais02.html");
}
}

import play.*;
import play.jobs.*;
@OnApplicationStart
public class Bootstrap extends Job {
@Override
public void doJob() {

Fixtures.loadModels("data-essais.yml");
}
}

And the "data-essais.yml":

essais.Person(p1):
name: Foo

Refresh the page Essais02 and you will see the Person.count()
incrementing.

And this "bug?" is only for just created entity, for existing entity
the merge() works has expected: no change in the db.

The second test case (add this method to controller Essais02):

public static void test2() {

Logger.debug("Person count %d", Person.count());

Person p = Person.all().first();
Logger.debug("first Person name: %s", p.name);

p.name = "blabla";

p.merge();

render("Essais/essais02.html");
}

You can see that for the existed entity the "p.name" never change, the
count never increase. That's what expected.

Cheers
Cyrille.

Guillaume Bort

unread,
Apr 19, 2011, 1:46:50 PM4/19/11
to play-fr...@googlegroups.com
Check the JPA documentation. EntityManager.merge() insert new objects
and update existing ones.

> --
> 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

cyrille giquello

unread,
Apr 23, 2011, 4:42:31 AM4/23/11
to play-fr...@googlegroups.com
2011/4/19 Guillaume Bort <guillau...@gmail.com>:

> Check the JPA documentation. EntityManager.merge() insert new objects
> and update existing ones.

Thanks,
Sorry for my bad JPA knowledge.

I just search how to re-attach detached manyToOne collections objects.

I'll read back the JPA documentation.

Regards
Cyrille.

Reply all
Reply to author
Forward
0 new messages