-->
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.  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: how do you create a composite primary key relationship mappi
PostPosted: Mon Dec 18, 2006 1:48 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
how do you create a composite primary key relationship mapping in you hibernate xml files. I have two tables and i want to run the follow sql:

select sale.storeName
from sale sale, storelocation sc
where sale.store_id = sc.store_id
and sale.storeLocId = sc.storeLocId


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 8:11 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Hey,

http://www.hibernate.org/hib_docs/v3/re ... ompositeid

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 3:58 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
i want to do it in the mappings can i do it like this:

but it get errors since the mapping is to the same class


<many-to-one name="store_id "
column="store_id"
class="com.accuserverx.accucharge.dao.hibernate.sale"
not-null="true" />

<many-to-one name="storeLocId "
column="storeLocId "
class="com.accuserverx.accucharge.dao.hibernate.sale"
not-null="true" />


Hey batman give me credit as well...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 4:03 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
many-to-one requres all the columns of the parent's key. so if you have 2 attributes in your composite-key, you need to reference back 2 columns in the many-to-one.

Otherwise your many-to-one could actually return more than one parent.

Code:
<many-to-one name="sale "
column="store_id"
column="storeLocId"
class="com.accuserverx.accucharge.dao.hibernate.sale"
not-null="true" />

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 5:13 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
suzie wrote:
Hey batman give me credit as well...

Sorry, I can't. Only the poster (you on this case) has access to the vote... Helpers can only receive credits. Try to help some others reading their post, you'll that there's nowhere to click like in this thread.

kochcp wrote:
Otherwise your many-to-one could actually return more than one parent.

<many-to-one name="sale "
column="store_id"
column="storeLocId"
class="com.accuserverx.accucharge.dao.hibernate.sale"
not-null="true" />

I totally agree about referencing all the columns of the composite key, but are you sure it's possible to use more than one column attribute? I thought it was not possible, first because XML would not permit it. Well, however I'm sure something like this one would work :
Code:
<many-to-one name="sale" class="com.accuserverx.accucharge.dao.hibernate.sale" not-null="true">
<column name="store_id" />
<column name="storeLocId" />
</many-to-one>

At least, that's the syntax used in the ref doc : http://www.hibernate.org/hib_docs/v3/re ... ompositeid

HTH

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 5:23 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
yeah, you're right about the xml syntax, I wasn't paying attention. just copying and pasting.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 6:56 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
it tried this
<many-to-one name="salePK"
column="store_id"
column="storeLocId"
class="com..dao.hibernate.sale"
not-null="true" />


but i get an error


Error while initializing hibernate: Could not read mappings from resource: /com//dao/hibernate/Sales.hbm.xml


since the column attribute is defined twice in the above and is invalid...













<hibernate-mapping>
<class
name="org.example.composite.SalesLocation"
table="saleLocation">

<!-- applicationPK is another class the implements Serializable -->

<composite-id
name="salePK"
class="org.example.composite.ApplicationPK" >

<key-property name="store_id" column="store_id" type="string" />
<key-property name="storeLocId" column="storeLocId" type="integer"/>

</composite-id>

</class>

</hibernate-mapping>


Here is an example of the Composite key class
public class Application {
private ApplicationPK applicationPK;
//declare other properties
//declare constructor
}
public class ApplicationPK implements Serializable {

//declare composite properties
// declare constructor

}

How to call the query
Note you need to set the required setter method in the composite key class
ht.load(Application.class,applicationPK)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 2:46 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Suzie, please read my answer to what kochcp said. There is a mistake in what he gave, use the following form instead:
Code:
<many-to-one name="sale" class="com.accuserverx.accucharge.dao.hibernate.sale" not-null="true">
<column name="store_id" />
<column name="storeLocId" />
</many-to-one>


And please, please use the code tags, above the edit window, so as to make you more readable.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 1:13 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
Code:
<class name="OrderLine">
   
    <composite-id name="id" class="OrderLineId">
        <key-property name="lineId"/>
        <key-property name="orderId"/>
        <key-property name="customerId"/>
    </composite-id>
</class>
Now, any foreign keys referencing the OrderLine table are also composite.

<many-to-one name="orderLine" class="OrderLine">
<!-- the "class" attribute is optional, as usual -->
    <column name="lineId"/>
    <column name="orderId"/>
    <column name="customerId"/>
</many-to-one>



so what will my getter and setters be in my java bean will they just be for
"id" and "OrderLine" or the columns with in the composite id?


Top
 Profile  
 
 Post subject: org.hibernate.ObjectNotFoundException: No row with the given
PostPosted: Thu Dec 21, 2006 1:40 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
Below is my composite key in my StoreLocation class

StoreLocation Class contains --->
Code:
    <composite-id name="id" class="StoreLocation">
        <key-property name="store_id"/>
        <key-property name="storeLocId"/>
    </composite-id>

and I added a getter and setter for "id" in my StoreLocation java bean



and my Sales Class I have the below since this is joining to the StoreLocation class with the composite key

Store Class contains--->
Code:
<many-to-one name="storelocation" class="StoreLocation">
    <column name="store_id"/>
    <column name="storeLocId"/>
</many-to-one>



and I added a getter and setter for "storelocation" attribute in my

but i get an error:


org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.dao.StoreLocation#com.dao.StoreLoaction@1f2c60d]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 2:12 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
batmat wrote:
Suzie, please read my answer to what kochcp said. There is a mistake in what he gave, use the following form instead:
Code:
<many-to-one name="sale" class="com.dao.hibernate.sale" not-null="true">
<column name="store_id" />
<column name="storeLocId" />
</many-to-one>


And please, please use the code tags, above the edit window, so as to make you more readable.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 2:33 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
batmat I did what you said if you see my posting above but I get an error in the getter and setter... I am not sure what I should have in javabean for the composite key... since I get the error....

org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.dao.StoreLocation#com.dao.StoreLoaction@1f2c60d]

org.hibernate.ObjectNotFoundException.throwIfNull(ObjectNotFoundException.java:27)
org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:128)
org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:161)
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:889)
org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:857)
org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:266)
org.hibernate.type.EntityType.resolve(EntityType.java:303)
org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
org.hibernate.loader.Loader.doQuery(Loader.java:717)
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
org.hibernate.loader.Loader.doList(Loader.java:2150)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
org.hibernate.loader.Loader.list(Loader.java:2024)
org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:298)
org.hibernate.impl.CriteriaImpl$Subcriteria.list(CriteriaImpl.java:143)
com.web.SearchSalesAction.execute(Unknown Source)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

which is not due to the column since I put them as you said above


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 3:11 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
It's just saying that it can't find the row in the db you're asking for. Are you sure the instance you're requesting in the db? Did you valued both keys of your javabean before trying to retrieve it ?

Quote:
I am not sure what I should have in javabean for the composite key... since I get the error....

According to this mapping:
Code:
<many-to-one name="sale" class="com.dao.hibernate.sale" not-null="true">
<column name="store_id" />
<column name="storeLocId" />
</many-to-one>

your javabeans should have set/getStore_id() and set/getStoreLocId() methods.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 4:04 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
i get the same error if i do that for the mapping

<many-to-one name="sale" class="com.dao.hibernate.sale" not-null="true">
<column name="store_id" />
<column name="storeLocId" />
</many-to-one>


as if dont have a getter and setter for sale... all these combinations dont work and when i remove the composite key and

i have the mapping now i get result for this but not for composite...

<many-to-one name="sale" class="com.dao.hibernate.sale" not-null="true">
<column name="store_id" />
</many-to-one>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 9:49 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
it didnt work batmat


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 20 posts ]  Go to page 1, 2  Next

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.