2 Replies Latest reply on Feb 7, 2006 11:33 AM by chrismalan

    Many to many plus additional value

    chrismalan

      This question has been asked at least twice. The answer is: "use an intermediate entity to represent your assoc table"

      I looked but could not find much on this in the Hibernate documentation. It is most likely where I did not look.

      I have an idea of what to do, but do not know if it right.

      Take the JBoss Flights_Customers example. Say I want to add another column named Date to the join table.

      Do I do this:

      @Entity
      public class Flights_Customers{
       private String flight, customer;
       private java.sql.Date date;
      

      and then in Flight
      @JoinTable(
       table=@Table(name="Flights_Customers"),
       joinColumns={@JoinColumn(name="flight")},
       inverseJoinColumns={@JoinColumn(name="customer")}
      

      In Customer the joinColumns and inverseJoinColumns swap over. I use a few more annotations to set up the join, but this is what is relevant.

      Is this how it should be done? If not, what should be done differently?

      Thanks,

        • 1. Re: Many to many plus additional value
          alximik

          Try this

          @Entity
          public class Flights_Customers{
           @ManyToOne
           private Flight flight;
          
           @ManyToOne
           private Customer customer;
          ...........
          }
          
          @Entity
          public class Customer{
           @OneToMany(mappedBy="customer")
           private Collection<Flights_Customers> flights_Customers;
          ...........
          }
          
          @Entity
          public class Flight{
           @OneToMany(mappedBy="flight")
           private Collection<Flights_Customers> flights_Customers;
          }
          



          • 2. Re: Many to many plus additional value
            chrismalan

            Thanks Alximik,

            I suppose one will then get all the customers for a flight (or vice versa) straight from the relationship table. I am quite sure this may actually work.

            Thanks again.

            Chris