Hi,
I want to look up all topics that have a certain subject indicator. While
this works without problems with the in-memory backend, there seems to be no
elegant solution for the Hibernate backend:
// using Hibernate provider...
LocatorFactory lf = provider.getLocatorFactory();
Locator psiSynonym = lf.createLocator( "URI", PSI_SYNONYM );
TopicMap tm;
// ...
Collection c = tm.getUtils().getMembersOfType( topic, psiSynonym );
The last line causes a ClassCastException with the Hibernate backend...
because the Hibernate implementation of getMembersOfType() uses a cast to
URILocatorImpl on the Locator object:
LocatorDataObject ldo = (LocatorDataObject) sess.load(
LocatorDataObject.class,
====> ((URILocatorImpl) subjectOrIndicator)
.getPersistenceId());
But createLocator() returns instances of TransientURILocatorImpl, so this
cannot work.
I've found a workaround by using an implementation specific call to create
suitable Locator instances:
if (lf instanceof org.tm4j.topicmap.hibernate.LocatorFactoryImpl) {
psiSynonym = ((org.tm4j.topicmap.hibernate.LocatorFactoryImpl)lf)
.createPersistentLocator( "URI", PSI_SYNONYM );
}
else {
psiSynonym = lf.createLocator( "URI", PSI_SYNONYM );
}
This works (and TM4J uses the same "trick" in its
getSubClasses/getSuperClasses methods), but it does not seem elegant to me
(it relies on the implementation instead of a public interface).
Is there a better solution that uses only public API and does not depend on
a certain implementation?
bye, Thomas
|