-->
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.  [ 7 posts ] 
Author Message
 Post subject: Relationship mapping
PostPosted: Wed Dec 21, 2005 4:38 pm 
Newbie

Joined: Wed Dec 21, 2005 4:03 pm
Posts: 11
Location: Pittsburgh
Hibernate version:3.1

How do you define a many-to-many relationship with additional restraints?
Here is my example:

Tables: People (columns - id, name, etc.), Relationship (columns - id1, id2, type_id, status, etc.), Relationship_Type (columns - type_id, type_name))

Examples of two rows in the People table:
1, john
2, paul

Examples of a row in the Relationship table:
1,2,1,"inactive"

Examples of a row in the Relationship_type table:
1,band_members

In the example.People.java file
I'll have a property of
...
private Set bandMembers;
...

In the example.People.hbm.xml file I can map

<set name="bandMembers" sort="unsorted" table="Relationship"
where="status='inactive'">
<key column="id1"/>
<many-to-many class="example.People"
column="id2" unique="false"/>
</set>

My questions are
1) Is this correct for getting all inactive members for a people object?
2) How do I integrate table Relationship_type into this setting so that I can get inactive band members for a People object? Do I have to do in the where clause as well?

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Relationship mapping
PostPosted: Thu Dec 22, 2005 6:23 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
1) Is this correct for getting all inactive members for a people object?
Yes it is. It's the way hibernate documents filtering of collections.
2) How do I integrate table Relationship_type into this setting so that I can get inactive band members for a People object? Do I have to do in the where clause as well?

That one is more tricky :D
I'll recommend not to do it in mapping file. If you want all People, linked with the pair type = "Band_Members", "status=Inactive", then do it with a hibernate query in your code, using session.createQuery().
This is because mapping is something static and you are adressing something not static in you where clause (what if someone decide while using application he'll rename Band_Members to "Members of Same Band"? You db shema will allow it and i think your code will to, or you wouldn't have extracted that String in a separate table)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 8:45 am 
Newbie

Joined: Wed Dec 21, 2005 4:03 pm
Posts: 11
Location: Pittsburgh
Thanks for your reply.

I do have a followup question regarding the where clause in the hbm mapping file. The document says it can be any sql statement. Should it be hql statement?

So I guess it's better to use straightforward queries if the relationship itself has properties. That means I have to define object class for the relationship as well, something I hate to do because it means composite id stuff unless there are other means that I'm not aware of (I guess one can add an id column to the Relationship table, but it is a rare practice).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 9:02 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
ZZTops wrote:
I do have a followup question regarding the where clause in the hbm mapping file. The document says it can be any sql statement. Should it be hql statement?

I don't think. If it behaves like formula, it's database specific SQL, though i suppose hibernate entity are allowed in it. The best answer is 'give it a try'

ZZTops wrote:
So I guess it's better to use straightforward queries if the relationship itself has properties. That means I have to define object class for the relationship as well, something I hate to do because it means composite id stuff unless there are other means that I'm not aware of (I guess one can add an id column to the Relationship table, but it is a rare practice).


Relationships with properties is always quite cumbersome to manage, it's like RelationShip Table which manage relation between 3 tables :)
I think the best is to think how you dispose you object before you think how you create database, unless the database is a forced one.

However, if you need to edit in your application the properties of the relation, you have no other choice then creating an Object for the relationShip. And instead of having People object storing People Sets, you'll have People Object storing Relations Sets(each relation having a type, a status, etc).


Top
 Profile  
 
 Post subject: The problem continues with a different flavour
PostPosted: Thu Dec 29, 2005 11:30 pm 
Newbie

Joined: Wed Dec 21, 2005 4:03 pm
Posts: 11
Location: Pittsburgh
Here is my setting with slight improvement:

Tables: People (columns - id, name, etc.), Relationship (columns - id1, id2, type_id, status, etc.), Relationship_Type (columns - type_id, type_name))

Examples of two rows in the People table:
1, john
2, paul

Examples of a row in the Relationship table:
1,2,1,"inactive"

Examples of a row in the Relationship_type table:
1,band_members

In the example.People.java file
I'll have a property of
...
private Set relations;
...

1) I added an example.Relationship_type.java class
mapping to the Relationship_type table, along with the example.Relationship_type.hbm.xml.

2) I added a utility java class as individual members of relations
example.Relation.java file
...
private People person;
private String status;
private Relationship_type relationshipType;
...
and getter/setters

In the example.People.hbm.xml file I now map

<set name="relations" sort="unsorted" table="Relationship" >
<key column="id1"/>
<composite-element class="example.Relation">
<many-to-one name="relationshipType" column="type_id" class="example.Relationship_type"/>
<many-to-many name="person" column="id2" class="example.People"/>
<property name="status"/>
</composite-element>
</set>

It compiles but bombs out in run time with a null pointer error. When I look into the log, it is in a Hibernate class of getting proxyFactory. By the debug info spitted out from Hibernate, I can see it actually built a correct sql for querying the Relationship table, and then went for loading example.Relationship_type class instance. Then there is a whole bunch of session closing debug information before the null pointer error.

This is where I'm stuck now.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 11:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp




Full stack trace of any exception that occurs:


Top
 Profile  
 
 Post subject: Here is the exception that I'm talking about
PostPosted: Fri Dec 30, 2005 9:48 am 
Newbie

Joined: Wed Dec 21, 2005 4:03 pm
Posts: 11
Location: Pittsburgh
... 17 more
Caused by: java.lang.NullPointerException
at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3120)
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:232)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:173)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:812)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:782)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:266)
at org.hibernate.type.EntityType.resolve(EntityType.java:303)
at org.hibernate.type.ComponentType.resolve(ComponentType.java:528)
at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:229)
at org.hibernate.persister.collection.AbstractCollectionPersister.readElement(AbstractCollectionPersister.java:644)
at org.hibernate.collection.PersistentSet.readFrom(PersistentSet.java:262)
at org.hibernate.loader.Loader.readCollectionElement(Loader.java:994)
at org.hibernate.loader.Loader.readCollectionElements(Loader.java:635)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:579)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1916)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:71)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1493)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:454)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:791)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:228)
at org.hibernate.loader.Loader.doList(Loader.java:2147)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2026)
at org.hibernate.loader.Loader.list(Loader.java:2021)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:298)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:137)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1014)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at com.fedex.ground.facility.model.dao.hibernate.WorkInternalOrgDAOImpl$1.doInHibernate(WorkInternalOrgDAOImpl.java:56)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:365)
at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:337)
at com.fedex.ground.facility.model.dao.hibernate.WorkInternalOrgDAOImpl.getWorkOrgs(WorkInternalOrgDAOImpl.java:46)
at com.fedex.ground.facility.model.service.spring.OrgServiceImpl.getOrdList(OrgServiceImpl.java:66)
at com.fedex.ground.facility.view.bean.FacilityListBean.searchFacilities(FacilityListBean.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
... 18 more


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

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.