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