3 Replies Latest reply on Feb 9, 2006 4:28 PM by epbernard

    primarykey mapping problem

    jzmmek2k2

      I have got a question about primarykey-mapping.

      Having two tables:

      t_customer
      ---------------
      int | customer_id (PK)
      varchar | firstname
      varchar | lastname

      t_friendship
      ---------------
      int | customer_id (PK)
      int | friend_customer_id (PK)
      date | created


      Is it possible to map this two tables to the following class-structure using annotations ?

      class Customer
      {
      public Long getId() {...}
      public String getFirstname() {...}
      public String getLastname() {...}
      }

      class Friendship
      {
      public Customer getCustomer() {...}
      public Customer getFriend() {...}
      public Date getCreated() {...}
      }


      The "Customer"-class is not the problem, but I am simply unable to declare "getCustomer() & getFriend()" as compound primary-key of class "Friendship".


      Any help would be appreciated

      Regards
      Jan

        • 1. Re: primarykey mapping problem
          bill.burke

          Use @IdClass

          See Hibernate EntityManager doco or specification . I don't have an wxample of it in the tjutotirals Only @EmbeddedId

          • 2. Re: primarykey mapping problem
            jzmmek2k2

            Thanks for fast reply, but I still having problems get it working.


            The following schema is generated

            CREATE TABLE t_customer
            (
             customer_id int8 NOT NULL,
             firstname varchar(255),
             lastname varchar(255),
             CONSTRAINT t_customer_pkey PRIMARY KEY (customer_id)
            )
            WITHOUT OIDS;
            
            CREATE TABLE t_friendship
            (
             customer bytea NOT NULL,
             friend bytea NOT NULL,
             created timestamp,
             CONSTRAINT t_friendship_pkey PRIMARY KEY (customer, friend)
            )
            WITHOUT OIDS;



            When I deploy the following classes

            @Entity
            @Table(name="t_customer")
            public class Customer implements Serializable
            {
             private Long pk = null;
             private String firstname = null;
             private String lastname = null;
            
             public Customer2()
             {
             }
            
             public String getFirstname()
             {
             return firstname;
             }
            
             public void setFirstname(String name)
             {
             this.firstname = name;
             }
            
             public String getLastname()
             {
             return lastname;
             }
            
             public void setLastname(String name2)
             {
             this.lastname = name2;
             }
            
             @Id
             @GeneratedValue(strategy=GenerationType.AUTO)
             @Column(name="customer_id")
             public Long getPk()
             {
             return pk;
             }
            
             public void setPk(Long pk)
             {
             this.pk = pk;
             }
            }




            @Embeddable
            public class FriendshipPK implements Serializable
            {
             private Customer customer = null;
             private Customer friend = null;
            
             public FriendshipPK()
             {
             }
            
             public Customer getCustomer()
             {
             return customer;
             }
            
             public void setCustomer(Customer customer)
             {
             this.customer = customer;
             }
            
             public Customer getFriend()
             {
             return friend;
             }
            
             public void setFriend(Customer friend)
             {
             this.friend = friend;
             }
            
            }



            @Entity
            @Table(name="t_friendship")
            @IdClass(FriendshipPK.class)
            public class Friendship
            {
             private Customer customer = null;
             private Customer friend = null;
            
             private Date created = null;
            
             public Friendship()
             {
             }
            
             public Date getCreated()
             {
             return created;
             }
            
             public void setCreated(Date created)
             {
             this.created = created;
             }
            
             @EmbeddedId
             public Customer getCustomer()
             {
             return customer;
             }
            
             public void setCustomer(Customer customer)
             {
             this.customer = customer;
             }
            
             @EmbeddedId
             public Customer getFriend()
             {
             return friend;
             }
            
             public void setFriend(Customer friend)
             {
             this.friend = friend;
             }
            }




            There is no difference when using "@Id" instead of "@EmbeddedId" in class "Friendship".


            It always generates the columns "customer" & "friend" of type "byte" instead of "int8".


            I am using EJB3 RC4-PFD.


            Looking in the docs does not really help, because it always describes PK consisting of primitive types.


            Would be great if anyone could help me.

            Regards
            Jan

            • 3. Re: primarykey mapping problem
              epbernard

              please reread the whole chapter about composite primary keys you're are mismatching all annotations.