Saving a referenced existing entity results in a new entity [message #1239745] |
Tue, 04 February 2014 23:46 |
Ralf Heydenreich 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.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03136 seconds