Enterprise JavaBeans - HowTo: construct Composite key using other composite key?

5 Messages Forum Options Options
Embed this topic
Permalink
holod
Enterprise JavaBeans - HowTo: construct Composite key using other composite key?
Reply Threaded MoreMore options
Print post
Permalink
Hello. I don' undestand how is it possible to do.
Please, see my code:

@Entity
@Table(name = "Person", schema = SCHEMA)
 
public class Person {
 
        @Id
        @GeneratedValue
        @Column(name = "id")
        private int id;
 
        private String medialogyUid;
        private String firstName;
        private String lastName;
        private String patronymic;
        private String biography;
}

 
 
@Entity
@IdClass(Convocation.PK.class)
@Table(name = "Convocation", schema = SCHEMA)
 
public class Convocation {
 
        @Id
        private int number;
       
        @Id
        @ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST})
        private Institution institution;
       
        private Date beginDate;
        private Date endDate;
       
        public Convocation(){
               
        }
       
        public Convocation(Institution inst, int number, Date beginDate, Date endDate){
                this.institution = inst;
                this.number = number;
                this.beginDate = beginDate;
                this.endDate = endDate;
        }
       
        public static final class PK implements Serializable {
 
                public int number;
                public int institution;
               
                public PK(){
                       
                }
                /**@param number is convocation,number
                 * @param institution is convocation.institution.id
                 * */
                public PK(int number, int institution){
                        this.number = number;
                        this.institution = institution;
                }
               
                @Override
                public boolean equals(Object o) {
                        if (!(o instanceof PK))
                                return false;
                        PK oPK = (PK)o;
                        return this.number == oPK.number && this.institution  == oPK.institution;
                }
 
                @Override
                public int hashCode() {
                        return ((number == 0) ? 0 : new Integer(number).hashCode()) ^ ((institution == 0) ? 0 : new Integer(institution).hashCode());
                }
    }

 
//And finally the last class:
 
@Entity
@IdClass(DeputyMandate.PK.class)
@Table(name = "DeputyMandate", schema = SCHEMA)
public class DeputyMandate {
       
        @Id
    @ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST})
        private Person person;
 
        @Id
        @ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST})
        private Convocation convocation;
       
        private Date dateIn;
        private Date dateOut;
       
        public static final class PK implements Serializable {
               //don't know what to use @Embeddable or what?
        }
    }




I want DeputyMandate to use composite key. This composite key is based on Person with simple key (int id) and Convocation PK.
Please, tell me how can I make composite key DeputyMandate.PK using simple key from Person and composite key from Convocation.
Pinaki Poddar
Re: Enterprise JavaBeans - HowTo: construct Composite key using other composite key?
Reply Threaded MoreMore options
Print post
Permalink
Hi,
  For multi-level derived identity, please see the discussion and examples provided with [1].
  OpenJPA manual describes the ground rules [2].


[1] https://issues.apache.org/jira/browse/OPENJPA-207
[2] http://openjpa.apache.org/builds/1.2.0/apache-openjpa-1.2.0/docs/manual/manual.html#ref_guide_pc_oid_entitypk
holod
Re: Enterprise JavaBeans - HowTo: construct Composite key using other composite key?
Reply Threaded MoreMore options
Print post
Permalink
I use composite keys successfully. These keys are based on @id of @Entities which have simple types (int, String, Date, e.t.c.).
The problem is that I can't create compostie key klass, because one of it's fields is @Entity with composite key. But It's not simple type, like int or long or String or Date.

I don't know what to do in this case.
Pinaki Poddar wrote:
Hi,
  For multi-level derived identity, please see the discussion and examples provided with [1].
  OpenJPA manual describes the ground rules [2].


[1] https://issues.apache.org/jira/browse/OPENJPA-207
[2] http://openjpa.apache.org/builds/1.2.0/apache-openjpa-1.2.0/docs/manual/manual.html#ref_guide_pc_oid_entitypk
Pinaki Poddar
Re: Enterprise JavaBeans - HowTo: construct Composite key using other composite key?
Reply Threaded MoreMore options
Print post
Permalink
Hi,
  The cited example [1] does demonstrate an use case where a second-level derived identity I2 (PageId) is composed of another identity I1 (BookId) which itself is a non-primitive or non-String composite key derived from another entity (Library).

  If you have access to OpenJPA source tree then you can find the test cases of similar usage with Page/Book/Library entity classes.
 
holod
Re: Enterprise JavaBeans - HowTo: construct Composite key using other composite key?
Reply Threaded MoreMore options
Print post
Permalink
I didn't see attached archive the first time, I 've visited the link.
Now, I see it. It seems to me better to include example when Composite key class uses other composite key as parameter.
Also I've explored such thing:
Entity Class A has Composite key A.PK
A.PK has fields: int, int.

Entity Class B has Composite key B.PK
B.PK has fields: int, A.PK

Entity Class C has Composite key C.PK
C.PK has fields: int, B.PK.

Entites A and B will be properly presented in DB tables.

But Entity C has Composite key (C.PK), based on other composite key (B.PK), which is also based on composite key (A.PK).
Entity C will not be presented. I don't know why. Maybe I make some mistakes, maybe the reason is other.
Entity A will have 2 key fields (int, int)
Entity B will have 3 key fields (approximate limit) (int,int form A.PK and own int)
Entity C will have 5 key fields (int,int from A.PK, int,int from B.PK and own int)

Databases works badly when table has more than 3 key fields.


Pinaki Poddar wrote:
Hi,
  The cited example [1] does demonstrate an use case where a second-level derived identity I2 (PageId) is composed of another identity I1 (BookId) which itself is a non-primitive or non-String composite key derived from another entity (Library).

  If you have access to OpenJPA source tree then you can find the test cases of similar usage with Page/Book/Library entity classes.