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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:02:30+00:00 2026-05-26T06:02:30+00:00

I have 3 entities: @Entity public abstract class A { @Id public Long id;

  • 0

I have 3 entities:

@Entity
public abstract class A {
  @Id
  public Long id;
  public String a1;
  public String a2;
  public String a3;

  //much more fields    
  //getters and setters
}

@Entity
public class B extends A {
  public String b1;
  public String b2;
  public String b3;

  //much more fields
  //getters and setters
}

@Entity
public class C extends A {
  public String c;
  //that's it. no more data

  //getters and setters
}

I want to map these classes to 2 tables. First one will contain all of A and C data (i.e. SINGLE_TABLE inheritance). And the second one will contain B‘s data and a foreign key to A (i.e. JOINED inheritance).

I tried the solution proposed here, but it doesn’t work for me. The attributes of BB1 and BB2 are also included into A.

How to implement such a strategy? Classes A and C are as different as Dog and Cat are, so I can’t merge them into one class.

Also I don’t want to use table-per-hierarchy which would result in duplicating lots of A‘s data.

  • 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-26T06:02:31+00:00Added an answer on May 26, 2026 at 6:02 am

    After spending so much time on this, and not getting any answer, I’ve come to this solution (which could be not the best):

    @Entity
    public abstract class A implements Serializable {
      @Id
      public Long id;
    
      public String a1;
      public String a2;
      public String a3;
    
      //much more fields    
      //getters and setters
    }
    
    @Entity
    public class B implements Serializable {
    
      @Id  
      @Column(name="id", nullable=false)  
      public Long id;
    
      @MapsId  
      @OneToOne(optional=false)  
      @JoinColumn(name="id")  
      public A a;
    
      public String b1;  
      public String b2;  
      public String b3;
    
      //much more fields
      //getters and setters
    }
    
    @Entity
    public class C extends A {  
    
      public String c;
      //that's it. no more data
    
      //getters and setters
    }
    

    Conclusion
    I was amazed at how such well-supported and popular technology as JPA does not offer a solution for such a trivial case. As pointed out by Pascal Thivent in his answer to this question, Hibernate is cheating us, using a secondary table, which is a very tedious and error-prone approach (you should manually specify for each field, to which table it belongs). So it looks like, there is still a room for improvement in JPA spec.

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

Sidebar

Related Questions

I have some Entities that look like this: @Entity public abstract class AbstractEntity{ @Id
I have two JPA entities: @Entity public class TaskSchedule { ... private String name;
A have two entities. For example timing settings and orders. @Entity public class TimingSettings{
I have a class abstract class Entity<T> where T : Entity<T> { public virtual
I have two entities @Entity public class Tabulka{ @OneToMany(mappedBy = tabulka) private List<VysledkyHraca> vysledkyHraca;
I have two entities like the following: @Entity public class Trip { @OneToMany(mappedBy =
I have such a simple model: public abstract class Entity { public virtual Guid
I have following structure of my entities: @MappedSuperclass public abstract class BaseEntity { @Id
Imagine you have an entity that has some relations with other entities and you
I have a Contact entity that exposes a navigation proeprty for child Phone entities.

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.