Ejb3 CMT没有提交外键约束

时间:2012-10-12 10:18:49

标签: transactions foreign-keys ejb-3.0

我正在尝试进行以下交易:

  1. 创建客户端
  2. 使用客户表的外键创建一个地址
  3. 将客户的组合外键更新为地址表
  4. 问题:

    1. 运行testMethod()时,我得到 java.sql.SQLIntegrityConstraintViolationException 关于ADDRESSES的约束
    2. 如果我在一个事务中更改代码并创建客户端,然后在另一个事务中创建地址并更新客户端字段,一切正常
    3. 如果我更改代码并在一个事务中创建客户端和地址并在另一个事务中更新客户端字段,那么一切正常
    4. 如果我使用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);
        }
      }
      
    5. 我的模特:

      客户:
        - 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;
      

0 个答案:

没有答案