Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Saving a referenced existing entity results in a new entity
Saving a referenced existing entity results in a new entity [message #1239745] Tue, 04 February 2014 23:46 Go to next message
Ralf Heydenreich is currently offline Ralf HeydenreichFriend
Messages: 235
Registered: July 2009
Senior Member
Hi all,
I've the following entities:

@Entity()
public class Contact {
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH,
CascadeType.PERSIST })
private Address address = null;
private String fooBar;
}

@Entity()
public class Address {
private Long id = null;
private String street = null;
private String cityAddon = null;
}

@Entity()
public class Document {
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.REFRESH, CascadeType.DETACH })
private Contact contact = null;
private String baz;
}

At first I save some Contacts in my DB, which works good. Then I create
some Documents. I will use the existing Contacts for them. Therefore at
first I select the Contacts from DB:

....
Contact selectedContact = em.findById(givenId);
Document newDoc = new Document();
newDoc.setContact(selectedContact);
em.save(newDoc);
....

This results in a newly created Document (which is correct :-) ) *and* a
new Contact (and a new Address, which results from the new Contact)! I
really don't know why. Any ideas?

TIA,
Ralf.
Re: Saving a referenced existing entity results in a new entity [message #1240438 is a reply to message #1239745] Thu, 06 February 2014 13:09 Go to previous messageGo to next message
Lukas JungmannFriend
Messages: 36
Registered: November 2013
Location: Prague, Czech Republic
Member
Hi,

it looks like a call to em.findById is done out of a transaction and therefore returned entity is in detached state. Now when you try to store new document, and that should be done within some transaction, EM thinks, that Contact is new since it lost track of it. So you should wrap your code with sth like
em.getTransaction().begin();....em.getTransaction().commit();
to get it working as expected.

HTH,
--lukas
Re: Saving a referenced existing entity results in a new entity [message #1244028 is a reply to message #1240438] Tue, 11 February 2014 20:25 Go to previous message
Ralf Heydenreich is currently offline Ralf HeydenreichFriend
Messages: 235
Registered: July 2009
Senior Member
Am 06.02.2014 14:09, schrieb Lukas Jungmann:
> Hi,
>
> it looks like a call to em.findById is done out of a transaction and
> therefore returned entity is in detached state. Now when you try to
> store new document, and that should be done within some transaction, EM
> thinks, that Contact is new since it lost track of it. So you should
> wrap your code with sth like
> em.getTransaction().begin();....em.getTransaction().commit(); to get it
> working as expected.
>
> HTH,
> --lukas


Hm. This is a little complicated since I'm using Eclipse Gemini (with
EclipseLink) as persistence layer. Maybe I've to ask this question there.

Regards,
Ralf.
Previous Topic:NOT NULL FK nullification problem
Next Topic:[SOLVED] For each call a new connection? Need only one when using RCP with Gemini or Swing
Goto Forum:
  


Current Time: Tue Apr 23 09:54:31 GMT 2024

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

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

Back to the top