使用@IdClass与Hibernate和arquillian强制CascadeType.Insert在一个ManyToOne外键对象上,该外键对象是实体复合主键的一部分

时间:2017-05-22 23:48:20

标签: java hibernate jpa jpa-2.0

我试图从我的arquillian测试类中继承一个新的Order对象。 订单实体包含OrderDetail实体的列表,该实体具有4个字段的组合主键。

但是当Hibernate尝试插入新的OrderDetail对象时,它还会插入 NOT NEW Item对象(Item是OrderDetail实体中的主要PK字段之一,并使用@ID注释),以及这会导致数据库中Item项的唯一约束异常。

请注意,我从Item @OneToMany注释中删除了CascadeType 并尝试了CascadeType.Refresh但我得到了相同的结果,没有任何改变。它可以在所有情况下强制使用CascadeType.Insert。

当我使用 @EmbeddedId 注释而不是 @IDClass 时, 工作符合预期 但我想知道为什么它不能与@IDClass一起使用?它是Hibernate中的一个错误吗?它应该按照JPA 2.1 specification完美地工作。

订单实体

@Entity
@Table(name = "ORDERS")
public class Orders extends BaseEntity {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ORDERNO")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long orderNo ;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "order", fetch = FetchType.LAZY)
    private List<OrderDetail> orderDetailsList ;

    // setters and getters
}

OrderDetail实体

@Entity
@Table(name = "ORDERDETAILS")
@IdClass(OrderDetailsPK.class)
public class OrderDetail extends BaseEntity {

private static final long serialVersionUID = 1L;

@Id
@JoinColumn(name = "ORDERNO", referencedColumnName = "ORDERNO")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Orders order;

@Id
@JoinColumn( name = "ITEMNO", referencedColumnName = "ITEMNO", insertable = false, updatable = false)
@ManyToOne(cascade = CascadeType.REFRESH,optional = false, fetch = FetchType.LAZY)
//@Cascade({CascadeType.DETACH}) // I tried this but did not work, it keep try to insert the item.
private Item item;

@Id
@Column(name = "LINETYPE")
private String lineType;

@Id
@Column(name = "PROMITEMNO")
private String promItemNo;

项目实体

@Entity
@Table(name = "ITEM")
public class Item extends BaseEntity {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ITEMNO")
    private String itemno;
    // setters and getters

   @Override
   public int hashCode() {
    int hash = 0;
    hash += (itemno != null ? itemno.hashCode() : 0);
    return hash;
   }

   @Override
   public boolean equals(Object object) {
    if (!(object instanceof Item)) {
        return false;
    }
    Item other = (Item) object;
    System.out.println("Item Equal Method = " + !((this.itemno == null &&        other.itemno != null) || (this.itemno != null && !this.itemno.equals(other.itemno))));
    return !((this.itemno == null && other.itemno != null) || (this.itemno != null && !this.itemno.equals(other.itemno)));
}

  @Override
  public String toString() {
    return "com.unilever.sas.model.entities.masterdata.Item[ itemno=" + itemno + " ]";
}

}

OrderDetailsPK

public class OrderDetailsPK  implements Serializable 
{

    private static final long serialVersionUID = 1L;

    private Long order;
    private String item;
    private String lineType;
    private String promItemNo;

    public OrderDetailsPK() {
    }

    public OrderDetailsPK(Long order, String item, String lineType, String promItemNo) {
        this.order = order;
        this.item = item;
        this.lineType = lineType;
        this.promItemNo = promItemNo;
    }

    public Long getOrder() {
        return order;
    }

    public String getItem() {
        return item;
    }

    public String getLineType() {
        return lineType;
    }


    public String getPromItemNo() {
        return promItemNo;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 73 * hash + Objects.hashCode(this.order);
        hash = 73 * hash + Objects.hashCode(this.item);
        hash = 73 * hash + Objects.hashCode(this.lineType);
        hash = 73 * hash + Objects.hashCode(this.promItemNo);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final OrderDetailsPK other = (OrderDetailsPK) obj;
        if (!Objects.equals(this.item, other.item)) {
            return false;
        }
        if (!Objects.equals(this.lineType, other.lineType)) {
            return false;
        }
        if (!Objects.equals(this.promItemNo, other.promItemNo)) {
            return false;
        }
        if (!Objects.equals(this.order, other.order)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "OrderDetailsPK{" + "order=" + order + ", item=" + item + ", lineType=" + lineType + ", promItemNo=" + promItemNo + '}';
    }

Arquillian测试

@RunWith(Arquillian.class)
public class OrdersFacadeTest
{


@Inject
OrdersFacadeLocal ordersFacade ;

@Inject    
ItemFacadeLocal itemFacade ;

@Test
public void testCreateOrder() throws Exception 
{
    Item item = itemFacade.find("20247912");
    Orders order = new Orders();

    OrderDetail orderDetail = new OrderDetail();
    orderDetail.setOrder(order);
    orderDetail.setItem(item);
    orderDetail.setLineType("NLI");
    orderDetail.setPromItemNo("99999");

    List<OrderDetail> orderDeialsList = new ArrayList<>();
    orderDeialsList.add(orderDetail);

    order.setOrderDetailsList(orderDeialsList);      

    ordersFacade.create(order);

    Orders createdOrder = ordersFacade.find(order.getOrderNo());
    assertEquals(order, createdOrder);
}

1 个答案:

答案 0 :(得分:0)

这可能是一个错误,但这里也有一些奇怪的映射选择。

为什么@JoinColumn不允许在插入或更新时设置FK?在这种情况下谁应该设置FK?

@JoinColumn( 
    name = "ITEMNO", 
    referencedColumnName = "ITEMNO", 
    insertable = false, updatable = false
)

如果您可以使用test case复制它,那么您应该打开一个Jira问题。