2 Replies Latest reply on Aug 2, 2006 7:42 AM by Aitor Menta Segura

    Compound primary key with cmr

    Aitor Menta Segura Newbie

      hi all!

      I'd like to know how I can create a entity with has a compound primary key which fields that are cmrs too. I've been googling and searching this forum but I haven't found how.

      I've create the Entity with the X cmrs and an
      @Id
      public Long id

      and a SessionBean to ensure that there isn't other entity with the same fields but I'm looking for this checking being performed automatically. I've used Dali and Netbeans to get the Entity from the database but it doesn't deploy.

      I've create the Entity bean with the three cmrs and the three ids (all of the related entities have one Id field) but the queries hibernate create include the fields duplicated (one for cmr, one for cmp)


      Could you give me any information?

      Thanks a lot!

        • 1. Re: Compound primary key with cmr
          Emmanuel Bernard Master

          not sure I understand everything but here is my answer
          You can do that with Hibernate, but the specification does not support it. Hence Dali & co probably do not support it

          • 2. Re: Compound primary key with cmr
            Aitor Menta Segura Newbie

            Thanks,

            this is a excerpt of the code:

            Class Person:

            @Entity
            public class Persona implements Serializable {
            
             @EmbeddedId
             private PersonaPK pK;
            
             @Transient
             private Pais pais;
            
             private String nombre;
            
             private String numTelefono;
            
             /** Creates a new instance of Persona */
             public Persona() {
             }
            
            
            
            
             public String getNombre() {
             return nombre;
             }
            
             public void setNombre(String nombre) {
             this.nombre = nombre;
             }
            
             public String getNumTelefono() {
             return numTelefono;
             }
            
             public void setNumTelefono(String numTelefono) {
             this.numTelefono = numTelefono;
             }
            
             public String toString() {
             return "" + this.getPK().getDni();
             }
            
            
             @JoinColumn(name="IDPAIS")
             @ManyToOne
             public Pais getPais() {
             return pais;
             }
            
             public void setPais(Pais pais) {
             this.pais = pais;
             }
            
             public PersonaPK getPK() {
             return pK;
             }
            
             public void setPK(PersonaPK pK) {
             this.pK = pK;
             }
            
             @Transient
             public Long getDni() {
             return pK.getDni();
             }
            
             public void setDni(Long dni) {
             pK.setDni(dni);
             }
            
            }
            



            Class Country
            @Entity
            public class Pais implements Serializable {
            
             @Id
             @GeneratedValue(strategy = GenerationType.AUTO)
             private Long id;
            
            
             private String nombre;
            
             private String moneda;
            
             @OneToMany(mappedBy="pais")
             private List<Persona> habitantes;
            
             /** Creates a new instance of Pais */
             public Pais() {
             }
            
             public Long getId() {
             return id;
             }
            
             public void setId(Long id) {
             this.id = id;
             }
            
             public String toString() {
             //TODO change toString() implementation to return a better display name
             return "" + this.getId();
             }
            
             public String getNombre() {
             return nombre;
             }
            
             public void setNombre(String nombre) {
             this.nombre = nombre;
             }
            
             public String getMoneda() {
             return moneda;
             }
            
             public void setMoneda(String moneda) {
             this.moneda = moneda;
             }
            
             public List<Persona> getHabitantes() {
             return habitantes;
             }
            
             public void setHabitantes(List<Persona> habitantes) {
             this.habitantes = habitantes;
             }
            
            }
            
            
            



            And the primary key
            @Embeddable
            public class PersonaPK implements Serializable {
            
            
             private Long dni;
            
            
             private Long idPais;
            
             /** Creates a new instance of PersonaPK */
             public PersonaPK() {
             }
            
             public PersonaPK(Long dni,Long idPais) {
             this.dni=dni;
             this.idPais=idPais;
             }
            
             public String toString() {
             return "" + this.getDni();
             }
            
            
             @Id
             public Long getDni() {
             return dni;
             }
            
             public void setDni(Long dni) {
             this.dni = dni;
             }
            
            
            
             public boolean equals(Object obj) {
             if (obj == this) return true;
             if (!(obj instanceof PersonaPK))
             return false;
             PersonaPK pk = (PersonaPK)obj;
            
             if (!getDni().equals(pk.getDni()))
             return false;
             if (!getIdPais().equals(pk.getIdPais()))
             return false;
             return true;
             }
             public int hashCode( ) {
             return getDni().hashCode( ) + getIdPais().hashCode();
             }
            
             @Id
             public Long getIdPais() {
             return idPais;
             }
            
             public void setIdPais(Long idPais) {
             this.idPais = idPais;
             }
            
            
            }
            



            I've needed to use Transient so the container create SQL sentences without duplicate columns. However if the relation between (Pais-Persona) is bidirectional Jboss tell me
            org.hibernate.AnnotationException: mappedBy reference an unknown property com.mycompany.theapp.Persona.pais

            I think that's because I've used transient in the field pais of the entity Persona.

            Briefly, the main problem is that I can't establish a bidirectional relation between two entities and the primary keys of one of the two entities is composed by a field which is a CMR too.

            Please, if somebody need more explanations ask me.


            Thanks a lot!