[JPA] @ElementCollection + em.persist() = KO

880 views
Skip to first unread message

laguiz

unread,
Apr 7, 2011, 3:19:09 PM4/7/11
to play-framework
Hi guys,

I'm trying to use @ElementCollection in an Entity with a new
EntityManager but values mapped with @ElementCollection are not saved.
This is what I do :

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@Entity
public class MyEntity extends Model {

//.....

@ElementCollection
@org.hibernate.annotations.Cascade(value =
org.hibernate.annotations.CascadeType.ALL)
public Set<String> totos = new HashSet<String>();

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

IF I DO :

myEntity.save()

THEN

myEntity is saved and Strings in *totos* are saved too

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

IF I DO :

EntityManager entityManage= JPA.newEntityManager();
entityManage.getTransaction().begin();
entityManage.persist(myEntity)

THEN

myEntity is saved BUT Strings in *totos* are NOT saved

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


I tried with and without @Cascade from javax and Hibernate (normally
we should have to use @Cascade annotation since @ElementCollection
already cascade).


Do you have idea why entityManager does't not cascade
@ElementCollection ?


I read this on Hibernate forum :
https://forum.hibernate.org/viewtopic.php?f=1&t=1009870
But I'm not able to find the reason.

Do you have any advice?

Thank you,
laguiz

laguiz

unread,
Apr 7, 2011, 3:22:13 PM4/7/11
to play-framework
Correction :
"I tried with and without @Cascade from javax and Hibernate (normally
we should NOT have to use @Cascade annotation since @ElementCollection
already cascade)."

Guillaume Bort

unread,
Apr 8, 2011, 2:53:51 AM4/8/11
to play-fr...@googlegroups.com
What if you don't inherit from Model?

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

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

laguiz

unread,
Apr 9, 2011, 12:33:23 PM4/9/11
to play-framework
I will try and let you know tomorrow. Cheers.
> Guillaume Bort,http://guillaume.bort.fr
>
> For anything work-related, use g...@zenexity.fr; for everything else,
> write guillaume.b...@gmail.com

laguiz

unread,
Apr 10, 2011, 6:53:51 AM4/10/11
to play-framework
Hi Guillaume,

I tried without inherit from Model and it works well.

Is it an expected behavior? I guess not.

Let me know if I should create an issue with sample code that
reproduce the issue.

Thank you,

Guillaume Bort

unread,
Apr 10, 2011, 1:39:34 PM4/10/11
to play-fr...@googlegroups.com
> I tried without inherit from Model and it works well.
>
> Is it an expected behavior? I guess not.

Well, it's not really expected, but it is a side effect of offering
explicit saving for entities extending Model.
http://www.playframework.org/documentation/1.1.1/jpa#save

For entities extending Model you should use the Model API (including
save). If you want to do plain old JPA, then it's better to not
inherit Model.

laguiz

unread,
Apr 10, 2011, 3:18:54 PM4/10/11
to play-framework
Ok for this case I can use plain jpa. I will try to wrapp the enum in
an entity.

Thank you

On Apr 10, 7:39 pm, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> > I tried without inherit from Model and it works well.
>
> > Is it an expected behavior? I guess not.
>
> Well, it's not really expected, but it is a side effect of offering
> explicit saving for entities extending Model.http://www.playframework.org/documentation/1.1.1/jpa#save

laguiz

unread,
Apr 13, 2011, 1:35:20 PM4/13/11
to play-framework
(sorry for my last message - it is not very clear - but I responded
with my phone walking in the street and drinking a cafe ^^)

So conclusion is :

1 - If we remove the inheritance to Model, @ElementCollection will
cascade using EntityManager directly.
2 - We can wrap the type in entity that inherit Model and then create
a collection of it using traditional @OneToMany(cascade =
CascadeType.ALL, mappedBy = "xxx")

Despite the advice of Guillaume I preferred the second solution to
have useful method from Model. In fact in my case I need to use the
entity manager only at a particular use case that is specific (to
ensure transaction independence). In all others cases, I'm happy with
the current Hibernate session created by Play so I preferred to keep
my entity managed this way.

@Guillaume :
Let me know if you think that the second solution is a bad idea :)

Thanks again,
Enjoy,




On Apr 10, 9:18 pm, laguiz <war...@gmail.com> wrote:
> Ok for this case I can use plainjpa. I will try to wrapp theenumin
> an entity.
>
> Thank you
>
> On Apr 10, 7:39 pm, Guillaume Bort <guillaume.b...@gmail.com> wrote:
>
>
>
>
>
>
>
> > > I tried without inherit from Model and it works well.
>
> > > Is it an expected behavior? I guess not.
>
> > Well, it's not really expected, but it is a side effect of offering
> > explicit saving for entities extending Model.http://www.playframework.org/documentation/1.1.1/jpa#save
>
> > For entities extending Model you should use the Model API (including
> > save). If you want to do plain oldJPA, then it's better to not
Reply all
Reply to author
Forward
0 new messages