-->
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: Hibernate uniderictional associations with parents ID
PostPosted: Fri Oct 28, 2011 2:44 pm 
Newbie

Joined: Fri Oct 28, 2011 12:55 pm
Posts: 3
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


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.