I have the following requirement.
Need to create project.
Need to create enterprise group (set of users - system defined)
Need to create Admin group (set of users - system defined)
Need to create groups (set of users - user defined)
Need to retrieve only groups on some occasions
Need to retrive only Admin groups on some occasions
Need to retrieve only Enterprise groups on some occasions
I have defined the following mapping in the hibernate mapping file.
<class name="net.sf.hibernate.examples.quickstart.Ou" table="ou" dynamic-update="true">
<id name="id" type="string" unsaved-value="null" >
<column name="ouId" sql-type="varchar(255)" not-null="true"/>
<generator class="assigned"/>
</id>
<property name="name" type="string"/>
<set name="Groups" lazy="true" inverse="false">
<key column="ParentOuId"/>
<one-to-many class="net.sf.hibernate.examples.quickstart.Group"/>
</set>
<set name="AdminGroups" lazy="true" inverse="false">
<key column="ParentOuId"/>
<one-to-many class="net.sf.hibernate.examples.quickstart.AdminGroup"/>
</set>
<set name="EnterpriseGroups" lazy="true" inverse="false">
<key column="ParentOuId"/>
<one-to-many class="net.sf.hibernate.examples.quickstart.EnterpriseGroup"/>
</set>
</class>
<class name="net.sf.hibernate.examples.quickstart.Group" table="cwgroup" discriminator-value="G">
<id name="id" type="string" unsaved-value="null" >
<column name="grpId" sql-type="varchar(255)" not-null="true"/>
<generator class="assigned"/>
</id>
<discriminator column="subclass" type="character"/>
<many-to-one name="ParentOu" column="ParentOuId" class="net.sf.hibernate.examples.quickstart.Ou" not-null="true"/>
<subclass name="net.sf.hibernate.examples.quickstart.AdminGroup" discriminator-value="A">
</subclass>
<subclass name="net.sf.hibernate.examples.quickstart.EnterpriseGroup" discriminator-value="E">
</subclass>
</class>
The classes AdminGroup and EnterpriseGroup are subclasses of the class Group.
The issue I am facing:
I have create the project and added all three types of groups to it using the above mapping successfully.
When I tried to get only AdminGroups by calling the function getAdminGroups, I am getting the following exception
[ERROR] PersistentCollection - -Failed to lazily initialize a collection <net.sf.hibernate.WrongClassException: Object with id: 101 was not of the specified subclass: net.sf.hibernate.examples.quickstart.AdminGroup (loaded object was of wrong class)>net.sf.hibernate.WrongClassException: Object with id: 101 was not of the specified subclass: net.sf.hibernate.examples.quickstart.AdminGroup (loaded object was of wrong class)
at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:300)
at net.sf.hibernate.loader.Loader.getRow(Loader.java:278)
at net.sf.hibernate.loader.Loader.doFind(Loader.java:159)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:602)
at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:102)
at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:2898)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:151)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:64)
at net.sf.hibernate.collection.Set.toArray(Set.java:146)
at net.sf.hibernate.examples.quickstart.SampleServlet.selectOuAndGroups(Unknown Source)
at net.sf.hibernate.examples.quickstart.SampleServlet.doGet(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:265)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:200)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:2546)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2260)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:139)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)
net.sf.hibernate.WrongClassException: Object with id: 101 was not of the specified subclass: net.sf.hibernate.examples.quickstart.AdminGroup (loaded object was of wrong class)
at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:300)
at net.sf.hibernate.loader.Loader.getRow(Loader.java:278)
at net.sf.hibernate.loader.Loader.doFind(Loader.java:159)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:602)
at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:102)
at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:2898)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:151)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:64)
at net.sf.hibernate.collection.Set.toArray(Set.java:146)
at net.sf.hibernate.examples.quickstart.SampleServlet.selectOuAndGroups(Unknown Source)
at net.sf.hibernate.examples.quickstart.SampleServlet.doGet(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:265)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:200)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:2546)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2260)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:139)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)
rethrown as net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection: Object with id: 101 was not of the specified subclass: net.sf.hibernate.examples.quickstart.AdminGroup (loaded object was of wrong class)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:163)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:64)
at net.sf.hibernate.collection.Set.toArray(Set.java:146)
at net.sf.hibernate.examples.quickstart.SampleServlet.selectOuAndGroups(Unknown Source)
at net.sf.hibernate.examples.quickstart.SampleServlet.doGet(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:265)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:200)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:2546)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2260)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:139)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)
Shouldn't hibernate retrieve only AdminGroups based on the discrimator value. Do I need to do any thing more. Please advice.
Here is my code:
package net.sf.hibernate.examples.quickstart;
//import net.sf.hibernate.examples.quickstart.Pfolder;
//import java.util.*;
import net.sf.hibernate.collection.Set;
public class Group extends Entity {
public Group() {
}
public String getId() {
return (String)getProperty((Object)"id");
}
public void setId(String id) {
setProperty((Object)"id", id);
}
public String getName() {
return (String)getProperty((Object)"name");
}
public void setName(String name) {
setProperty((Object)"name", name);
}
public Set getOus() {
return (Set) getProperty((Object)"ous");
}
public void setOus(Set ous) {
setProperty((Object)"ous", (Object)ous);
}
public Ou getParentOu()
{
return (Ou)getProperty((Object)"parentOu");
}
public void setParentOu(Ou ou)
{
setProperty((Object)"parentOu", (Object)ou);
}
}
package net.sf.hibernate.examples.quickstart;
//import net.sf.hibernate.examples.quickstart.Pfolder;
import java.util.*;
public class AdminGroup extends Group
{
}
public Ou selectOuAndGroups(ServletOutputStream out, String Id)
throws HibernateException, Exception
{
Ou ou = null;
out.print("<h3>Retrieving OU:</h3>");
String queryString = "select ou from Ou as ou where ou.id = :id";
Query query = session.createQuery(queryString);
query.setString("id", Id);
for (Iterator it = query.iterate(); it.hasNext();)
{
ou = (Ou) it.next();
out.println("User: " + ou.getName() + " (" + ou.getId() + ", " + ")<br/>");
}
net.sf.hibernate.collection.Set grps = (net.sf.hibernate.collection.Set)ou.getGroups();
Object[] objs = grps.toArray();
for ( int i = 0 ; i < objs.length; i++ )
{
net.sf.hibernate.examples.quickstart.Group grp = (net.sf.hibernate.examples.quickstart.Group)objs[i];
out.println("Group: " + grp.getName() + " (" + grp.getId() + ", " + ")<br/>");
}
net.sf.hibernate.collection.Set agrps = (net.sf.hibernate.collection.Set)ou.getAdminGroups();
objs = (Object[])agrps.toArray();
for ( int i = 0 ; i < objs.length; i++ )
{
AdminGroup agrp = (AdminGroup)objs[i];
out.println("AdminGroup: " + agrp.getName() + " (" + agrp.getId() + ", " + ")<br/>");
}
net.sf.hibernate.collection.Set egrps = (net.sf.hibernate.collection.Set)ou.getEnterpriseGroups();
objs = (Object[])egrps.toArray();
for ( int i = 0 ; i < objs.length; i++ )
{
EnterpriseGroup egrp = (EnterpriseGroup)objs[i];
out.println("EnterpriseGroup: " + egrp.getName() + " (" + egrp.getId() + ", " + ")<br/>");
}
return ou;
}
Since we have good number of such Hierarchical mappings, it would help us a lot if we can retrieve only the required type of children.
thanks,
TM
_________________ TM
|