Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Cascade.Persist problems
Cascade.Persist problems [message #507809] Thu, 14 January 2010 17:57 Go to next message
Matthew Pocock is currently offline Matthew PocockFriend
Messages: 18
Registered: January 2010
Junior Member

I have two classes: Foo and Pair.

@Table(uniqueConstraints = @UniqueConstraint(columnNames = "fooName"))
public class Foo
@GeneratedValue(strategy= GenerationType.SEQUENCE)
private Integer id;
private String fooName;


@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"foo_id", "foo2_id"})})
public class Pair
implements Serializable
@GeneratedValue(strategy= GenerationType.SEQUENCE)
private Integer pairID;
@ManyToOne(fetch = FetchType.EAGER)
private Foo foo1;
@ManyToOne(fetch = FetchType.EAGER)
private Foo foo2;


Now, if I call persist(pair), I get an exception if one of the Foo instances have not already been persisted. It works fine if they have already been persisted.

If I add a cascade annotation for PERSIST, then I get an exception if one of the Foo instances has already been persisted (duplicate primary key). If neither have, it works fine. This is because it is attempting to insert a Foo (id and fooName) that already is in the database as it's already been persisted.

I am at a bit of a loss. In my application it is not obvious which instances of Pair may or may not contain persisted instances of Foo. I was hoping that there would be a 'cascade if needed' option that deals with this use-case, but I can't find it.


Re: Cascade.Persist problems [message #508030 is a reply to message #507809] Fri, 15 January 2010 15:44 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1300
Registered: July 2009
Senior Member

The problem is that you are using detached entities. Calilng persist on a detached entity is not allowed and results in the exception. When it the cascade option is missing and pair references an existing Foo, EclipseLink will use the primary key to populate the foreign key in Pair, but that is all - no changes in Foo will get picked up. When Foo is new, you get an exception because it tries to set the foreign key to a value that doesn't exist in Foo.

Setting the cascade option has the opposite effect because (I believe anyway) persist is specific to inserts and it is an app error to try to insert an existing object.

What you can use instead is Merge, which will operate as you expected persist to. Merge will determine at each level if the object is existing or new, so it will work the same for Pair. If you have cascade merge set, it will then cascade to Foo and do the same - when it encounters an existing but detached foo though, it will merge it and any changes into the existing copy of the Foo object instead of throwing the exception. The major application difference between merge and persist that you will have to account for though is that Merge returns the managed object, where as persist causes the object passed into become managed. This means that any changes made afterward will need to be made on the object tree returned from Merge - changes made to the object passed into it will not get picked up like they would if you continue to use the object passsed into persist.

Hope this helps.
Best Regards,
Re: Cascade.Persist problems [message #508111 is a reply to message #508030] Sat, 16 January 2010 00:23 Go to previous message
Matthew Pocock is currently offline Matthew PocockFriend
Messages: 18
Registered: January 2010
Junior Member
Ah, perhaps I'm seeing this because I have my transaction boundaries wrong. Now that I'm doing all these updates in a single transaction, I'm not seeing the same errors. So perhaps before the Foo instances where, as you say, becoming disconnected.
Previous Topic:ReadAllQuery not working for in-memory db
Next Topic:Unexpected entity version number
Goto Forum:

Current Time: Tue Jun 19 22:31:20 GMT 2018

Powered by FUDForum. Page generated in 0.02179 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top