@EmbeddedID - JPA
lekkSep 22 2006 — edited Sep 25 2006Hello!!
I'm trying to map a relationship 1-N (Product(1)/Item(N)) and I'm getting an error when i
insert the product.
Error: Cannot insert the value NULL into column ITEM_PRODUCT.CD_PRODUCT.
It sounds like I'm missing something, specially in my composite key.
Please, could someone take a look and try to identify anything wrong?
I did many tests already, but... :(( nooo good
Code:
Produto.class (Product)
@Id
@Column(name = "CD_PRODUTO", nullable = false)
@SequenceGenerator(name="SqProduto",sequenceName="SQ_PRODUTO", allocationSize=20)
@GeneratedValue(strategy=javax.persistence.GenerationType.SEQUENCE, generator="SqProduto")
private Long cdProduto;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "produto")
private java.util.Collection <entity.ItemProduto> itemProdutoCollection;
ItemProduto.class (ItemProduct)
@EmbeddedId
protected ItemProdutoPK ItemProdutoPK;
@JoinColumn(name = "CD_PRODUTO", referencedColumnName = "CD_PRODUTO", insertable=false, updatable=false)
@ManyToOne
private Produto produto;
ItemProdutoPK.class
@Id
@Column(name = "CD_PRODUTO", nullable = false)
private int cdProduto;
@Id
@Column(name = "CD_ITEM_PRODUTO", nullable = false)
@SequenceGenerator(name="SqItemProduto",sequenceName="SQ_ITEM_PRODUTO", allocationSize=20)
@GeneratedValue(strategy=javax.persistence.GenerationType.SEQUENCE, generator="SqItemProduto")
private int cdItemProduto;
Test.class
Produto p = new Produto();
p.setDsProd("p1");
ItemProduto ip = new ItemProduto();
ip.setDsItem("item1");
ip.setProduto(p);
p.getItemProdutoCollection().add(ip);
ip = new ItemProduto();
ip.setDsItem("item2");
ip.setProduto(p);
p.getItemProdutoCollection().add(ip);
em.persist(p); // ERROR
Thanks a lot
!_Let's share ideas_!