-->
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.  [ 5 posts ] 
Author Message
 Post subject: queries for a table having defined Discriminator fetches all
PostPosted: Thu Nov 20, 2003 9:43 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 2:47 pm
Posts: 21
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 9:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You need to add a where attribute to your collection mappings.

Personally I believe strongly that this is an evil non-use of polymorphism. Not good OO design.


Top
 Profile  
 
 Post subject: Hierarchical mapping
PostPosted: Fri Nov 21, 2003 8:39 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 2:47 pm
Posts: 21
I am in the process of migrating an existing application and am having the constraint of not making too many changes to the existing object model. Hence I was experimenting on the subclass and found out this. I had gone through the documentation on joined-subclass. I am not sure how much of this will help me.

thanks,
TM

_________________
TM


Top
 Profile  
 
 Post subject: where attribute in the collection
PostPosted: Fri Nov 21, 2003 10:07 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 2:47 pm
Posts: 21
Can I use where attribute for the collection of type set. I could not find documentation for this. Could some one provide the pointer to the documentation or let me know what is the format of mapping file declaration.

thanks,
TM

_________________
TM


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 9:51 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
DTD is the best doc for that
http://www.hibernate.org/26.html

_________________
Emmanuel


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