Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Hibernate @Where annotation not working with inheritance
PostPosted: Thu Oct 05, 2017 5:02 pm 
Newbie

Joined: Wed Jun 22, 2005 3:58 am
Posts: 14
It seems that it is not possible to use the @Where annotation when mapping a collection of entities that make use of the JOIN inheritance strategy. The problem is that Hibernate assumes that the column referenced in the @Where clause will be found in the table of the entity we are limiting, rather than also checking for the column in the super-class tables for that entity.

I have created a Jira issue and a test case demonstrating the problem: https://hibernate.atlassian.net/browse/HHH-12016

My question is if anyone can think of a workaround for this problem. How can I limit my @OneToMany children, without giving up inheritance?


Top
 Profile  
 
 Post subject: Re: Hibernate @Where annotation not working with inheritance
PostPosted: Fri Oct 06, 2017 3:06 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1588
Location: Romania
There is a workaround.

Just use @JoinFormula on the @ManyToOne association.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Hibernate @Where annotation not working with inheritance
PostPosted: Fri Oct 06, 2017 2:03 pm 
Newbie

Joined: Wed Jun 22, 2005 3:58 am
Posts: 14
Vlad, could you please expand a little on your answer?

I already tried using @JoinFormula, but the problem I ran into was that Hibernate generated a sub-select query inside the outter join condition, resulting in
Code:
"ORA-01799: a column may not be outer-joined to a subquery"
error. So that didn't work either.

It is possible that I used @JoinFormula wrong though. Could you please show me how to do it correctly? The details of what I tried are here:
https://stackoverflow.com/questions/465 ... nheritance

But to summarize, I did:
Code:
@ManyToOne
    @JoinColumnsOrFormulas([
        @JoinColumnOrFormula(formula = @JoinFormula(value = "(APPLICATION_OID)", referencedColumnName="APPLICATION_OID")),
        @JoinColumnOrFormula(formula = @JoinFormula(value = "(select apv.PROJECT_VERSION_OID from DFIP_PROJECT_VERSION pv, DFIP_APPLN_PROJ_VERSION apv where apv.PROJECT_VERSION_OID = pv.PROJECT_VERSION_OID and apv.APPLICATION_OID = APPLICATION_OID and pv.PROJ_VSN_EFF_TO_DTM is null)", referencedColumnName="PROJECT_VERSION_OID")),
    ])
    ApplicationProjectVersion latestVersion;


What is the correct way to do it?


Top
 Profile  
 
 Post subject: Re: Hibernate @Where annotation not working with inheritance
PostPosted: Fri Oct 06, 2017 2:17 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1588
Location: Romania
Just make the @ManyToOne LAZY. By default, it's EAGER, which is bad for performance and it's not working for you either.

Code:
@ManyToOne(fetch = FetchType.LAZY)

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Hibernate @Where annotation not working with inheritance
PostPosted: Fri Oct 06, 2017 2:33 pm 
Newbie

Joined: Wed Jun 22, 2005 3:58 am
Posts: 14
If I make it LAZY, does it not just defer exactly the same problem until the time when I do want to fetch the latest ApplicationProjectVersion? Besides, I never want the Application w/o the latest ApplicationProjectVersion - it's not useful w/o it.


Top
 Profile  
 
 Post subject: Re: Hibernate @Where annotation not working with inheritance
PostPosted: Fri Oct 06, 2017 3:41 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1588
Location: Romania
If you defer it, it will use a secondary query.

If you always need it, then just remove the complex relationship and write a JPQL/SQL query to fetch these two using a JOIN instead. Usually, an exotic association is a code smell.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Hibernate @Where annotation not working with inheritance
PostPosted: Tue Dec 05, 2017 8:34 pm 
Newbie

Joined: Wed Jun 22, 2005 3:58 am
Posts: 14
Just to follow up, I did end up discovering a way to emulate a polymorphic @Where annotation.

Solution described here:
https://stackoverflow.com/questions/465 ... nheritance


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 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.