1 Reply Latest reply on May 8, 2006 12:09 PM by cyril.joui

    Multiple PK, one from joined column

    elvisd

      Hi all,

      I have this situation:
      2 entities, country and state
      country has code and name as fields, code is PK
      state has code, name and country_code (linked to country code field) as fields, code is PK and country_code should too.


      @Entity @Table(name="country")
      class Country implements Serializable{
      @Id @Column(name="code",length=2)
      private String code;
      @Column(name="name",length=50)
      private String name;
      
      //setters and getters here
      }
      

      then
      @Entity @Table(name="state")
      class State implements Serializable{
      @Id @Column(name="code",length=2)
      private String state;
      @Column(length=50)
      private String name;
      @ManyToOne
      @JoinColumn(name = "countryCode", nullable = false)
      private Country country; //<-- should be PK too!!!
      //setters and getters here
      }
      


      Can someone help me to understand this?
      Thank you all in advance

        • 1. Re: Multiple PK, one from joined column

          Here is an example :

          With Order / OrderPK / OrderLine (=> has a composite primary key with 2 foreign keys)

          @Entity
          @Table(name="ORDERS")
          public class Order {
          
           private int id;
          
           private Date orderDate;
          
           @Id
           @GeneratedValue(strategy=GenerationType.AUTO)
           public int getId() {
           return id;
           }
          
           public void setId(int id) {
           this.id = id;
           }
          
           public Date getOrderDate() {
           return orderDate;
           }
          
           public void setOrderDate(Date orderDate) {
           this.orderDate = orderDate;
           }
          }
          





          
          @Entity
          @IdClass(OrderLinePk.class)
          public class OrderLine {
          
           private Product product;
          
           private Order order;
          
           private int quantity;
          
           public OrderLine() {
           }
          
           public int getQuantity() {
           return quantity;
           }
          
           public void setQuantity(int quantity) {
           this.quantity = quantity;
           }
          
           @ManyToOne
           @JoinColumn(name="orderId", insertable=false, updatable=false)
           public Order getOrder() {
           return order;
           }
          
           public void setOrder(Order order) {
           this.order = order;
           }
          
           @ManyToOne
           @JoinColumn(name="productId", insertable=false, updatable=false)
           public Product getProduct() {
           return product;
           }
          
           public void setProduct(Product product) {
           this.product = product;
           }
          
           @Id
           public int getProductId() {
           return getProduct().getId();
           }
          
           @Id
           public int getOrderId() {
           return getOrder().getId();
           }
          
           public void setOrderId(int orderId) {
           getOrder().setId(orderId);
           }
          
           public void setProductId(int productId) {
           getProduct().setId(productId);
           }
          
          }
          
          
          



          
          @Embeddable
          public class OrderLinePk implements Serializable {
          
           private static final long serialVersionUID = 1L;
          
           private int orderId;
          
           private int productId;
          
           protected OrderLinePk() {
           }
          
           public OrderLinePk(int orderId, int productId) {
           this.orderId = orderId;
           this.productId = productId;
           }
          
           public int getOrderId() {
           return orderId;
           }
          
           public void setOrderId(int orderId) {
           this.orderId = orderId;
           }
          
           public int getProductId() {
           return productId;
           }
          
           public void setProductId(int productId) {
           this.productId = productId;
           }
          
          }
          
          
          


          Hope it would be useful.