This content has been marked as final.
Show 4 replies
-
1. Re: hql and polymorphism
flopsi Dec 1, 2010 5:45 PM (in response to flopsi)okay, declaring the base class abstract seems to force hibernate to look for subclasses. unfortunately this has some impact on the rest of my application, so the question remains:
is ther another way to force hibernate to do so?
thx + regards
flo -
2. Re: hql and polymorphism
rogermorituesta.rogermori.yahoo.com Dec 1, 2010 7:27 PM (in response to flopsi)
...but i needed it to be the right subclass to be able to fetch all properties. casting to the subclass results in a ClassCastException.A polymorfic hql result set already contains runtime instances of the subclasses. So no casting is required.
If you want to query upon a specific subclass add a where clause to your hql.
Roger.
-
3. Re: hql and polymorphism
flopsi Dec 3, 2010 9:38 AM (in response to flopsi)hi roger,
maybe i miss something. I tried using the 'class' attribute like
select objA from classA objA inner join fetch objA.prop objB where objB.class=:classId
with binding the simple class name to the :classId parameter, but an exception was thrown:
java.lang.String cannot be cast to java.lang.Integer
the generated sql looks like:
...
and case
when objB_1_.id is not null then 1
when objB_2_.id is not null then 2
when objB_.id is not null then 0
end=?
where objB_1_ and objB_2_ reference the subclass tables as i can see in the select. so maybe binding 0, 1 or 2 to the :classId param would help.
but how do i find out the correct number? is the hibernate generated statement fix so i can rely on the numbers shown here? or is it possible on the next execution the numbers have changed?
is there maybe another way to specify the concrete class?
thanks a lot
best regards
flo -
4. Re: hql and polymorphism
rogermorituesta.rogermori.yahoo.com Dec 3, 2010 10:47 AM (in response to flopsi)Hi Florian:
Dealing with addresses (Geographic, email, web, phone, etc), I have a similar case with inheritance type JOINED. Yet, I have added a string column and a pre persist method to the base class in order to store the class name via entity listener (@PrePersist):
@PrePersist public void prePersist(Object entity) { if (entity instanceof Address) { Address address = (Address)entity; String type = address.getClass().getSimpleName(); address.setType(type); } }
Also this hql will work as well:
select objA from classA objA , subclassB objB where objA.xxx = objB.yyy
Regards.
Roger.