我正在尝试进行以下交易:
问题:
如果我使用nativequeries而不是EntityManager中的persist和merge方法,一切正常
@Stateless
public class Test implements TestLocal {
@PersistenceContext(unitName = "testPU")
private EntityManager em;
@Override
public void testMethod(Client client, Address address, boolean mainAddress) {
client.setIdcliente(new Long(110));
em.persist(client);
address.setAddressPK(new AddressPK(new Long(120), client.getIdcliente()));
em.persist(address);
if (mainAddress) {
client.setMainAddress(address);
em.merge(client);
}
}
我的模特:
客户:
- IDCLIENTE pk
- NMORADAPRI可以为空的
FOREIGN KEY(IDCLIENTE,NMORADAPRI)参考地址(IDCLIENTE,IDMORADA)
@Entity
@Table(name = "CLIENTS")
public class Client implements Serializable {
@Id
@Basic(optional = false)
@NotNull
@Column(name = "IDCLIENTE")
private Long idcliente;
@JoinColumns({
@JoinColumn(name = "IDCLIENTE", referencedColumnName = "IDCLIENTE", insertable = false, updatable = false),
@JoinColumn(name = "NMORADAPRI", referencedColumnName = "IDMORADA")})
@OneToOne(optional = false)
private Address main_address;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "client")
private Collection<Address> addressesCollection;
ADDRESSES:
- IDMORADA pk
- IDCLIENTE pk
FOREIGN KEY(IDCLIENTE)参考客户(IDCLIENTE)
@Entity
@Table(name = "ADDRESSES")
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected AddressPK addressPK;
@JoinColumn(name = "IDCLIENTE", referencedColumnName = "IDCLIENTE", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Client client;
撰写密钥
@Embeddable
public class AddressPK implements Serializable {
@Basic(optional = false)
@NotNull
@Column(name = "IDMORADA")
private long idmorada;
@Basic(optional = false)
@NotNull
@Column(name = "IDCLIENTE")
private long idcliente;