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 #1670979] Fri, 13 March 2015 12:14 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/21176/0/

The entities:

@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(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")
     })      
    private CustomerOrder customerOrder;
    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() {
    }
}


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.
Re: Unabled to set derived IDs [message #1672208 is a reply to message #1670979] Fri, 13 March 2015 22:51 Go to previous messageGo to next message
Will Dazey is currently offline Will DazeyFriend
Messages: 10
Registered: February 2015
Junior Member
Some things to note:

  1. OrderItem must use either an ID class or an embedded ID to specify its composite primary key, not both. If you define both as you have it, you get the exception you are seeing at runtime.
  2. OrderItemPK class should include method definitions for equals() and hashcode(). You may have dropped this off to paste here, but this and #1 are both caught by Eclipse at compile time.
  3. OrderItem.customerOrder shouldn't be annotated with @Id and you have one too many OrderItem.customerOrder defined. Might have been a mistype, but the latter is another compile time error that an IDE like Eclipse should warn you about.

Re: Unabled to set derived IDs [message #1681974 is a reply to message #1672208] Tue, 17 March 2015 14:41 Go to previous message
Matias Panasci is currently offline Matias PanasciFriend
Messages: 7
Registered: March 2015
Junior Member
Thank you very much for your help, I was finally able to run the example with the advice you gave me.
Previous Topic:JTA Persistence issue (Null Pointer Exception)
Next Topic:Creating a entity instance dynamically
Goto Forum:
  


Current Time: Mon Oct 07 10:30:51 GMT 2024

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

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

Back to the top