Hi,
I was wondering if anyone my take the time to consider the possible solutions for the following problem. Consider this (edited) class.
/**
* @hibernate.class
*/
public class CircuitComponent extends Persistent {
private Product product;
/**
* @hibernate.many-to-one type="com.xxxx.model.Product" not-null="true"
* @return Returns the product.
*/
public Product getProduct() {
return product;
}
/**
* @param product The product to set.
*/
public void setProduct(Product product) {
this.product = product;
}
}
Essentially, the Product member of CircuitComponent indicates the "type" of CircuitComponent. A CircuitComponent can be thought of as an actual physical instance of a particular Product. My problem is this:
Clearly, I have a many-to-one relationship and this is very straight forward to model. However, when saving/adding a new CircuitComponent I want to ensure that that the Product (type) object already exists in the database and is not itself an "unsaved" object which results in hibernate saving a new Product as well when simply calling "session.save(circuitComponent)"
What is the best way to go about enforcing this relationship? Should my dao or manager class be checking to see whether the Product exists before trying to commit a new CircuitComponent object? Or is there a way to enforce this in the mapping/XDoclet so that hibernate will only save the new CircuitComponent object if the Product object already exists?
Ideally I'd like to do both.
Hibernate version: 2.x
DB: MySQL 4.1
|