Call .save() in PostPersist method

675 views
Skip to first unread message

Stephane Piette

unread,
Jul 12, 2011, 6:42:24 AM7/12/11
to play-framework
Hi,
I'm trying to automatically create a new object each time another
object is created. I decided to use @PostPersist annotation, here is
my code :

public class AdviceSheet extends GenericModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int id;
public String title;
public String city;

@PostPersist
public void postPersist() {
System.out.println(this.description);
new City(this.city).save();
}
}

public class City extends GenericModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int id;
public String name;

public City(String name){
this.name = name;
}
}



But with this code, if I try to create an AdviceSheet, I got an
exception :
play.exceptions.JavaExecutionException: null id in models.AdviceSheet
entry (don't flush the Session after an exception occurs)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:228)
at Invocation.HTTP Request(Play!)
....


What's the problem ?
Is there any workaround ?
Thanks

Manuel Bernhardt

unread,
Jul 12, 2011, 9:34:37 AM7/12/11
to play-fr...@googlegroups.com
see http://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/listeners.html:
"A callback method must not invoke EntityManager or Query methods!" -
i.e., @PostPersist was apparently not designed for the purpose of
calling persistance operations.

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

Manuel Bernhardt

unread,
Jul 12, 2011, 9:36:01 AM7/12/11
to play-fr...@googlegroups.com
About a workaround: I resorted to overriding the save() and create()
methods of the entity in which I want to do something after
persistance and performing additional operations in there.

Stephane Piette

unread,
Jul 12, 2011, 10:35:03 AM7/12/11
to play-framework
Ok the answer is clear !
Thanks, I'll do like you

On 12 juil, 15:36, Manuel Bernhardt <bernhardt.man...@gmail.com>
wrote:
> About a workaround: I resorted to overriding the save() and create()
> methods of the entity in which I want to do something after
> persistance and performing additional operations in there.
>
> On Tue, Jul 12, 2011 at 3:34 PM, Manuel Bernhardt
>
>
>
>
>
>
>
> <bernhardt.man...@gmail.com> wrote:
> > seehttp://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/l...
Reply all
Reply to author
Forward
0 new messages