-->
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.  [ 4 posts ] 
Author Message
 Post subject: Possible limitation with Hibernate 3.0 collections?
PostPosted: Sun Apr 10, 2005 9:48 pm 
Newbie

Joined: Sun Jan 30, 2005 9:43 pm
Posts: 16
I'm using Hibernate version 3.0 Final.

I'm new to Hibernate but in the past I've built an O/R mapping layer for another product that supports a feature I've not seen in Hibernate 3.0 - or at least not yet figured out how to use. So this is either a suggestion or a request for advice.

What I think is lacking in Hibernate is that all collections appear to require a foreign key; in other words they must be part of an association. Right?

What I want to do is to be able to navigate an entire object model via a single top level 'Model' class that exposes a small number of collections (much like foriegn key association collections) that span an entire table (no foreign key filter). For example, suppose we have User and Group objects and a bidirectional association between them. The User and Group classes both have Id and Name properties. Our Model class might expose Users and Groups Map collections indexed by their Name. We can use these collections to Add/Remove User and Group objects and find them by Name.

Like their foreign key linked cousins, I use these top level collections to add/remove and retrieve elements (typically via a Map collection key) the difference being that they control an entire table rather than just a subset of objects filtered by a foriegn key. The Model class gives the illusion that the entire object model is in memory. It works because the O/R mapping engine is smart enough to not retrieve the entire collection - particularly when only a single element is requested (lazy loading). Hibernate's support for paginated loading futher mitigates any performance concern with exposing an enitire table as a collection.

The single Model class can in some circumstances replace or at least reduce the number of traditional DAO layer classes.

Code:
class Model
{
    private Map users;
    private Map groups;

    // Get/Set ALL users in the database indexed by User.getName()
    public Map getUsers() { return users; }
    private void setUsers(Map users) { this.users = users; }

    // Get/Set ALL groups in the database indexed by Group.getName()
    public Map getGroups() { return groups; }
    private void setGroups(Map users) { this.groups = groups; }
}

class User
{
    private Long id;
    private String name;
    private Map memberOfGroups;

    public Long getId() { return id; }
    private void setId(Long id) { this.id = id; }

    public String getName() { return name; }
    private void setName(String name) { this.name = name; }

    // Map collection link via UserId foriegn key
    // Get/Set groups of this user indexed by Group.getName()
    public Map getMemberOfGroups() { return memberOfGroups; }
    private void setMemberOfGroups(Map memberOfGroups) { this.memberOfGroups = memberOfGroups; }
}

class Group
{
    private Long id;
    private String name;
    private Map groupMembers;

    public Long getId() { return id; }
    private void setId(Long id) { this.id = id; }

    public String getName() { return name; }
    private void setName(String name) { this.name = name; }

    // Map collection link via GroupId foriegn key
    // Get/Set groups of this user indexed by User.getName()
    public Map getGroupMembers() { return groupMembers; }
    private void setGroupMembers(Map groupMembers) { this.groupMembers = groupMembers; }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 12, 2005 8:59 pm 
Newbie

Joined: Sun Jan 30, 2005 9:43 pm
Posts: 16
Well it looks like the biggest risk with Hibernate is no community support.

I've spent considerbale time preparing two carefully thought out postings only to get no responses. I can only assume that there are too many people asking and not enough replying.

This now appears to be a much greater limitation.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 12, 2005 9:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
http://www.hibernate.org/SupportTrainin ... rHibernate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 12, 2005 9:39 pm 
Newbie

Joined: Sun Jan 30, 2005 9:43 pm
Posts: 16
Thanks Gavin. I know about the commercial support options for OS software. That's not my point. Unfortnately I don't have this luxury.

Of more use would be a reason why no one bothered to read/reply ... or even better some useful feedback on the original posting. Perhaps it was just too left field for anyone to bother understanding it?

My current solution is to add a new top level Model table & class (Yuk!) and add an unnecessary foriegn key ModelId to top level tables (double Yuk!). The only bonus is that I can easily cascade delete Models during testing. Hibernate is very close to this clean solution but I think that most programmers are still in the mindset that you always need a DAO layer. I think that a small extention to Hibernate could greatly reduce or in some cases remove this layer. You just need a Model instance bound to a Session and you're in business.

I've inspected the source code for the collection classes and a variation on org.hibernate.persister.collection.BasicCollectionPersister looks promising for implementing this feature. However, I'm too green to Hibernate to attempt it.

Cheers,
Tony.


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