-->
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: many-to-one/one-to-many mappings with table per hierarchy
PostPosted: Wed May 31, 2006 12:43 am 
Newbie

Joined: Tue May 30, 2006 11:38 pm
Posts: 2
I am trying to create a class containing a set of any of a number of subclasses which are stored in a single table, using the descriminator column to differentiate the subclasses.

i.e.:
Code:
public class A{
    private long id;
    private Set<B> setofB;
. . .
    public Set<B> getSetofB(){
        return setofB;
    }
. . .
}

public abstract class B{
    private A owner;
    private String Identifier;
. . .
}

public class B1 extends B{
. . .
}

public class B2 extends B{
. . .
}


I would like to map this out as a one-to-many in the class A mapping, and a many-to-one mapping for class B. Also, while I would like to use the string Identifier as a key, I would like it to be unique only in relation to its owner. Essentially, I would like to have class B utilize a composite-id with the many-to-one key and the Identifier.

For the class A mappings, I wrote this:
Code:
<hibernate-mapping>
<class name="A" table="A" >
<id
name="id"
column="ID"
type="long">
<generator class="identity" />
</id>
<set name="setofB" inverse="true" cascade="save-update">
<key column="OWNER" />
<one-to-many class="B" />
</set>
</class>
</hibernate-mapping>


and for the class B mappings, this:
Code:
<hibernate-mapping>
<class name="B" table="B" >
<composite-id>
<key-many-to-one class="A" name="owner" column="OWNER" />
<key-property name="identifier" column="ID" type="string" />
</composite-id>
<discriminator column="DATA_TYPE" type="string" />
<subclass name=B1>
. . .
</subclass>
<subclass name=B2>
. . .
</subclass>
</hibernate-mapping>



The problem I am having is that whenever I attempt to access the Set<B> setofB member (i.e. call getSetofB() method), I get this exception:
Quote:
Exception in thread "main" org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: B
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:79)
at org.hibernate.tuple.AbstractComponentTuplizer.instantiate(AbstractComponentTuplizer.java:177)
at org.hibernate.type.ComponentType.instantiate(ComponentType.java:428)
at org.hibernate.type.ComponentType.instantiate(ComponentType.java:434)
at org.hibernate.type.EmbeddedComponentType.instantiate(EmbeddedComponentType.java:56)
at org.hibernate.type.ComponentType.resolve(ComponentType.java:524)
at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:229)
at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1088)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:554)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1919)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:71)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1627)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:109)
at org.hibernate.collection.PersistentSet.isEmpty(PersistentSet.java:121)
at main(test.java:38)


I had managed to get this to successfully work without the use of the composite id's, and instead using a natively assigned id (Identifier) and a regular many-to-one tag within the B mapping file. However, the problem with that is that the identifier must be unique for all instances of class B. As stated before, I only want the identifer to be unique for all instances of class B which happen to be owned by a particular instance of class A. I think setting up a composite-id with the identifier and owner is the best way of achieving this.

Hibernate version:3.1

Name and version of the database you are using:MySQL 5.0.21

Thanks in advance for any assistance you can offer.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 12:39 am 
Newbie

Joined: Tue May 30, 2006 11:38 pm
Posts: 2
I've done some more research on the problem, and it appears the problem stems from trying to use composite-ids with abstract classes?


I've just managed to get it working by changing my abstract class (class B) into a concrete class. Of course, I would prefer to keep it as an abstract class if possible.

Has anyone else encountered this limitation? Is there a reasonable way around this problem?

Thanks again in advance for any assistance and suggestions.


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.