-->
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: NullObject pattern
PostPosted: Fri Sep 05, 2003 12:17 pm 
Newbie

Joined: Fri Sep 05, 2003 12:06 pm
Posts: 4
I have some null objects and I would like Hibernate to return these when it cannot find any results from a given query.

For example say I have this code

public User getUser(Long id) {
return (User) session.load(UserImpl.class, id);
}

if this user with ID=1 does not exist, I would like it to return me the null object I specified somewhere in my system instead of returning null.

This null object could be in my User interface. i.e.

public interface User {
public static final NULL = new User() {};
}

Does Hibernate support something like this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 12:25 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
if not, you could write that

public User getUser(Long id) {
User u = (User) session.load(UserImpl.class, id);
if (u == null) return new User();
return u;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 12:38 pm 
Newbie

Joined: Fri Sep 05, 2003 12:06 pm
Posts: 4
I could do that but I like to know if Hibernate can do this behind
the scenes.

Now let say if I wanted to return a list of Users, it would be a pain
to iterate through the list of users, finding out which is null, and inserting that index column to reference the NULL object.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 12:46 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
did you look at the session.filter()?

are you using a list i.e. getUsers() maybe in your Role.getUsers() and you're getting empty rows? I noticed this myself in my m:m mappings, where if I delete an entity, then the cascading db constraint deletes the mapping. The net result is that the list has nulls. I think it's cause the list routine in hibernate expects everything nice and orderly in the index column with no gaps from 0 to n in the list. I am still debating the best way to deal with this. Currently, when using one of these lists, I iterate over the list and simply continue if the next element is null. I'm considering some sort of after delete trigger on the db, but I have not fully poured through the docs for a better way to get an ordered set back.


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.