0 Replies Latest reply on Sep 17, 2005 9:37 AM by Andrig Miller

    Relationship specified with @JoinColumn doesn't actually do

    Andrig Miller Newbie

      In my application, I have an Order bean and an OrderLine bean with a one-to-many relationship specified with a join column that is as follows:

      Order's primary key is orderId which is an auto increment column in MySQL:

      private long orderId;
      ...
      private List orderLines;

      ...

      @Id(generate = GeneratorType.AUTO)
      public long getOrderId () {

      return orderId;

      }

      ...

      @OneToMany(mappedBy="order", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
      public List getOrderLines() {

      return orderLines;

      }

      public void setOrderLines(List orderLines) {

      this.orderLines = orderLines;

      }

      In the OrderLine class I have the following:

      @IdClass(OrderLinePK.class)
      @Entity
      @Table(name="EJB3.OrderLine")
      public class OrderLine {

      private OrderLinePK orderLinePK;

      ...

      private Order order;

      ...

      @ManyToOne()
      @JoinColumn(name="orderId", insertable=false, updatable=false)
      public Order getOrder() {

      return order;

      }

      public void setOrder(Order order) {

      this.order = order;

      }

      and the OrderLinePK class looks like the following:

      public class OrderLinePK implements Serializable {

      private long orderId;
      private long lineNumber;

      public OrderLinePK() {


      }

      public OrderLinePK(long orderId
      , long lineNumber) {

      this.orderId = orderId;
      this.lineNumber = lineNumber;

      }

      public long getOrderId() {

      return orderId;

      }

      public void setOrderId(long orderId) {

      this.orderId = orderId;

      }

      public long getLineNumber() {

      return lineNumber;

      }

      public void setLineNumber(long lineNumber) {

      this.lineNumber = lineNumber;

      }



      }

      So the OrderLine primary key contains the orderId from the Order, and at the physical database level there is a foreign key relationship from the orderId of the OrderLine to the orderId of the Order. The orderId of the OrderLine is not an auto increment column, it actually contains the value from the order table.

      Now, what I expected to happen with this setup, is when I fetched an Order, that the OrderLines would be loaded through an actual join as follows:

      select orderId, customerid, ...
      from Order o, OrderLine ol
      where o.orderId = ol.orderId;

      Instead I get a select from the Order table, and then a separate select from the OrderLines, resulting in two queries to the database instead of the natural join that I expected. What's the point of actually specifying a join column, and going through the effort of actually setting up the logical and physical models if there is not going to be a join?