Hibernate version: 3.2.0cr1
with ejb3 and annotations in a Resource Local config
Im having trouble expressing our db mapping in Entity Beans. We have the following tables:
Code:
products
product_id (pk)
product_master (fk)
...
product_masters
product_master_id
original_product_id (fk to products)
...
As you can see the product has a reference to the master. All products created in the system have masters although you may later switch the master it is pointing to. Neither products or masters are ever deleted from the system. The original product id should always point to the one that was inserted. This means that all of these relationships are not nullable.
Mapping documents:Here's what I'm trying to do although I cant seem to get the right annotations for it to work:
Code:
@Entity
@Table(name = "products")
@SequenceGenerator(name = "products_seq", sequenceName="products_seq")
Product {
private ProductMaster productMaster;
private ProductMaster originalProductMaster;
...
protected Product(){}
public Product() {
ProductMaster master = new ProductMaster(this);
setProductMaster(master);
setOriginalProductMaster(master);
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="products_seq")
@Column(name = "product_id")
public Long getProductId() {
return productId;
}
...
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "PRODUCT_MASTER_ID")
public ProductMaster getProductMaster() {
return productMaster;
}
...
@NotNull
@OneToOne(fetch=FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name="product_id", updatable=false)
public ProductMaster getOriginalProductMaster() {
return originalProductMaster;
}
...
}
Code:
@Entity
@Table(name = "product_master")
ProductMaster {
private Long productMasterId;
private Product originalProduct;
private List<Product> products;
...
protected ProductMaster(){}
public ProductMaster(Product p) {
setOriginalProduct(p);
products.add(p);
}
...
@Id
@Column(name = "product_master_id")
@GeneratedValue(strategy=GenerationType.AUTO, generator="product_masters_seq")
public Long getProductMasterId() {
return productMasterId;
}
...
@NotNull
@ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name="original_product_id", updatable=false)
public Product getOriginalProduct() {
return originalProduct;
}
...
@NotNull
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "PRODUCT_MASTER_ID")
public List<Product> getProducts() {
return products;
}
....
}
Full stack trace of any exception that occurs:
Hibernate: select product_masters_seq.nextval from dual
Hibernate: select products_seq.nextval from dual
2006-04-27 10:13:46,437 [main] ERROR com.sonymusic.aoma.metadata.commands.ImportXMLCommand - javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.sonymusic.aoma.persistence.beans.Product.productMaster
...
Caused by: javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.sonymusic.aoma.persistence.beans.Product.productMaster
_____ 2006-04-27 10:13:46,453 [main] ERROR com.sonymusic.aoma.metadata.HotFolderImportDaemon - at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:562)
_____ 2006-04-27 10:13:46,453 [main] ERROR com.sonymusic.aoma.metadata.HotFolderImportDaemon - at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:193)
Name and version of the database you are using: Oracle 9
Any help is greatly appreciated