-->
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: tricky mapping problem
PostPosted: Mon Feb 23, 2004 5:33 am 
Senior
Senior

Joined: Wed Aug 27, 2003 4:08 am
Posts: 178
Location: Wiesbaden, Germany
I try to create relation ( map ) with subclass hierarchy.

Say, I have base ( abstract ) class, I use a field ( "name" ) as map key ( it's unique for all subclass istances )

And I like to have

1. Map of all instances
2. Maps of instances of particular subclass

My mapping is:
( from map side, fixtures is map of abstract base class )
Code:
        <map
            name="fixtures"
            lazy="true"
            sort="unsorted"
            inverse="false"
            cascade="delete"
            order-by="name"
        >

              <key
                  column="projectid"
              />

              <index
                  column="name"
                  type="string"
              />

              <one-to-many
                  class="com.infodesire.status.project.impl.FixtureData"
              />
        </map>

        <map
            name="laterals"
            lazy="true"
            sort="unsorted"
            inverse="false"
            cascade="none"
            order-by="name"
        >

              <key
                  column="projectid"
              />

              <index
                  column="name"
                  type="string"
              />

              <one-to-many
                  class="com.infodesire.status.project.impl.LateralData"
              />
        </map>

        <map
            name="manholes"
            lazy="true"
            sort="unsorted"
            inverse="false"
            cascade="none"
            order-by="name"
        >

              <key
                  column="projectid"
              />

              <index
                  column="name"
                  type="string"
              />

              <one-to-many
                  class="com.infodesire.status.project.impl.ManholeData"
              />
        </map>

        <map
            name="sewers"
            lazy="true"
            sort="unsorted"
            inverse="false"
            cascade="none"
            order-by="name"
        >

              <key
                  column="projectid"
              />

              <index
                  column="name"
                  type="string"
              />

              <one-to-many
                  class="com.infodesire.status.project.impl.SewerData"
              />
        </map>



And here are subclasses:
Code:
<hibernate-mapping>
    <class
        name="com.infodesire.status.project.impl.FixtureData"
        table="fixture"
        polymorphism="implicit"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Integer"
            unsaved-value="null"
        >
            <generator class="native">
            </generator>
        </id>

        <discriminator
            column="type"
            type="string"
        />

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

        <many-to-one
            name="project"
            class="com.infodesire.status.project.impl.ProjectData"
            cascade="none"
            outer-join="auto"
            update="false"
            insert="true"
            column="projectid"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-FixtureData.xml
            containing the additional properties and place it in your merge dir.
        -->
        <subclass
            name="com.infodesire.status.project.impl.LateralData"
            dynamic-update="false"
            dynamic-insert="false"
            discriminator-value="lateral"
        >
            <!--
                To add non XDoclet property mappings, create a file named
                hibernate-properties-LateralData.xml
                containing the additional properties and place it in your merge dir.
            -->

        </subclass>
        <subclass
            name="com.infodesire.status.project.impl.ManholeData"
            dynamic-update="false"
            dynamic-insert="false"
            discriminator-value="manhole"
        >
            <!--
                To add non XDoclet property mappings, create a file named
                hibernate-properties-ManholeData.xml
                containing the additional properties and place it in your merge dir.
            -->

        </subclass>
        <subclass
            name="com.infodesire.status.project.impl.SewerData"
            dynamic-update="false"
            dynamic-insert="false"
            discriminator-value="sewer"
        >
            <!--
                To add non XDoclet property mappings, create a file named
                hibernate-properties-SewerData.xml
                containing the additional properties and place it in your merge dir.
            -->

        </subclass>

    </class>

</hibernate-mapping>


Map of parent class ( fixture ) works fine, but whe I try to read map of one of subclasses, I get:

Code:
Failed to lazily initialize a collection: Object with id: 5 was not of the specified subclass: com.infodesire.status.project.impl.SewerData (loaded object was of wrong class)
net.sf.hibernate.WrongClassException: Object with id: 5 was not of the specified subclass: com.infodesire.status.project.impl.SewerData (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:2897)
        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.Map.size(Map.java:82)
        at com.infodesire.status.project.test.FixtureCRUDTest.testFixturesCreation(FixtureCRUDTest.java:100)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
r



Question is classical, what I'm doing wrong, or maybe it's unsu

_________________
Got new hibernate xdoclet plugin? http://www.sourceforge.net/projects/xdoclet-plugins/
... Momentan auf der Suche nach neuen Projekt ode Festanstellung....


Top
 Profile  
 
 Post subject: nevermind. solution found.
PostPosted: Mon Feb 23, 2004 5:47 am 
Senior
Senior

Joined: Wed Aug 27, 2003 4:08 am
Posts: 178
Location: Wiesbaden, Germany
adding "where" clause to <map> in question helps...

Reading DTD rulez :)

_________________
Got new hibernate xdoclet plugin? http://www.sourceforge.net/projects/xdoclet-plugins/
... Momentan auf der Suche nach neuen Projekt ode Festanstellung....


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.