Hi All,
I am new to hibernate and trying to figure out how 1-to-1 and 1-to-many relationships can be represented in hibernate config, if I want to put the parent id in child row without child object storing a reference to parentId. Let me explain with an example: Lets say I have an object model as below:
public class Stock { private Integer stockId; private String stockCode; private String stockName; private StockDetail stockDetail; //getters and setters }
public class StockDetail { private String compName; private String compDesc; //getters and setters }
Here, I think there is a unidirectional 1-to-1 association between Stock and StockDetail objects, and StockDetail does not have a reference back to Stock.
I want to store this in the following tables:
Stock --------- stockId (PK) stockCode stockName
and
StockDetail ------------ stockId (PK) [Foreign key to Stock table too, so the stockId is the same as the stockId in the Stock table for a Stock Object] compName compDesc
I tried to get the stockId in StockDetail table from Stock Object, but am not sure how it can be done. Here is the hibernate config I have:
<hibernate-mapping> <class name="stocks.Stock" table="Stock"> <id name="stockId" type="java.lang.Integer"> <column name="STOCK_ID" /> <generator class="assigned" /> </id> <property name="stockCode" type="string" column="stockCode"/> <property name="stockName" type="string" column="stockName"/> <one-to-one name="stockDetail" class="stocks.StockDetail" cascade="save-update"></one-to-one> </class> <class name="stocks.StockDetail" table="StockDetail"> <id name="stockId" type="java.lang.Integer"> <column name="stockId" /> <generator class="foreign"> <param name="property">stock</param> </generator> </id> <property name="compName" column="compName"/> <property name="compDesc" column="compDesc"/> </class> </hibernate-mapping>
I am getting the following error: "Could not find a getter for stockId in class stocks.StockDetail". I was hoping hibernate would automatically look for stockId property in Stock object due to foreign key constraint. Can someone please help me with getting this working? I am trying to represent the implicit reference connection (between Stock and StockDetail objects) as foreign key in StockDetail table.
Thanks in advance, Malay
|