Bump.
We are encountering the same issue, which is creating a real problem.
Environment is openjpa-1.2.3-SNAPSHOT. See below for the mappings, which
I've converted to the example in this thread. In our case is it a full
bidirectional 2-way not 1.5 way relationship.
On persist, I get the message :
Attempt to set column "PHONE.addressId" to two different values:
(null)"null", (class java.math.BigDecimal)"129" This can occur when you fail
to set both sides of a two-sided relation between objects, or when you map
different fields to the same column, but you do not keep the values of these
fields in synch".
It appears that the 2 times in question are when the persist is first
cascaded to a phone (PHONE.addressId is null), and later when the Address'
generated sequence number (ie 129) is populated into the phone.
The remedies offered in this thread are not availing.
A) set the phone FK in the @PostPersist of Address. The same error occurs.
B) use @Basic and @ForeignKey(implicit=true).
This is not available in 1.2.3-SNAPSHOT. In any case, this seems like a
bandaid - JPA should do the Right Thing, when the a full and proper
bidrectional mapping is defined. The addressId isn't available until after
the Address is inserted, so JPA would need to be tolerant of the generated
key being set into the child entity subsequent to when it is persisted.
C) remove the field addressId from Phone
addressId is not nullable, so this causes an error (DB2 SQLCODE 407) because
the addressId is omitted from the insert.
I dont know if the picture has changed since this thread started, but there
does not seem to be any way around it, openjpa wont seem to support a
cascading persist from parent to bidirectional child, with generated
sequence numbers.
At present, this is a big showstopper for us. In our real model we have
dozens of such relationships, and persisting it piecemeal would be too
burdensome. We have to use sequence objects for keys. We are probably not at
liberty to change the version of openJPA right now. (The environment is
Rapid Applicaton Developer 7.5.5, (a rebranded, prepackaged Eclipse)).
Any advice would be greatly appreciated.
public class Address {
...
@Column
@GeneratedValue
// persistence.xml contains
// <property name="openjpa.Sequence"
value="native(sequence=NAME_OF_DB2_SEQ_OBJ)"/>
@Id
BigDecimal addressId;
@OneToMany(mappedBy="address",cascade={CascadeType.ALL})
private List<Phone> phoneList = new ArrayList<Phone>();
@PostPersist
// this does not work, same error
public void populatePhoneFKs(){
BigDecimal addressId = getAddressId();
System.out.println("populating Phone FKs with " + fk);
for (Phone phone : getPhoneList()) {
phone.setAddressId(addressId);}}
...
// Accessors for all fields
}
public class Phone {
...
@Column(name="addressId")
private BigDecimal addressId;
@ManyToOne
@JoinColumn(name="addressId")
private Address address;
...
// Accessors for all fields
}
--
View this message in context:
http://openjpa.208410.n2.nabble.com/OpenJPA-two-sided-relation-between-objects-Issue-tp687050p5730843.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.