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 6087009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:49:16+00:00 2026-05-23T11:49:16+00:00

How can I write a Hibernate Criteria query, for a super-class, and check for

  • 0

How can I write a Hibernate Criteria query, for a super-class, and check for a certain sub-class? Let’s imagine we have the following classes all mapped up with Hibernate-JPA:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Bar {
    @Id
    @Column(name = "id")
    private Long id;
}

@Entity
@PrimaryKeyJoinColumn(name="bar_id")
public class Foo extends Bar {
}

@Entity
@PrimaryKeyJoinColumn(name="bar_id")    
public class Goo extends Bar {
}

When writing a Criteria query like this, I would like, for performance, to use a left-join with the sub-class:

getSession()
    .createCriteria(Bar.class)
    .createAlias("Foo", "foo", CriteriaSpecification.LEFT_JOIN)
    .add(Restrictions.isNotNull("foo.bar_id"))
    .list();

This fails, because the association path “Foo” doesn’t work obviously, but it will illustrate what I want. Or is there another way of doing this type of query? I need the query to be performed on the superclass. If I would have done it in SQL it would look like this:

select b.*
from bar b left join foo f on f.bar_id = b.id
where f.bar_id is not null;

The SQL query above is just to illustrate what I mean, I know it would be easier to use a “normal” join in that specific case.

  • 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-23T11:49:17+00:00Added an answer on May 23, 2026 at 11:49 am

    It’s really not clear what you want to do.

    First of all, since Foo inherits from Bar, searching for Bar instances will automatically return Foo instances. Hibernate takes care of joining the tables by itself.

    Second: your SQL query is really strange. You’re doing a left join (which means that you’re searching for bars who might not have an associated foo), but you also have a where close on foo.bar_id being not null. This in fact constitutes an inner join, and could be rewritten as

    select b.* from bar b inner join foo f on f.bar_id = b.id
    

    If what you want to do is search for Foos, and Foos only, then use a Criteria with Foo as root entity:

    getSession()
        .createCriteria(Foo.class)
        .list();
    

    You will get Foo instances, but since Foo extends Bar, these Foo instances are also Bar instances. That’s what inheritance is.

    Now if you’re building your Criteria instance dynamically, and realize at some point that the search must only return instances of Foo, you have to use the implicit class property:

    Criteria c = getSession().createCriteria(Bar.class, "bar")
    // ...
    if (limitToFoos) {
        c.add(Restrictions.eq("bar.class", Foo.class));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You can also write hibernate hql or criteria queries. I have Teacher entity and
I learned that to configure c3p0 pooling in hibernate, we can have write the
I have a problem with SetFetchMode call in Criteria API in following query: DetachedCriteria.For<User>()
I have a problem with hibernate and criterias. I have two Classes: public class
Let's say I am using Hibernate Validator and I write the following code: @Email
We can write CSS as the following types: Inline CSS Embedded CSS External CSS
In SQL one can write a query that searches for a name of a
In C++ one can write any of the following statements: 10; true; someConstant; //if
Imagine the following (simplified) database layout: We have many holiday records that relate to
Hibernate complains, Caused by: java.lang.UnsupportedOperationException: Can't write to a readonly object at org.hibernate.cache.ReadOnlyCache.lock(ReadOnlyCache.java:68) for

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.