8 Replies Latest reply on May 4, 2007 11:11 PM by fhh

    help composite keys

    sashaxiv

      what´s wrong with this?

      @Entity
      @Table(name="ruta")
      public class Ruta{

      .....

      @OneToMany(mappedBy="ruta", cascade=CascadeType.REMOVE)
      public List getPuertos() {
      return rutapuertos;
      }

      .....


      ------------------

      @Entity
      @Table(name="puerto")
      public class Puerto {
      ...

      @OneToMany(mappedBy="puerto",cascade=CascadeType.REMOVE)
      public List getRutaPuertos() {
      return rutaPuertos;
      }
      ...
      ---------------------


      @Entity
      @Table(name="rutapuerto")
      @IdClass(RutaPuertoPK.class)
      public class RutaPuerto{
      @Id
      @Column(name="idPuerto",nullable=false,insertable=false, updatable=false)
      public Long getIdPuerto() {
      return idPuerto;
      }

      //setter

      @Id
      @Column(name="idRuta",nullable=false,insertable=false,
      updatable=false)
      public Long getIdRuta() {
      return idRuta;
      }

      //setter

      @Column(name="kilometros")
      public Double getKilometros() {
      return kilometros;
      }

      public void setKilometros(Double kilometros) {
      this.kilometros = kilometros;
      }

      @ManyToOne(optional=false)
      @JoinColumn(name="idPuerto")
      public Puerto getPuerto() {
      return puerto;
      }

      public void setPuerto(Puerto puerto) {
      this.puerto = puerto;
      }

      @ManyToOne(optional=false)
      @JoinColumn(name="idRuta")
      public Ruta getRuta() {
      return ruta;
      }

      ..............


      }



      Jboss tells me next error:

      .......................
      .......................
      Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: es.rutamanager.modelo.rutapuerto.entidad.RutaPuerto column: idPuerto (should be mapped with insert="false" update="false")


      thanks!

        • 1. Re: help composite keys

          Apart fropm many things that went worng with your ERM you have two @Ids on rutapuerto.

          Regards

          Felix

          • 2. Re: help composite keys
            sashaxiv

            but with IdClass anottation two @ids are allowed. this is the diference with embedable

            i try this, once i have read the next link
            http://arsenalist.com/2007/02/23/additional-columns-in-a-manytomany-mapping-using-java-persistence-api-toplink/

            i need a many to many mapping with a new column.

            • 3. Re: help composite keys

              Sorry missde that annotation.

              But your problem is quite simple. You are using "idruta" as part of the pk and as fk.

              Regards

              Felix

              • 4. Re: help composite keys
                sashaxiv

                i do what you say because i need it. I have a many to many relation beetwen "puerto" and "ruta" and the pks in these classes(idRuta and idPuerto) are the composite pk in the "rutapuerto" entity and also they are fks referencing puerto and ruta.

                This is Database Theory but what can i do to this in EJB3?

                if i can´t find a solution i will use ManytoMany annotation creating a new table
                with it and i will use two lists, but how can i aggregate a new field with this annotation, and before find a entity in the table.

                • 5. Re: help composite keys

                   


                  This is Database Theory but what can i do to this in EJB3?


                  This not about database theory nor about EJB 3 but about your data model. I think you should focus first on what you actually want to map.

                  I assume the follwing is true:

                  1.) One Route consists of many ports.

                  2.) One port can be a destionation of many routes.

                  3.) Port and route are mapped correctly (e.g. have @Id, overriden equals(), hashCode() etc.)

                  Questions:

                  1.) Is a port unique on each route? (Can a single port be twice on the iternary of a route?)

                  2.) What is the distance for? The distance of the total route? Or between two ports?

                  Regards

                  Felix



                  • 6. Re: help composite keys
                    sashaxiv

                    well, at first thank you for your time

                    a Ruta can have 0,n Puertos
                    a Puerto can be part of 0,n Rutas

                    the distance(which is also a attribute in Puerto and Ruta with other significant) indicates the distance beetwen the start of the Ruta and start of the Puerto. I need it to calculate a coeficient of the difficult of a Ruta.

                    Thanks felix!!

                    • 7. Re: help composite keys
                      sashaxiv

                      finally i solved my problem!! this is what i do


                      @Entity
                      @Table(name="rutapuerto")
                      public class RutaPuerto{

                      private RutaPuertoPK pk;
                      private Puerto puerto;
                      private Ruta ruta;
                      private Double kilometros;

                      public RutaPuerto() {}

                      public RutaPuerto(RutaPuertoPK pk, Puerto puerto, Ruta ruta, Double kilometros) {
                      super();
                      this.pk = pk;
                      this.puerto = puerto;
                      this.ruta = ruta;
                      this.kilometros = kilometros;
                      }



                      @EmbeddedId
                      public RutaPuertoPK getPk() {
                      return pk;
                      }

                      public void setPk(RutaPuertoPK pk) {
                      this.pk = pk;
                      }

                      @Column(name="kilometros")
                      public Double getKilometros() {
                      return kilometros;
                      }

                      public void setKilometros(Double kilometros) {
                      this.kilometros = kilometros;
                      }

                      @ManyToOne
                      @JoinColumn(name="idPuerto", updatable=false, insertable=false)
                      public Puerto getPuerto() {
                      return puerto;
                      }

                      public void setPuerto(Puerto puerto) {
                      this.puerto = puerto;
                      }

                      @ManyToOne
                      @JoinColumn(name="idRuta", updatable=false, insertable=false)
                      public Ruta getRuta() {
                      return ruta;
                      }

                      public void setRuta(Ruta ruta) {
                      this.ruta = ruta;
                      }


                      }


                      ------------------------------------------------------------

                      @Entity
                      @Table(name="puerto")
                      public class Puerto {

                      .......

                      @OneToMany(mappedBy="puerto",cascade=CascadeType.REMOVE)
                      public List getRutaPuertos() {
                      return rutaPuertos;
                      }
                      ----------------------------------------------------------


                      @Entity
                      @Table(name="ruta")
                      public class Ruta{

                      public Ruta() {}

                      @OneToMany(mappedBy="ruta", cascade=CascadeType.REMOVE)
                      public List getPuertos() {
                      return rutapuertos;
                      }

                      ----------------------------------------------------------------------


                      @Embeddable
                      public class RutaPuertoPK implements Serializable{

                      private Long idRuta;
                      private Long idPuerto;

                      public RutaPuertoPK() {}

                      ..........................
                      --------------------------------------------
                      Anyway very much thanks Felix!!!


                      • 8. Re: help composite keys

                        In thoery this would probably best be solved by using @idclass with ruta and puerto as the two @ids (assuimng every puerto is only once on a route).

                        But if your solution works for you I wouldn't bother.

                        Regards

                        Felix