本文介绍了JPA EclipseLink oneToMany派生ID失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 异常说明:无效的组合主键规范。主键类[entitys.OrderItemPK]中的主键字段或属性的名称以及实体bean类[class entitys.OrderItem]中的主键字段或属性的名称必须相对应,且其类型必须相同。另外,请确保已为XML中的相应属性指定了ID元素,并在实体类的相应字段或属性中指定了@Id。 注意:我正在使用EclipseLink和MySQL DB 实体: $ b @Entity public class CustomerOrder implements Serializable { private static最后一次serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(可选= false) @Column(name =idOrder)私人整数idOrder ; @Basic(可选= false) @Column(name =orderText) private String orderText; @OneToMany(cascade = CascadeType.ALL,mappedBy =customerOrder)私人收藏< OrderItem> orderItemCollection; $ b $ public CustomerOrder(){} } @Entity @IdClass(OrderItemPK.class) public class OrderItem implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId 保护OrderItemPK orderItemPK; @Basic(可选= false) @Column(name =itemDesc) private String itemDesc; $ b $ @ @ @ManyToOne(可选= false) @JoinColumns({ @JoinColumn(name =idOrder,referencedColumnName =idOrder), @JoinColumn(name =ItemNumber,referencedColumnName =ItemNumber)}) // @ JoinColumn(name =idOrder,referencedColumnName =idOrder,insertable = false,updatable = false) private CustomerOrder customerOrder; 私人CustomerOrder customerOrder; public OrderItem(){ this.orderItemPK = new OrderItemPK(); @Embeddable public class OrderItemPK implements Serializable { @Basic(optional = false) @Column(name =idOrder) private int idOrder; @Basic(可选= false) @Column(name =itemNumber) private int itemNumber; 公共OrderItemPK(){} } public static void main(String [] args){ factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); EntityManager em = factory.createEntityManager(); em.getTransaction()。begin(); //填写物品档案 OrderItem item = new OrderItem(); item.setItemDesc(Item Text); //填写订单字段 CustomerOrder order = new CustomerOrder(); order.setOrderText(Order text); //填充关系字段 order.getOrderItemCollection()。add(item); item.setCustomerOrder(order); em.persist(order); em.getTransaction()。commit(); } 我不知道我做错了什么,欢迎提供任何建议。 解决方案那么我设法解决这个问题,通过删除idClass注释和添加@MapId $ $ b $ $ $ $ $ $ $ $ $ @Table(name =Orders)b $ b 继承人的最终代码: @Entity @Table public class CustomerOrder implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(可选= false) @Column(name =idOrder)私人整数idOrder ; @Basic(可选= false) @Column(name =orderText) private String orderText; @OneToMany(mappedBy =customerOrder,cascade = CascadeType.ALL) private Collection< OrderItem> orderItemCollection; $ b public CustomerOrder(){} @Entity @Table(name =OrdersItems) public class OrderItem实现Serializable { private static final long serialVersionUID = 1L; @EmbeddedId 保护OrderItemPK orderItemPK; @Basic(可选= false) @Column(name =itemDesc) private String itemDesc; @MapsId(idOrder) @ManyToOne(可选= false) @JoinColumn(name =idOrder,referencedColumnName =idOrder,nullable = false)私人CustomerOrder customerOrder; public OrderItem(){ this.orderItemPK = new OrderItemPK(); $ b @Embeddable public class OrderItemPK implements Serializable { @Basic(optional = false) @Column(name =idOrder) private int idOrder; @Basic(可选= false) @Column(name =itemNumber) private int itemNumber; $ b公共OrderItemPK(){} public static void main(String [] args){ $ b $ factory = Persistence.createEntityManagerFactory (PERSISTENCE_UNIT_NAME); EntityManager em = factory.createEntityManager(); em.getTransaction()。begin(); //填写订单商品 OrderItem item = new OrderItem(); item.getOrderItemPK()。setItemNumber(1); item.setItemDesc(Product Name); //填写订单 CustomerOrder order = new CustomerOrder(); order.setOrderText(Testing); //创建关系 order.getOrderItemCollection()。add(item); item.setCustomerOrder(order); //坚持 em.persist(order); em.getTransaction()。commit(); $ / code> 但是我不能使用IdClass,这里是源代码 p> @Embeddable public class OrderItemPK implements Serializable { @Basic(optional = false) @Column(name =idOrder) private Integer idOrder; @Basic(可选= false) @Column(name =ItemNumber) private Integer itemNumber; $ b公共OrderItemPK(){} @实体 @IdClass(OrderItemPK.class)公共类OrderItem实现Serializable { private static final long serialVersionUID = 1L; @Id @Column(name =itemNumber) private Integer itemNumber; @Basic(可选= false) @Column(name =itemDesc) private String itemDesc; @MapsId(idOrder) @ManyToOne(可选= false) @JoinColumn(name =idOrder,referencedColumnName =idOrder,nullable = false)私人CustomerOrder customerOrder; $ b公共OrderItem(){} @实体公共类CustomerOrder实现Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(可选= false) @Column(name =idOrder)私人整数idOrder ; @Basic(可选= false) @Column(name =OrderText) private String orderText; @OneToMany(mappedBy =customerOrder,cascade = CascadeType.ALL) private Collection< OrderItem> orderItemCollection; public CustomerOrder(){ this.orderItemCollection = new ArrayList(); } Hello I'm trying to make an example of persistence of a OneToMany relationship in which I get the following error:Exception Description: Invalid composite primary key specification. The names of the primary key fields or properties in the primary key class [entitys.OrderItemPK] and those of the entity bean class [class entitys.OrderItem] must correspond and their types must be the same. Also, ensure that you have specified ID elements for the corresponding attributes in XML and/or an @Id on the corresponding fields or properties of the entity class.Note: I'm using EclipseLink and MySQL DBThe entities:@Entitypublic class CustomerOrder implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "idOrder") private Integer idOrder; @Basic(optional = false) @Column(name = "orderText") private String orderText; @OneToMany(cascade = CascadeType.ALL, mappedBy = "customerOrder") private Collection<OrderItem> orderItemCollection; public CustomerOrder() { }}@Entity@IdClass(OrderItemPK.class)public class OrderItem implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId protected OrderItemPK orderItemPK; @Basic(optional = false) @Column(name = "itemDesc") private String itemDesc;@Id@ManyToOne(optional = false)@JoinColumns({ @JoinColumn(name="idOrder", referencedColumnName="idOrder"), @JoinColumn(name="ItemNumber", referencedColumnName="ItemNumber")})//@JoinColumn(name = "idOrder", referencedColumnName = "idOrder", insertable = false, updatable = false)private CustomerOrder customerOrder; private CustomerOrder customerOrder; public OrderItem() { this.orderItemPK = new OrderItemPK(); }}@Embeddablepublic class OrderItemPK implements Serializable { @Basic(optional = false) @Column(name = "idOrder") private int idOrder; @Basic(optional = false) @Column(name = "itemNumber") private int itemNumber; public OrderItemPK() { }}The test Source:public static void main(String[] args) { factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); // Fill the items fileds OrderItem item = new OrderItem(); item.setItemDesc("Item Text"); // Fill the orders fields CustomerOrder order = new CustomerOrder(); order.setOrderText("Order text"); // Fill the relationship fields order.getOrderItemCollection().add(item); item.setCustomerOrder(order); em.persist(order); em.getTransaction().commit();}I have no idea what I'm doing wrong, any suggestions are welcome. 解决方案 Well i managed to solve the problem by removing idClass annotation and adding the @MapIdheres the final code:@Entity@Table(name = "Orders")public class CustomerOrder implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "idOrder") private Integer idOrder; @Basic(optional = false) @Column(name = "orderText") private String orderText; @OneToMany(mappedBy = "customerOrder", cascade = CascadeType.ALL) private Collection<OrderItem> orderItemCollection; public CustomerOrder() { }@Entity@Table(name = "OrdersItems")public class OrderItem implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId protected OrderItemPK orderItemPK; @Basic(optional = false) @Column(name = "itemDesc") private String itemDesc; @MapsId("idOrder") @ManyToOne(optional = false) @JoinColumn(name = "idOrder", referencedColumnName = "idOrder", nullable = false) private CustomerOrder customerOrder; public OrderItem() { this.orderItemPK = new OrderItemPK(); }@Embeddablepublic class OrderItemPK implements Serializable { @Basic(optional = false) @Column(name = "idOrder") private int idOrder; @Basic(optional = false) @Column(name = "itemNumber") private int itemNumber; public OrderItemPK() { }public static void main(String[] args) { factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); // Fill the order item OrderItem item = new OrderItem(); item.getOrderItemPK().setItemNumber(1); item.setItemDesc("Product Name"); // Fill the order CustomerOrder order = new CustomerOrder(); order.setOrderText("Testing"); // Create relationship order.getOrderItemCollection().add(item); item.setCustomerOrder(order); // Persist em.persist(order); em.getTransaction().commit();}But i can't get it work with IdClass, here's the source@Embeddablepublic class OrderItemPK implements Serializable { @Basic(optional = false) @Column(name = "idOrder") private Integer idOrder; @Basic(optional = false) @Column(name = "ItemNumber") private Integer itemNumber; public OrderItemPK() { }@Entity@IdClass(OrderItemPK.class)public class OrderItem implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "itemNumber") private Integer itemNumber; @Basic(optional = false) @Column(name = "itemDesc") private String itemDesc; @MapsId("idOrder") @ManyToOne(optional = false) @JoinColumn(name = "idOrder", referencedColumnName = "idOrder", nullable = false) private CustomerOrder customerOrder; public OrderItem() { } @Entity public class CustomerOrder implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "idOrder") private Integer idOrder; @Basic(optional = false) @Column(name = "OrderText") private String orderText; @OneToMany(mappedBy = "customerOrder", cascade = CascadeType.ALL) private Collection<OrderItem> orderItemCollection; public CustomerOrder() { this.orderItemCollection = new ArrayList(); } 这篇关于JPA EclipseLink oneToMany派生ID失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 07:17