OneToMany 関係で子を持つエンティティを永続化するときに問題が発生します。ここに私の2つのエンティティ:
@Entity
@Table(name="USER")
public class User implements Serializable {
...
@Id
@Column(name="ID_USER")
private String idUser;
@OneToMany(mappedBy="user", cascade=CascadeType.PERSIST)
private List<Address> address;
public List<Address> getAddress() {
return this.address;
}
public void setAddress(List<Address> address) {
this.address = address;
}
public Address addAddress(Address address) {
getAddress().add(address);
address.setUser(this);
return address;
}
public Address removeAddress(Address address) {
getAddress().remove(address);
address.setUser(null);
return address;
}
...
}
@Entity
@Table(name="ADDRESS")
public class Address implements Serializable {
...
@EmbeddedId
private AddressPK id;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name="ID_USER", insertable=false, updatable=false, nullable=false)
private User user;
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
}
@Embeddable
public class AddressPK implements Serializable {
...
@Column(name="ID_USER", insertable=false, updatable=false)
private String idUser;
...
}
そして、ORA-02291 エラーを生成する永続操作:
User u = new User();
u.setAddress(new ArrayList<Address>());
u.set...
Address a1 = new Address();
a1.set...
u.addAddress(a1);
Address a2 = new Address();
a2.set...
u.addAddress(a2);
entityManager.persist(u);
この問題を解決するアイデアはありますか?
みんなありがとう!