-->
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.  [ 9 posts ] 
Author Message
 Post subject: Subclassing problem
PostPosted: Fri Feb 06, 2004 1:34 pm 
Newbie

Joined: Wed Nov 12, 2003 3:45 pm
Posts: 8
I beg of you, elucidation.

Code:
public abstract class AbstractAction extends PersistedObject
{
   private Date date;
   private User user;
   
   public Date getDate()
   {
      return date;
   }

   public void setDate(Date date)
   {
      this.date = date;
   }

   public User getUser()
   {
      return user;
   }

   public void setUser(User user)
   {
      this.user = user;
   }
}

<class name="collective.mc.AbstractAction" table="VersionActions" discriminator-value="X">
   <id name="id" unsaved-value="any">
      <generator class="native" />
   </id>
   <discriminator column="actionType" />
   <property name="date" />
   <many-to-one name="user" class="collective.instance.User" column="user_id" />
   <subclass name="collective.mc.ViewAction" lazy="true" discriminator="V" />
   <subclass name="collective.mc.EditAction" discriminator-value="E" lazy="true">
      <property name="comment" />
   </subclass>
   <subclass name="collective.mc.ApproveAction" discriminator-value="A" lazy="true" />
</class>

public class Version
{
   private List approveActions = new ArrayList();
   private List editActions = new ArrayList();
   private List viewActions = new ArrayList();
}

<class name="collective.mc.Version" table="Versions">
   <id name="id" unsaved-value="any">
      <generator class="native" />
   </id>
   <list name="approveActions" lazy="true" cascade="all">
      <key column="version_id"/>
      <index column="idx"/>
      <one-to-many class="collective.mc.ApproveAction"/>
   </list>
   <list name="approveActions" lazy="true" cascade="all">
      <key column="version_id"/>
      <index column="idx"/>
      <one-to-many class="collective.mc.ApproveAction"/>
   </list>
   <list name="approveActions" lazy="true" cascade="all">
      <key column="version_id"/>
      <index column="idx"/>
      <one-to-many class="collective.mc.ApproveAction"/>
   </list>
</class>


I have three loops in my presentation (using Tapestry), one for each type of action for a given version, used to display a version's history. It works when there are no actions for a version. If I view the version (add a ViewAction to the viewActions list in Version), it breaks on trying to loop through the edit and approve lists, claiming that object id 1 was not the right type of object.

Basically, object with id 1 is an entry in the Actions table with a discriminator of V, or a ViewAction. The calls to getApproveActions() in Version end up trying to return all the entries in this database, as opposed to all the entries with the discriminator A, and so on for the other types of actions.

If I say that the one-to-many class for each list is collective.mc.AbstractAction, I don't get any exceptions, but each of the lists in Version have all actions in them.

What's going on? I thought I did this properly, but it doesn't seem to do what I expect.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 1:48 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This are not your real mappings. Please show the real ones.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 1:58 pm 
Newbie

Joined: Wed Nov 12, 2003 3:45 pm
Posts: 8
Sorry, I forgot to edit the Version mapping file lists. Did you really need the doctype info and other extra stuff? Anyway, here's the fixed ones.

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
   <class name="collective.mc.AbstractAction" table="VersionActions" discriminator-value="X">
      <id name="id" unsaved-value="any">
         <generator class="native" />
      </id>
      <discriminator column="actionType" />
      <property name="date" />
      <many-to-one name="user" class="collective.instance.User" column="user_id" />
            
      <subclass name="collective.mc.ViewAction" discriminator-value="V" lazy="true" />
      <subclass name="collective.mc.EditAction" discriminator-value="E" lazy="true">
         <property name="comment" />
      </subclass>
      <subclass name="collective.mc.ApproveAction" discriminator-value="A" lazy="true" />
   </class>
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
   <class name="collective.mc.Version" table="Versions">
   
      <id name="id" unsaved-value="any">
         <generator class="native" />
      </id>
      
      <list name="approveActions" lazy="true" cascade="all">
         <key column="version_id" />
         <index column="idx" />
         <one-to-many class="collective.mc.ApproveAction" />
      </list>

      <list name="editActions" lazy="true" cascade="all">
         <key column="version_id" />
         <index column="idx" />
         <one-to-many class="collective.mc.EditAction" />
      </list>

      <list name="viewActions" lazy="true" cascade="all">
         <key column="version_id" />
         <index column="idx" />
         <one-to-many class="collective.mc.ViewAction" />
      </list>
   
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 2:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please show the log including the generated SQL and the exception.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 3:16 pm 
Newbie

Joined: Wed Nov 12, 2003 3:45 pm
Posts: 8
Hibernate: select editaction0_.id as id__, editaction0_.version_id as version_id__, editaction0_.idx as idx__, user1_.id as id0_, user1_.username as username0_, user1_.hashedPassword as hashedPa3_0_, user1_.enabled as enabled0_, user1_.pageSize as pageSize0_, user1_.sort as sort0_, user1_.clearCart as clearCart0_, user1_.forcePasswordChange as forcePas8_0_, user1_.receiveRegistrationEmail as receiveR9_0_, user1_.receiveOrderApprovalEmail as receive10_0_, user1_.expirationDate as expirat11_0_, user1_.defaultApplication as default12_0_, user1_.locale as locale0_, user1_.workingOrder as working14_0_, user1_.address as address0_, order2_.id as id1_, order2_.datePlaced as datePlaced1_, order2_.expireDate as expireDate1_, order2_.note as note1_, order2_.chargeNumber as chargeNu5_1_, order2_.orderSequenceId as orderSeq6_1_, order2_.approved as approved1_, order2_.denied as denied1_, order2_.cart as cart1_, order2_.delivery as delivery1_, order2_.shippingAddress as shippin11_1_, order2_.creator_id as creator_id1_, order2_.initialUser_id as initial13_1_, cart3_.id as id2_, cart3_.name as name2_, cart3_.description as descript3_2_, cart3_.owner_id as owner_id2_, delivery4_.id as id3_, delivery4_.name as name3_, delivery4_.description as descript3_3_, delivery4_.carrier as carrier3_, delivery4_.shipper as shipper3_, delivery4_.shipperMethod as shipperM6_3_, delivery4_.fedexAccountNumber as fedexAcc7_3_, delivery4_.permissionSet as permissi8_3_, delivery4_.shippingPriceAmount as shipping9_3_, delivery4_.shippingPriceDisplayAmount as shippin10_3_, delivery4_.mediaPriceAmount as mediaPr11_3_, delivery4_.mediaPriceDisplayAmount as mediaPr12_3_, permission5_.id as id4_, address6_.id as id5_, address6_.company as company5_, address6_.department as department5_, address6_.title as title5_, address6_.streetAddress as streetAd9_5_, address6_.city as city5_, address6_.state as state5_, address6_.zip as zip5_, address6_.country as country5_, address6_.phone as phone5_, address6_.firstName as firstName5_, address6_.lastName as lastName5_, address6_.email as email5_, address7_.id as id6_, address7_.company as company6_, address7_.department as department6_, address7_.title as title6_, address7_.streetAddress as streetAd9_6_, address7_.city as city6_, address7_.state as state6_, address7_.zip as zip6_, address7_.country as country6_, address7_.phone as phone6_, address7_.firstName as firstName6_, address7_.lastName as lastName6_, address7_.email as email6_, editaction0_.id as id7_, editaction0_.comment as comment7_, editaction0_.date as date7_, editaction0_.user_id as user_id7_, editaction0_.version_id as version_id7_ from VersionActions editaction0_ left outer join Users user1_ on editaction0_.user_id=user1_.id left outer join Orders order2_ on user1_.workingOrder=order2_.id left outer join Cart cart3_ on order2_.cart=cart3_.id left outer join DeliveryMethods delivery4_ on order2_.delivery=delivery4_.id left outer join PermissionSets permission5_ on delivery4_.permissionSet=permission5_.id left outer join Contacts address6_ on order2_.shippingAddress=address6_.id left outer join Contacts address7_ on user1_.address=address7_.id where editaction0_.version_id=?
ERROR [Thread-4] (PersistentCollection.java:206) - Failed to lazily initialize a collection
net.sf.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: collective.mc.EditAction (loaded object was of wrong class)
at net.sf.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:455)
at net.sf.hibernate.loader.Loader.getRow(Loader.java:422)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:208)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:132)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:909)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:884)
at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:80)
at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:284)
at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3133)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:203)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:69)
at net.sf.hibernate.collection.List.size(List.java:81)
at org.apache.tapestry.Tapestry$CollectionIteratorAdaptor.coerce(Tapestry.java:570)
at org.apache.tapestry.Tapestry.coerceToIterator(Tapestry.java:866)
at org.apache.tapestry.components.Foreach.getSourceData(Foreach.java:113)
at org.apache.tapestry.components.Foreach.renderComponent(Foreach.java:126)
at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:880)
at org.apache.tapestry.BaseComponent.renderComponent(BaseComponent.java:159)
at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:880)
at org.apache.tapestry.AbstractComponent.renderBody(AbstractComponent.java:657)
at org.apache.tapestry.components.Foreach.renderComponent(Foreach.java:163)
at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:880)
at org.apache.tapestry.AbstractComponent.renderBody(AbstractComponent.java:657)
at org.apache.tapestry.components.RenderBody.renderComponent(RenderBody.java:86)
at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:880)
at org.apache.tapestry.AbstractComponent.renderBody(AbstractComponent.java:657)
at org.apache.tapestry.html.Body.renderComponent(Body.java:310)
at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:880)
at org.apache.tapestry.AbstractComponent.renderBody(AbstractComponent.java:657)
at org.apache.tapestry.html.Shell.renderComponent(Shell.java:173)
at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:880)
at org.apache.tapestry.BaseComponent.renderComponent(BaseComponent.java:159)
at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:880)
at org.apache.tapestry.BaseComponent.renderComponent(BaseComponent.java:159)
at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:880)
at org.apache.tapestry.AbstractPage.renderPage(AbstractPage.java:341)
at org.apache.tapestry.engine.RequestCycle.renderPage(RequestCycle.java:411)
at org.apache.tapestry.engine.AbstractEngine.renderResponse(AbstractEngine.java:774)
at org.apache.tapestry.engine.ActionService.service(ActionService.java:206)
at org.apache.tapestry.engine.AbstractEngine.service(AbstractEngine.java:912)
at org.apache.tapestry.ApplicationServlet.doService(ApplicationServlet.java:238)
at org.apache.tapestry.ApplicationServlet.doGet(ApplicationServlet.java:199)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:432)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:386)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:534)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:530)
at java.lang.Thread.run(Thread.java:536)
INFO [Thread-4] (HibernateMonitor.java:307) - <<serviceException>>: org.apache.tapestry.ApplicationRuntimeException: Failed to lazily initialize a collection
INFO [Thread-4] (AbstractEngine.java:952) - Uncaught exception
org.apache.tapestry.ApplicationRuntimeException: Failed to lazily initialize a collection
at org.apache.tapestry.engine.RequestCycle.renderPage(RequestCycle.java:425)
at org.apache.tapestry.engine.AbstractEngine.renderResponse(AbstractEngine.java:774)
at org.apache.tapestry.engine.ActionService.service(ActionService.java:206)
at org.apache.tapestry.engine.AbstractEngine.service(AbstractEngine.java:912)
at org.apache.tapestry.ApplicationServlet.doService(ApplicationServlet.java:238)
at org.apache.tapestry.ApplicationServlet.doGet(ApplicationServlet.java:199)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:432)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:386)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:534)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:530)
at java.lang.Thread.run(Thread.java:536)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 4:33 pm 
Newbie

Joined: Wed Nov 12, 2003 3:45 pm
Posts: 8
My problem in the abstract, hopefully to help define the issue.

Parent class AbstractAction
subclass ApproveAction
subclass EditAction
subclass ViewAction

Class Version
List of ApproveActions
List of EditActions
List of ViewActions

When the class for the List mappings is set to the AbstractAction class, there are no exceptions thrown, but all three lists have approveActions, editActions, and viewActions in them. When the class for the List mappings is set to the correct subclass, an exception is thrown because Hibernate is still attempting to load all the actions into each list, but now the object types do not match.

It seems like Hibernate should be intelligent enough to only load the records that match the discriminator for that subclass.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 4:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Hm, don't know if this should work. A workaround would be to add where="actionType = W" to your set mappings.


Top
 Profile  
 
 Post subject: Duplicate Code (Inheritance def and Where clause)
PostPosted: Sun Feb 08, 2004 3:29 am 
Newbie

Joined: Sun Feb 08, 2004 3:17 am
Posts: 2
I am having the exact same issue. The where-clause workaround does in fact work.
It's a shame to have to duplicate code like that, though.
I only have the problem (so far) when I map a collection of the subclass objects. If I query directly against the base class or directly against any of the subclasses, I get the correct objects--and the correct where clause is generated for the query.

Given the hierarchy:

Hat
->BaseballCap
->TopHat
->Bonnet

If I create a RichGuy class with a collection of TopHats, I get the error. If I query all of the Hats in the shop, I do not get the error; all of the subclasses resolve correctly.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 5:47 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://opensource.atlassian.com/projects/hibernate/secure/ViewIssue.jspa?key=HB-406

_________________
Emmanuel


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