Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6848583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:53:11+00:00 2026-05-27T00:53:11+00:00

My situation is this: We have a superclass, MyAbstractEntity @Entity @Table(name = MyTable) @Inheritance(strategy

  • 0

My situation is this:

We have a superclass, “MyAbstractEntity”

@Entity
@Table(name = "MyTable")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DISCRIMINATOR", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("FOO")
public abstract class MyAbstractEntity implements Comparable<MyAbstractEntity> {

This class in turn has quite a few subclasses, looking like this:

@Entity
@DiscriminatorValue("BAR")
public class MyEntity extends MyAbstractEntity implements MyInterface {

About half of the subclasses implement “MyInterface”.

I have a Criteria in which I am interested in getting only objects from a class implementing that interface.
When I define my Criteria like this:

Criteria criteria = getSession().createCriteria(MyInterface.class)

Hibernate turns around and generates one DB-call for each implementation of the interface and the DB-guys goes nuts. I was half expecting Hibernate to translate this into something that used an “in” clause on the DISCRIMINATOR column rather than creating a separate call for each implementation.

I’ve tried googling around for this, but unfortunately my results get polluted by the fact that my query contains “hibernate”, “criteria” and “interface” and I keep bumping into pages that either explain the API or demonstrates basic examples.

I am aware that I could create the Criteria on “MyAbstractEntity” rather than “MyInterface” and remove unwanted results but I want this filtering to happen in the database.

Is there no way to tell Hibernate that I want everything in one single db-call?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:53:12+00:00Added an answer on May 27, 2026 at 12:53 am

    I could not get Nizet’s solution to work so my slightly dirty way of doing it was:

    Restrictions.sqlRestriction("{alias}.DISCRIMINATOR in (" + discriminatorValues + ")")
    

    where discriminatorValues is a static field initialized in a static block by using a Spring helper class.

        Set<Class <? extends MyAbstractEntity>> result = new HashSet<Class <? extends MyAbstractEntity>>();
    
        ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
        provider.addIncludeFilter(new AssignableTypeFilter(MyInterface.class));
        Set<BeanDefinition> components = provider.findCandidateComponents("path/to/package/with/subclasses");
        for (BeanDefinition component : components) {
            Class<?> clazz = null;
            try {
                clazz = Class.forName(component.getBeanClassName());
            } catch (ClassNotFoundException e) {
                ; //stuff some WTF-message into the logger
            }
    
            result.add((Class<? extends MyAbstractEntity>) clazz);
        }
    
        String sep = "";
        for (Class<? extends MyAbstractEntity> o : result) {
            discriminatorValues += sep + "'" + o.getAnnotation(DiscriminatorValue.class).value() + "'";
            sep = ",";
        }
    

    Not the kind of solution I wanted to go for, but the resulting SQL looks like I wanted it to…
    (and new implementations of the interface will be included in the query with no further action from future devs)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The situation is this: I have a div with overflow auto containing a table.
I have this situation: <select name=year > <?php $today = date(Y); for ($i=2005; $i<=$today;
Situation is this: I have a table for staff and another one for course.
I have this situation: $(#button).toggle(function(){ $(#window).animate({top:'0%'},1000); },function(){ $(#window).animate({top:'-100%'},1000); }); but I need change it
I have this situation: http://jsfiddle.net/bRDgK/3/ In this situation I have a modal dialog with
I have this situation: { float foo[10]; for (int i = 0; i <
I have this situation where I want to display a list of Administration objects
We have this situation: Window Keyboard ^ ^ | / ApplicationWindow so class Window
I have this situation: interface MessageListener { void onMessageReceipt(Message message); } class MessageReceiver {
I have this situation: There are a login page with a login form (form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.