-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: @Id and @ManyToOne for annotate the same field - Why not ?
PostPosted: Fri Aug 31, 2007 10:01 am 
Newbie

Joined: Thu Jul 28, 2005 12:24 pm
Posts: 7
May be this is a stupid question but in a database a column can be either a primary and a foreign-key, so why cannot a field/property be annotated to be both Id and ManyToOne ?

I have 3 classes :
Code:
@Entity
@Table(name="ORDINI")
@SequenceGenerator(sequenceName="HIBERNATE_SEQUENCE", name="SEQ")
public class Ordine {
   @Id
   @GeneratedValue(generator="SEQ")
   private int id;
   
   private int shipped;
   
   private String description;
   

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

... getter/setter
}

@Entity
@Table(name="PRODOTTI")
@SequenceGenerator(sequenceName="HIBERNATE_SEQUENCE", name="SEQ")
public class Prodotto {
    @Id
    @GeneratedValue(generator="SEQ")
   private int id;

... getter/setter
}

@Entity
@Table(name="RIGHE_ORDINE")
@IdClass(RigaOrdine.RighaOrdinePk.class)
public class RigaOrdine {
   
   @Id
   @Column(insertable=false, updatable = false)
   [b]private Ordine ordine;[/b]

   @Id
   @Column(insertable=false, updatable = false)
   [b]private Prodotto prodotto;[/b]
   
   private int qty;
   
   
   
   @Embeddable
   public static class RighaOrdinePk implements Serializable {

      @ManyToOne
      @JoinColumn(name="ORD_ID")
      [b]private Ordine ordine;[/b]

      @ManyToOne
      @JoinColumn(name="PROD_ID")      
      [b]private Prodotto prodotto;[/b]
      
      ... getter/setter/equals...
   }



This works fine but there is a big problem : in the Ordine class i cannot define a @OneToMany property related to the ordine field in the RigaOrdine class.

This is because this field is already annotated as @Id and cannot be annotated also as @ManyToOne twice.
I've also tried to use the the ordine field in the inner PK class (already annotated as @ManyToOne) but i've not found a way to do that.

I've also tried to see what does the JPA tool in Eclipse generates starting from my database. The generated PK class looks like this one :

Code:
public class RigaOrdinePK implements Serializable {
    @Column(name = "ord_id", nullable = false)
    private int orderId;
    @Column(name = "prod_id", nullable = false)
    private int productId;
...


and the RigaOrdine :

Code:
    @EmbeddedId
    protected RigaOrdinePK rigaOrdinePK;
    @Column(name = "qty", nullable = false)
    private int qty;
    @JoinColumn(name = "ord_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne
    private Ordine ordine;
    @JoinColumn(name = "prod_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne
    private Prodotto prodotto;




This works fine and in the Ordine class i can define may OneToMany relation. But i found this mapping not clear (in my model the Pk is the class, not the id value ..)

Do you have any suggestion on how to map this kind of composite key ?

TIA

Sergio Sette


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.