Error: detached entity passed to persist - try to persist complex data (Play-Framework)

1,089 views
Skip to first unread message

Tobias S.

unread,
Nov 3, 2011, 5:53:03 PM11/3/11
to play-framework
Hello,

I really looking forward for an answer for my problem as described at
stackoverflow: http://stackoverflow.com/questions/7986970/

I try to summarize the information:
Model
Shop
- name
- List of addresses

Address
- location
- shop (link to shop)

my form:
- hidden: shop.id=1
- input: shop.name=foo
- List of addresses
- hidden: shop.addresses[0].id=0
- input: shop.addresses[0].location=bar
- submit

The post parameter would look like ?
shop.id=1&shop.name=foo&shop.addresses[0].id=0&shop.addresses[0].location=bar

The controller has just an object shop:

public form(Long id, Shop shop) {
....
shop.save()
}

The data in shop are all there and everythink looks fine, but if I try
to store the data, the error detached entity passed to persist
occurs.

I also tryed shop.merge() but the address data will not be persisted
to the database.

How can I store my data without getting this annoying error?
Every hint would be very helpful.

Guillaume Bort

unread,
Nov 3, 2011, 6:47:20 PM11/3/11
to play-fr...@googlegroups.com
Add a Cascade.ALL setting on your relation.

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

Tobias S.

unread,
Nov 4, 2011, 1:47:39 AM11/4/11
to play-framework
Please see my post at stackoverflow for detail information:
http://stackoverflow.com/questions/7986970/
I already added Cascade.All, or is this not correct there?

-- shop Model

OneToMany(cascade=CascadeType.ALL, mappedBy="shop",
fetch=FetchType.EAGER)
public List<Address> addresses;

Sebastien Cesbron

unread,
Nov 4, 2011, 3:09:10 AM11/4/11
to play-fr...@googlegroups.com
Hi

Before saving you have to reconstruct the relation in the way adress -> shop, something like

for (Address adress : shop.adresses) {
  adress.shop = shop;
}

Regards
Seb

2011/11/4 Tobias S. <tobi....@googlemail.com>

Tobias S.

unread,
Nov 4, 2011, 1:19:16 PM11/4/11
to play-framework
Hi Seb,

it dosn't sovle the problem, the error still occurs. I've got the
feeling that I'm trying to solve that in the wrong way. Maybe there is
a better solution to archive that.

On Nov 4, 8:09 am, Sebastien Cesbron <scesb...@gmail.com> wrote:
> Hi
>
> Before saving you have to reconstruct the relation in the way adress ->
> shop, something like
>
> for (Address adress : shop.adresses) {
>   adress.shop = shop;
>
> }
>
> Regards
> Seb
>
> 2011/11/4 Tobias S. <tobi.con...@googlemail.com>

Guillaume Bort

unread,
Nov 4, 2011, 3:36:18 PM11/4/11
to play-fr...@googlegroups.com, play-framework
Your JPA mapping is wrong. If you want a bi-directional mapping you need to specify which side owns the relation using mappedBy

Tobias S.

unread,
Nov 4, 2011, 7:12:27 PM11/4/11
to play-framework
Hello Guillaume,

I have added mappedBy="shop", see my email post from Nov 4, 6:47 am.
Its not neccessary to have a bi-dirictional mapping. I tried it
without one, but I wasnt successfull at all. At least I tried a bunch
of combinations, but the correct one is still missing.

On Nov 4, 8:36 pm, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Your JPA mapping is wrong. If you want a bi-directional mapping you need to specify which side owns the relation using mappedBy
>

Sebastien Cesbron

unread,
Nov 5, 2011, 5:16:17 PM11/5/11
to play-fr...@googlegroups.com
Hi

Maybe the adresses objects are not correctly attached to the session.

You can try to do

shop = shop.merge();

before

shop.save();

If the problem persist, try to put a breakpoint on the exception and see exactly which entity is being persist when the exception is thrown

Regards
Seb

2011/11/5 Tobias S. <tobi....@googlemail.com>

Tobias S.

unread,
Nov 6, 2011, 3:17:14 PM11/6/11
to play-framework
Hi Sebastien,

the error dissapears, BUT the address data will not be updated :(
I've got the feeling, that merge() doesn't merge my address data.

The strange thing is, that shop = shop.merge(); delivers a object with
the new data,
but then shop.save() just update the shop and address index itself,
but not the address data (location)...

I'm running out of ideas :(

On Nov 5, 10:16 pm, Sebastien Cesbron <scesb...@gmail.com> wrote:
> Hi
>
> Maybe the adresses objects are not correctly attached to the session.
>
> You can try to do
>
> shop = shop.merge();
>
> before
>
> shop.save();
>
> If the problem persist, try to put a breakpoint on the exception and see
> exactly which entity is being persist when the exception is thrown
>
> Regards
> Seb
>
> 2011/11/5 Tobias S. <tobi.con...@googlemail.com>

bryan.w

unread,
Nov 8, 2011, 11:18:11 AM11/8/11
to play-framework
I would be curious to see a solution to this also, I have never been
able to get it working.

Right now, I'm doing something like this: http://pastebin.com/z0ehihT6

Using a separate list for the child objects. Then, if the child
object has an id, load it from the db and re-bind the request
parameters before saving (borrowed this bit from the CRUD module.)

bryan.w

unread,
Nov 8, 2011, 11:24:11 AM11/8/11
to play-framework
Just noticed a bug in my pastbin code - the for loop should start at
zero :-}

Tobias S.

unread,
Nov 9, 2011, 3:49:47 AM11/9/11
to play-framework
Hello Bryan,

thank you for your answer and its good to know that someone is sharing
my problem.
Your code helped me alot and finally I managed to store my data in a
way. Its not perfect,
but its ok.

Please check my stackoverflow question, I've updated it:
http://stackoverflow.com/questions/7986970 (see Update 2). If you
manage to find a solution for that problem, feel free to share it with
me and others :D
Reply all
Reply to author
Forward
0 new messages