users@glassfish.java.net

How to persist an entity with Composite primary key

From: <glassfish_at_javadesktop.org>
Date: Sun, 24 Jun 2007 13:03:10 PDT

Problem Statement:-
Entity A have many to one relation with Entity C
Entity B have many to one relation with Entity C
Entity C have a composite primary key of (Entity A PK & Entity B PK)

A --< C
B --< C

the entites are automatic generated by Dali tool

@Entity
public class C implements Serializable
{
        @EmbeddedId
        private C.PK pk;

        private int attribute;

        //Code
        @Embeddable
        public static class PK implements Serializable
        {
                public int A_Id;
                public int B_Id;
                
                //Code
        }
}

@Entity
public class A implements Serializable
{
        @Id
        private int AId;

        @OneToMany
        private Set<C> C_Collection;
        
        //Code
}

@Entity
public class B implements Serializable
{
        @Id
        private int BId;

        @OneToMany
        private Set<C> C_Collection;
        
        //Code
}

i made a session Bean which add a new entity C
Session
{
        A a = em.find(A.class, AId);
        B b = em.find(B.class, BId);
        
        
        C c = new C();

        C.PK pk = new C.PK();
        pk.AId(a.getID());
        pk.BId(b.getID());
        
        c.setPk(pk);
        c.setA(a);
        c.setB(b);
        c.setAttribute(12);
        em.persist(c);
}

when running this code an exception is raised

Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2006.8 (Build 060830)): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'A_ID' in 'field list'Error Code: 1054
Call:INSERT INTO C (attribute, Aid, A_ID, B_ID, Bid) VALUES (?, ?, ?, ?, ?)
        bind => [2, 6, 6, 1, 1]


this is logic as Table "C" has only 3 coloumns only "attribute" , "Aid" , "Bid"
but the mapping is different as it adds the composite PK attributes to the insert statement

if there is a sample code or how to solve this issue it would be great
[Message sent by forum member 'bakr_awad' (bakr_awad)]

http://forums.java.net/jive/thread.jspa?messageID=223712