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.