I can access all the data for ContentVO, however if I have let's say LinkVO as the subclass then how do I access it? Do I need to reformulate my query or is there any way I can get access to the subclass?
The query above returns ContentVO as well as all its subclasses.
However, I'm clueless as to how to access the subclasses; there must be some additional mapping that needs to be done, otherwise I don't see how it's possible to access them.
I get a class cast exception error as expected b/c the resultset is returned as a ContentVO type and not a LinkVO.
Is this example a correct implementation of what you had in mind?
Sujan Pradhan
Greenhorn
Joined: Jul 19, 2006
Posts: 11
posted
0
It seems to me that only way to avoid the class cast exception would be to alter the query to return a LinkVO:
session.createCriteria( LinkVO.class ).list();
However, doing so defeats the purpose of being able to access the data for all the subclasses of ContentVO in one go...
I am still clueless at this point :roll:
pascal betz
Ranch Hand
Joined: Jun 19, 2001
Posts: 547
posted
0
whats this resultset.get(i) ? is resultset a JDBCResultSet ? where does it come from ? why do you need to do this ? the persistent items are in the List returned by the criteria. Without showing some more code we can not tell why you have a class cast exception. but to me it looks like:
if you want to query for a hierarchy and then work on the items:
(assuming you have java 1.5. otherwise just use the normal iterator way to loop.
pascal
Sujan Pradhan
Greenhorn
Joined: Jul 19, 2006
Posts: 11
posted
0
The resultset represents a list (it's just a variable); no JDBC is used here- strictly hibernate. Sorry for the ambiguity.
The enhanced-for loop is quite nice to use and it got rid of the class cast exception, however the instanceof block is never executed- another words 'linkVO' is not an instance of 'ContentVO' when the for-loop is executed. That could only mean it returns a list of ContentVOs. I will post my hbm mapping file as well as the code to show you what I've done.
The hbm.xml mapping:
Code for retrieving result set:
Also, the entity class LinkVO extends ContentVO (consistence with inheritance hbm mapping).
I've also checked to make sure that relationships between the two are actually implemented by Hibernate by changing the query to this:
My experience so far has been that if the criteria is the Base class then it only returns the the Base class and not its subclasses ; obviously, it would be same if the criteria was a subclass (but I am also able to access all Base class variables via inheritance).
Yet still, I am not able to access all the subclasses in one go if my criteria is a Base class (per hibernate documentation); is my mapping wrong or what is it that I am missing (pulling my hair out)?
Thanks!
Sujan Pradhan
Greenhorn
Joined: Jul 19, 2006
Posts: 11
posted
0
Just to bring this to closure...
The above mapping and DAO code actually worked; I am not sure why it didn't work the first time around.