-->
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: Best Way to model a many-to-one relationship question-Newbie
PostPosted: Tue Jul 05, 2005 6:52 am 
Newbie

Joined: Tue Jul 05, 2005 6:32 am
Posts: 1
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


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.