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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:06:31+00:00 2026-05-15T16:06:31+00:00

I am trying to map a JPA (using Hibernate) one-to-one relationship with a inheritance

  • 0

I am trying to map a JPA (using Hibernate) one-to-one relationship with a inheritance strategy TABLE_PER_CLASS. Here is an example:

@Entity
public class DrivingLicense {

    @OneToOne(targetEntity = Human.class, cascade = CascadeType.ALL, fetch=FetchType.LAZY)
    @JoinColumn
    private Human human;

    @SuppressWarnings("unchecked")
    public static List<DrivingLicense> findMansDrivingLicenses(Long id) {
        if (id == null) return null;
        return entityManager()
            .createQuery("select o from DrivingLicense o left join fetch o.human where o.id = :id")
            .setParameter("id", id)
            .getResultList();
    }

}

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Human {
   ...
}

@Entity
public class Man extends Human {
   ...
}

@Entity
public class Mutant extends Human {
   ...
}

When I call “findMansDrivingLicenses” to retrieve all man’s driving licenses hibernate does a “UNION ALL” with both tables (MAN and MUTANT). Follow the log output:

select
        drivinglic0_.id as id3_0_,
        human1_.id as id0_1_,
        drivinglic0_.first_name as first2_3_0_,
        drivinglic0_.human as human3_0_,
        drivinglic0_.last_name as last3_3_0_,
        drivinglic0_.type as type3_0_,
        drivinglic0_.version as version3_0_,
        human1_.version as version0_1_,
        human1_.comment as comment1_1_,
        human1_.behavior as behavior2_1_,
        human1_.clazz_ as clazz_1_ 
    from
        driving_license drivinglic0_ 
    left outer join
        (
            select
                id,
                version,
                comment,
                null as behavior,
                1 as clazz_ 
            from
                man 
            union
            all select
                id,
                version,
                null as comment,
                behavior,
                2 as clazz_ 
            from
                mutant 
        ) human1_ 
            on drivinglic0_.human=human1_.id 
    where
        drivinglic0_.id=?

Is there any way to prevent hibernate to do this “UNION ALL” and only join with MAN table?

  • 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-15T16:06:31+00:00Added an answer on May 15, 2026 at 4:06 pm

    Try to use class

    select o from DrivingLicense o left join fetch o.human human where o.id = :id and human.class = Man
    

    UPDATE

    retrieve your relationship with Native queries

    session = sessionFactory.openSession();
    
    StringBuilder query = new StringBuilder();
    query
    .append("select ")
        .append("{driving.*}, {man.*} ")
    .append("from ")
        .append("DrivingLicense driving ")
    .append("left join ")
        .append("Man man ")
    .append("on ")
        .append("driving.human_id = man.id ")
    .append("where ")
        .append("driving.id = :id");
    
    Query _query = session.createSQLQuery(query.toString())
                          /**
                            * It means: driving alias WILL BE MAPPED TO DrivingLicense Entity
                            */
                          .addEntity("driving", DrivingLicense.class)
                          /**
                            * It means: man alias WILL BE MAPPED TO human property of DrivingLicense Entity
                            */
                          .addJoin("man", "driving.human")
                          .setParameter("id", <DRIVING_LICENSE_ID_GOES_HERE>);
    
    
    Object [] resultArray = query.list.get(0);
    
    session.close();
    

    And

    DrivingLicense driving = resultArray[0];
    /**
      * YES, Man IS NOT automatically MAPPED TO driving.human property
      * You have to set up manually
      */
    Man man = resultArray[1];
    
    driving.setHuman(man);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to map a one to zero or one relationship in Hibernate. I
I'm trying to map the Northwind Employee entity with NHibernate: public class Employee {
Trying to map the following schema using the Entity Framework. A Customer can have
I am trying to map an Address entity and a Person entity using a
I am trying to define xml mapping for a Map<String,String> field. The entity class
I'm trying to map a fairly standard category model using EF Code First public
Hallo all. I'm trying to map an association one-to-many to an entity that is
I have a domain object class User (it is a JPA entity): @Entity public
I am trying to do do JPA/Hibernate mappings to map two tables, but am
I'm trying to map a @ManyToMany association using hibernate. But so far I only

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.