-->
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: Composite key with FK being part of PK and Serial
PostPosted: Fri May 19, 2006 2:29 pm 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
Hibernate version:3.2CR2


Hi,

I´m trying to get the following using an embedded id
with a ManyToOne association but I can´t get it to work
properly


My Tables are like this:

Code:
CREATE TABLE informix.pedido (
    id_pedido   SERIAL NOT NULL PRIMARY KEY,
    name        VARCHAR(50) NOT NULL
)

CREATE TABLE informix.item (
    id_item     SERIAL NOT NULL,
    id_pedido   INTEGER NOT NULL REFERENCES informix.pedido(id_pedido),
    des         VARCHAR(50) NOT NULL

)


ALTER TABLE item ADD CONSTRAINT PRIMARY KEY (id_item, id_pedido) CONSTRAINT pk_tb_item


My Classes are like this:

Code:

@Entity
public class Pedido {
   
   private Integer Id;
   private String name;
   private Set<Item> itens = new HashSet<Item>();

   public Pedido(){
      
   }

   @Id
   @GeneratedValue (strategy=GenerationType.AUTO)
   @Column(name="id_pedido")
   public Integer getId() {
      return Id;
   }

   public void setId(Integer id) {
      Id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   @OneToMany (cascade=CascadeType.ALL)
   @JoinColumn (name="id_pedido")
   public Set<Item> getItens() {
      return itens;
   }

   public void setItens(Set<Item> itens) {
      this.itens = itens;
   }

}

@Entity
@AssociationOverride (name="idItem.pedido", joinColumns = {@JoinColumn(name="id_pedido")})
public class Item {
   
   private ItemId idItem;
   private String name;
   
   public Item(){
      idItem = new ItemId();
   }

   @EmbeddedId
   public ItemId getIdItem() {
      return idItem;
   }

   public void setIdItem(ItemId id) {
      this.idItem = id;
   }


   @Column(name="des")
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
   
}

@Embeddable
public class ItemId implements Serializable{

   private Integer id;
   private Pedido pedido;
   
   public ItemId(){
      
   }

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name="id_item")
   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }
   
   @ManyToOne
   @JoinColumn(name="id_pedido", referencedColumnName="id_pedido")
   public Pedido getPedido() {
      return pedido;
   }

   public void setPedido(Pedido pedido) {
      this.pedido = pedido;
   }
   

}




Now the only way to get to save an Item was including the @Id in teh ItemId class later I discovered that this property is simply ignored so it will work fine.... I found this out when I tried to get an Item from the database

It seems to me that the only way to get this to work properly is to forget the Primary key class (ItemId) and include Pedido into Item as normal association defining the column as not null to garantee integrity.

Does anybody have know if it is possible to make this work using a primary key class?

Thanx
Ernst


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.