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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:06:38+00:00 2026-05-18T22:06:38+00:00

I ran into a problem with Hibernate concerning hql query execution on classes which

  • 0

I ran into a problem with Hibernate concerning hql query execution on classes which use inheritance.


Model Structure

public class Filed {

private Long id; 
private String name;

public Long getId() {
 return this.id;
}

public void setId(Long id) {
 this.id = id;
}

public String getName(){
 this.name;
}

public void setName(String name) {
 this.name = name;
}

}



public class SuperField extend Field {

private String role;

public String getRole(){
 return this.role;
}

public void setRole(String role) {
 this.role = role;
}

}

Mapping

<hibernate-mapping>
 <class name="org.stackoveflow.Field" table="FILEDS" schema="STACK">
  <id name="id" type="java.lang.Long">
   <column name="FILED_ID" precision="22" scale="0" />
   <generator class="sequence">
    <param name="sequence">STACK.FIELDS_SEQ</param>
   </generator>
  </id>
  <property name="name" type="string" column="NAME"/>
 </class>
</hibernate-mapping>

<hibernate-mapping>
 <class name="org.stackoveflow.SuperField" table="SUPER_FILEDS" schema="STACK">
  <id name="id" type="java.lang.Long">
   <column name="FILED_ID" precision="22" scale="0" />
   <generator class="sequence">
    <param name="sequence">STACK.SUPER_FIELDS_SEQ</param>
   </generator>
  </id>
  <property name="name" type="string" column="NAME"/>
  <property name="role" type="string" column="ROLE"/>
 </class>
</hibernate-mapping>

Then we have two functions.

public Long countFiledByName(String name) {

  String query = "select count(*) from Filed Field where Field.name = :fieldName";

  List<?> list = getHibernateTemplate().findByNamedParam(query, new String[] {fieldName},new Object[] { name});

  return (Long) list.get(0);

}

public Long countSuperFieldByName(String name) {

  String query = "select count(*) from SuperFiled SuperField where SuperField.name = :superFieldName";

  List<?> list = getHibernateTemplate().findByNamedParam(query, new String[] {"superFieldName"},new Object[] { name});

  return (Long) list.get(0);

}

The problem is that in function countFiledByName, list have two elements in result

  • 0 – Count from SuperField
  • 1 – Count from Field

Naturally this look like hibernate, execute the statement for class hierarchy even if the mappings are not related.

Does anyone know what lock this behavior and load only objects from one table ?

PS.

Same behavior goes for criteria, but then result is inverted

  • 0 – Count from Field
  • 1 – Count from SuperField
  • 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-18T22:06:39+00:00Added an answer on May 18, 2026 at 10:06 pm

    Interesting behaviour.

    The root cause seems to be this (10.1.6. Table per concrete class using implicit polymorphism):

    The disadvantage of this approach is that Hibernate does not generate SQL UNIONs when performing polymorphic queries.

    Looks like you can solve this problem by swithing to explicit table-pre-concrete-class inheritance strategy, though I’m not sure what to do with different id generators:

    <hibernate-mapping>
        <class name="org.stackoveflow.Field" table="FILEDS" schema="STACK">
            <id name="id" type="java.lang.Long">
                <column name="FILED_ID" precision="22" scale="0" />
                <generator class="sequence">
                    <param name="sequence">STACK.FIELDS_SEQ</param>
                </generator>
            </id>
            <property name="name" type="string" column="NAME" />
            <union-subclass name = "org.stackoveflow.SuperField" table = "SUPER_FILEDS" schema = "STACK">
                <property name="role" type="string" column="ROLE"/> 
            </union-subclass>
        </class>
    </hibernate-mapping>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've ran into a problem while trying to test following IRepository based on NHibernate:
I ran into the problem that my primary key sequence is not in sync
I recently ran into a problem that I thought boost::lambda or boost::phoenix could help
I recently ran into a problem with a COM object that was using a
I'm writing a C parser using PLY, and recently ran into a problem. This
I know this question has been asked before, but I ran into a problem.
Today, I ran into this weird problem with a user using Mac OS X.
Ok, so I just ran into the following problem that raised an eyebrow. For
A problem I ran into a while back I never found a good solution
Here's a problem I ran into recently. I have attributes strings of the 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.