-->
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: What is the best practice to avoid
PostPosted: Tue May 06, 2008 12:16 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
We have a Customer bean as:

Code:
public class Customer{
  private Set roles = new HashSet();
  public Set getRoles() { return roles;}
}


In business controller,
Code:
Customer c = ...//getCustomer() from database
Set roles = c.getRoles(); //this works fine
Set roles = (Set)c.getRoles(); //this works fine
Set roles = (HashSet)c.getRoles(); //throws an exception java.lang.ClassCastException: org.hibernate.collection.PersistentSet


What is the best practice If I want pass Set roles to UI layer as HashSet. My two options are:

a. return getRoles as HashSet in Customer class
Code:
public class Customer{
  private Set roles = new HashSet();
  public Set getRoles() { return new HashSet(roles);}
}


b. Return a new HashSet for c.getRoles() wherever required.
Code:
Set roles = new HashSet(c.getRoles());


I don't want to call c.getRoles() as it is from UI layer as it returns implementation of org.hibernate.collection.PersistentSet and I have to include hibernate.jar in UI layer.

Let me know the best practice.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 1:47 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Both look bad to me, but the least bad would be b).

However: why do you need to use a concrete implementation (HashSet) at all as a left-side member in your code?
That suggests me you want to directly manipulate that hashSet somehow, and I would strongly advice you against that.
Hibernate, for the most part, doesn't know what you do to that hashSet once you "disconnected" it from its parent object!

I hate providing one of those smug answers that divert your question instead of answer it specifically, but I really advise you to consider

1) why you need such "disconnected" collection at all
2) If you really have to use it, why does it need to be a concrete implementation, instead of just a Set.

_________________
Gonzalo Díaz


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.