users@glassfish.java.net

Foreign Composite Primary Key

From: <glassfish_at_javadesktop.org>
Date: Thu, 30 Aug 2007 16:15:03 PDT

I have an entity which defines a composite primary key, of which one part is a foreign key (many-to-one relationship).

    AClass
    ------
    [PK] ID

    BClass
    ------
    [PK,FK] AClass_ID
    [PK] Sequence

How can such mappings be specified with JPA annotations?

Currently, I have the following, which results in an EntityExistsException ("Cannot persist detached object"):

    @Entity(name = "AClass")
    class AClass {

        @OneToMany(cascade = CascadeType.ALL)
        protected List<BClass> bclasses;

    }

    @Entity(name = "BClass")
    class BClass {

        @Id
        @ManyToOne(optional = false)
        @JoinColumn(name = "ACLASS_ID", nullable = false, updatable = false)
        protected AClass aclass;

        @Id
        protected int sequence;

    }

Any assistance would be greatly appreciated.
[Message sent by forum member 'shelleyb' (shelleyb)]

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