[jboss-user] [EJB 3.0] - Re: Compound primary key with cmr

Ajaleo do-not-reply at jboss.com
Wed Aug 2 07:42:35 EDT 2006


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!

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3962421#3962421

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3962421



More information about the jboss-user mailing list