-->
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.  [ 10 posts ] 
Author Message
 Post subject: Problem w/ polymorphic queries -how to map to constant value
PostPosted: Mon Nov 03, 2008 1:32 pm 
Newbie

Joined: Wed Oct 29, 2008 6:29 am
Posts: 7
Location: Liège, BE
Hello,

here's my problem :

I have a MappedSuperClass A, with two Entity subclasses B and C, mapped on separate tables.
A declares a foreign key property, say 'children', which is properly implemented in B.
But C, for usecase specific and performance reasons, never has children, and should not have it in its db mapping :

Code:
                 A --> declares getChildren()
               /   \
              |     C.getChildren(): returns empty set
              |
              B.getChildren() : mapped as foreign key in DB


Now, I want to express a query (in my case in HQL), where I say :

Code:
select a from A where 'foo' in children


And this fails, since C does not have any DB mapping for 'children' :
org.hibernate.QueryException: could not resolve property: children

How can I implement this ?
Is there any way to override a property and mark it as being a constant or null in one of the subclasses, so the mapping doesn't try to resolve it ?
Or do you think there's a flaw in my object model design if I need to do that ?

thank you


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 2:06 pm 
Beginner
Beginner

Joined: Wed Sep 24, 2008 5:44 pm
Posts: 34
Have you looked through the Inheritance Mapping docs?

http://www.hibernate.org/hib_docs/refer ... leperclass

I'm not sure but implicit polymorphism may do what you need.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 3:56 pm 
Newbie

Joined: Wed Oct 29, 2008 6:29 am
Posts: 7
Location: Liège, BE
Yes, I have gone through the inheritance mapping docs. I've read about implicit polymorphism, but I didn't feel it would solve my problem.

What I would really like, is make Hibernate understand that, for class C, there are never children, and there should not be any column for the foreign key.
Just as it is possible to override an attribute and map it on a different column, i'd like to override the attribute by mapping it on NULL, or no column, whatever you call it.

Or did I miss something about implicit polymorphism ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 4:35 pm 
Beginner
Beginner

Joined: Wed Sep 24, 2008 5:44 pm
Posts: 34
I think I'm still confused or something I'm still missing here.

How are your mapping files structured? Is it not possible to move the mapping of children into B's mapping instead of A's?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 4:46 pm 
Newbie

Joined: Wed Oct 29, 2008 6:29 am
Posts: 7
Location: Liège, BE
yes, i can move the mapping down in B, but my problem still remains : I need to express a query on A (i.e. B and C together), where the children property is at stake, but that property is not mapped in C.

Let me give another example :

Code:
select item from A as item where
    'foo' in item.children OR
    item.name = 'bar'


where 'name' is a property defined and mapped in A.

this should match B instances that have a 'foo' child, and all A instances (B and C) that have 'bar' as name.

To achieve this, Hibernate must understand that, when resolving the 'children' property, it must ignore C, and only query for B.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 5:00 pm 
Beginner
Beginner

Joined: Wed Sep 24, 2008 5:44 pm
Posts: 34
Ah, I understand.
Unfortunately I can't give you any suggestions :(


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 6:01 pm 
Newbie

Joined: Wed Jun 28, 2006 3:32 pm
Posts: 16
The problem is that your query does not correspond to your mappings. Since you want to write a query on A, the children property needs to be on A also; otherwise you are asking for something that doesn't exist. If you cannot have the children property on A but must be on B, the query needs to be written against B. If you need the results from C also, use a union or a second select; of course that is not possible if your query does a join between B and C.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 6:07 pm 
Newbie

Joined: Wed Jun 28, 2006 3:32 pm
Posts: 16
The problem is that your query does not correspond to your mappings. Since you want to write a query on A, the children property needs to be on A also; otherwise you are asking for something that doesn't exist. If you cannot have the children property on A but must be on B, the query needs to be written against B. If you need the results from C also, use a union or a second select; of course that is not possible if your query does a join between B and C.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 6:09 pm 
Newbie

Joined: Wed Jun 28, 2006 3:32 pm
Posts: 16
The problem is that your query does not correspond to your mappings. Since you want to write a query on A, the children property needs to be on A also; otherwise you are asking for something that doesn't exist. If the children property must be on B, the query needs to be written against B. If you need the results from C also, use a union or a second select; of course that is not possible if your query does a join between B and C.

Sorry for the multiple posts, the web site is so unresponsive that you don't know if and when something got through.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 7:22 pm 
Newbie

Joined: Wed Oct 29, 2008 6:29 am
Posts: 7
Location: Liège, BE
that's the answer i hoped not to get, I have to say...

It thought it should be possible, because you can, for instance, define a mapping in A, and then override it in B and C, with different columns for each.
In that case, hibernate will handle it nicely, and do the query on the appropriate column for each subclass B and C .

From there, i thought it should be possible to override the mapping to target no column, which is just a special case.

anyway, thanks for the answer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.