-->
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.  [ 2 posts ] 
Author Message
 Post subject: Generic findChildren API for 1-M relationship?
PostPosted: Thu Apr 19, 2007 12:57 pm 
Newbie

Joined: Thu Apr 19, 2007 12:32 pm
Posts: 2
Hi All,

I am just starting out on EJB3/hibernate and am having trouble implementing a generic findChildren API.

What I'd like is an API that looks something like this

public List findChildren(SomeEntity parent, String childrenProperty)


I do realize, we can do parent.getChildren() but I want a more dynamic way to get children of a given entity.


For eg. Given the following schema,

PARENT
==========
PARENT_ID PK


CHILD
=======
CHILD_ID PK
CHILD_NAME
PARENT_ID FK

I should be able call

List childList = HibernateAPI.findChildren(parent, "children");


Any help is appreciated.
Thanks,
Sridhar.


Top
 Profile  
 
 Post subject: This is what I have so far...
PostPosted: Thu Apr 19, 2007 4:26 pm 
Newbie

Joined: Thu Apr 19, 2007 12:32 pm
Posts: 2
@SuppressWarnings("unchecked")
public List findChildren(Object parent, String children) {

SingleTableEntityPersister parentMetadata = (SingleTableEntityPersister) HibernateService.getSessionFactory().getClassMetadata(parent.getClass());
Object parentId = parentMetadata.getIdentifier(parent, EntityMode.POJO);
String parentTableName = parentMetadata.getConstraintOrderedTableNameClosure()[0];

Class childClass = null;
CollectionMetadata md = HibernateService.getSessionFactory().getCollectionMetadata(parent.getClass().getName()+"."+children);
if (md != null && md instanceof OneToManyPersister) {
OneToManyPersister otm = (OneToManyPersister) md;
SingleTableEntityPersister childMetadata = (SingleTableEntityPersister) otm.getElementPersister();
childClass = childMetadata.getConcreteProxyClass(EntityMode.POJO);
}
else {
throw new RuntimeException(parentTableName + " is not related(? to many) to " + children);
}


Criteria crit = getSession()

.createCriteria(childClass)
.createCriteria(parentTableName).add(Restrictions.idEq(parentId))
;

return crit.list();

}


Has tons of holes and does not handle join tables.


Anyone has better ideas?...Anyone?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.