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.