Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Unabled to set derived IDs(I can't set derived ids in a classic Master Detail pattern)
Unabled to set derived IDs [message #1670990] Fri, 13 March 2015 12:19 Go to next message
Matias Panasci is currently offline Matias PanasciFriend
Messages: 7
Registered: March 2015
Junior Member
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 DB

index.php/fa/21177/0/



  • Attachment: bd.png
    (Size: 36.69KB, Downloaded 355 times)

[Updated on: Fri, 13 March 2015 13:57]

Report message to a moderator

Re: Unabled to set derived IDs [message #1671197 is a reply to message #1670990] Fri, 13 March 2015 13:58 Go to previous message
Matias Panasci is currently offline Matias PanasciFriend
Messages: 7
Registered: March 2015
Junior Member
Well i managed to solve the problem by removing idClass annotation and adding the @MapId
heres 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();
  }

@Embeddable
public 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();        
}

Previous Topic:java.lang.IncompatibleClassChangeError: org/eclipse/persistence/internal/jpa/metadata/accessors/obje
Next Topic:@MappedSuperClass with InheritanceType.JOINED nullPointerException
Goto Forum:
  


Current Time: Tue Mar 19 05:47:14 GMT 2024

Powered by FUDForum. Page generated in 0.03021 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top