subclass 1 « Map « JPA Q&A

1. Refactoring a Hibernate entity into subclasses

I have a class that is currently mapped as an entity in a database table using Hibernate. This class should be refactored into an abstract class containing some field common to ...

2. Hibernate/NHibernate : how to persist subclass as instance of superclass

I have two classes I would like to persist via NHibernate: - Cat, which has a name and an Id, - Kitten, which is a subclass of Cat (no extra public properties). For stupid ...

3. Cannot use identity column key generation with ( TABLE_PER_CLASS )


@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class SuperClass implements Serializable {
    private static final long serialVersionUID = -695503064509648117L;

    long confirmationCode;


4. With Hibernate, can I mix table-per-subclass and a within the same table?

I'm attempting to map an object model for "Promotions"; i.e., a customer enters a promo code during signup. Different promo codes might have different types of "benefits", meaning what we are ...

5. jpa how to create new entity with same id as parent entity (JOINED Inheritance)

my question is very similar to, but instead i´m using InheritanceType.JOINED instead of Table_per_class. This means i don´t to change any table, just to create a new subclass, with the ...

6. JPA Inheritance demands ID in subclass

I have a problem with my jpa domain model. I am just trying to play around with simple inheritance for which I use a simple Person base-class and and a Customer ...

7. multiple discriminator columns for persistent subclasses

I have banged my self with a very particular problem. Using OpenJPA (KODO 4.1) is there a way to use more than one column as a discriminator column? My problem is that ...

8. hibernate subclass one to one mapping

i got a complicate mapping, i think it suppose to work...but why it compile A.d column is not existed???

public abstract Class A {
private Integer Id;

public Class SubA extend A {

9. how to map a one-to-many collection to a joined subclass when key is in the parent class

I'd like to map a one to many collection to a subclass but the key of the collection is an attribute of the parent class. Currently i'm mapping AbstractFoo Foo and Bar ...

10. Efficiency of Hibernate's table-per-subclass inheritance strategy

I'm thinking about table layout for a Hibernate-managed class hierarchy, and certainly the table per subclass technique strikes me as the most appropriate in a general sense. However, thinking through ...

11. Mapping Hibernate "Custom" Subclasses

As a new Hibernate user, I have heard that a good pattern for adding some helper methods to generated classes is to add them in a subclass. For example:

// generated via ...

12. Criteria query with restriction across a joined subclass problem

I have the following graph:


13. Hibernate -using Table per subclass - how to link an existing superclass object to a sibclass object

I have a User hibernate class, Clerk class and Consumer class. All these maps to their own tables in database. The User PK also acts as Clerk's and Consumer's PK. So ...

14. Hibernate Subclass which has multiple discriminator values

Hello I have a Subclass which needs to cover multiple Discriminator Values. something like: @DiscriminatorValue(value = "ACT","DNR","ATT" ) would do me perfect. we have existing data where several discriminators can be mapped to one ...

15. Hibernate: What's wrong with this mapping to a subclass joined on foreign key?

I am experimenting with Hibernate to gain experience. I created a class Person with two subclasses: Student and Worker:

public abstract class Person {
    private Long id;

16. Can I reuse a column across subclasses in a Hibernate table-per-hierarchy inheritance strategy?

In a simple inheritance tree, an abstract superclass has two subclasses. The subclasses both store a key-value pair, but but one will be holding a type Encrypted String, and the other ...

17. Hibernate: How do I link a subclass to its superclass?

I'm having a little problem setting up my webshop project. Thing is, I have a User() superclass and two subclasses, PrivateUser and BusinessUser. Now, I'm not quite sure how to get my ...

18. Mixing table per subclass and per hierarchy in hibernate

In my database there are two three tables. The first one, table ABSTRACT, holds three columns

id, type, someText
This table contains all abstract information for the abstract class abstract. Now the ...

19. Hibernate subclass with foreign key relationships

I need some help defining the following object hierarchy/ database relationship in Hibernate From the object sense – Agent is inherited from Person and Agency is inherited from Organization. they are inherited ...

20. Jpa subclass mapping

I am making a POS like system. I wonder how to map subclass using JPA (this is for my DAO). Product class has product details and OrderProduct class has information about ...

21. Persist subclass as superclass using Hibernate

I have a subclass and a superclass. However, only the fields of the superclass are needed to be persist.

session.saveOrUpdate((Superclass) subclass);
If I do the above, I will get the following exception.
org.hibernate.MappingException: Unknown ...

22. Hibernate transform instance to subclass

I have mapped class in JPA. :

@Inheritance(strategy = InheritanceType.JOINED)
public class User {
    private Long id;
    private Long name;
    //getters and setters go ...

23. 'table per subclass' or 'table per class hierarchy'

I am using FNHibernate and its default is 'table per subclass' for inheritance mapping. This seems to be the cleanest way to do things but it has its downsides too. My ...

24. Setting subclass primary key as auto_increment using JPA 2 annotations

I'm trying to generate a database schema for my project using hbm2ddl. I'm using JPA 2 annotations to specify how the schema should look like. Right now I'm having some issues ...

25. Can I use CompositeId on subclasses of Discriminated Entity Hierarchy?

I have a entity hierarchy has single table for all subclasses strategy. And I want to use each properties - mapped @ManyToOne - as primarykey. When I put @Id annotation, ...

26. Is it possible to use different inheritance strategy for subclasses?

I currently have a mapping problem and no idea how to solve it. Here is what we currently have. AbstractEntity is a @MappedSuperclass ExtendedEntity is an abstract @Entity extending AbstractEntity with InheritanceType.TABLE_PER_CLASS Proposal is ...

27. Using JAXB to pass subclass instances as superclass

What I have is a set of Java classes (close to 25) representing message types. They all inherit from a Message class which I'd like to be abstract. Each ...

28. JPQL exclude subclasses in a query

If I have a class Apple that extends Fruit, how do I write a JPQL query to return all objects that are strictly Fruits and not Apples?

29. Retrieve Collection of Subclasses Based on Column Value

The title is a bit weird, so let me clarify. I have two objects, Garage and Vehicle, in a one-to-many relationship. There are multiple types of vehicles, such as car and ...

30. Hibernate - force inner join between parent and subclass

I have a parent class Party and a subclass LiveParty along with others (see below) and this is always a one to one relationship in the database. Whenever I use ...

31. Inheritance Joined Strategy With Multiple Non-Keyed Columns

I have two classes Foo and Bar mapped to two different tables, and I'd like them to use the JOINED inheritance strategy, but joined using two non-keyed columns. The schema ...

32. hibernate mapping of subclass and individual fields

I have this class hierachy:

class A
  private String a1;
  private Class B a2;
// getter & setter

class B
  private String b1;
  private String b2;
  // getter & ...

33. JPA discriminator column problem

Hallo all. I have this set of classes:

@Table(name = "S_MC_CC_RAPPORTI")
                     discriminatorType=DiscriminatorType.CHAR, ...

34. How to convert an ORM to its subclass using Hibernate?

For example, I have two classes : Person and Employee (Employee is a subclass of Person). Person : has a lastname and a firstname. Employee : has also a salary. On the client-side, I ...

35. Can I use subclass properties in Hibernate Criterion Example?

I have a class-hierarchy:

Company <---- Customer
------        --------
string id     bool prospect
string name
Basically everything works fine (save and load). What fails is this: ...

36. Hibernate error: one-to-one mapping on superclass and subclass

I'm working on a project with Hibernate and MySQL, use annotation for mapping database to data object model. I need to mapping one-to-one between superclass and subclass on ID primary key ...

37. Is it possible to map a superclass and subclass by OneToOne relationship in Hibernate?

Is it possible to map a subclass to its superclass by OneToOne relationship base on their primary key properties in Hibernate? How can I implement this?

38. Map superclass and subclass to different tables in Hibernate

I want to map a class to a table and a subclass to another table. I'm not sure if you can do this in a proper way. I tried this so ...

39. Hibernate one-to-one mapping between 2 subclasses

How can I make an one-to-one relationship between 2 subclasses when they do not have the ID property? Because each of them was inherited the Id property from superclass so I ...

40. How to override the collection element type in subclass in hibernate?

For example, a Tiger is a Cat, but Tiger won't have any Cat child:

class Cat {
    Set<Cat> getChildren() { ... }
And the Tiger:
class Tiger ...

41. Nhibernate/hibernate Avoid Insert in joined table or view

I have to join a entity with a view to retrieve some data into properties

<join table="XXVIEW" optional="true">
      <key column="ID_ENT" />

42. Nhibernate / hibernate discriminator in subclass

I have a subclass with a discriminator, can i have another discriminator in a subclass
I'll try to explain
One discriminator is in Person Table may be type and i have two subclasses ...

43. Ejb 3.0 - Table per subclass - Unknown entity: ejb.entity.RubricaBean

Hello I have this problem using table per subclass:

public class PimBean implements Serializable {

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PM_ID_SEQ")
@SequenceGenerator(name = "PM_ID_SEQ", sequenceName = "pm_id_seq", initialValue = 1, allocationSize = 1)

@Column(name = ...

44. Hibernate cannot create joined sub-class table while using annotation

I am new to Hibernate and I cannot create joined sub-class table while using Hibernate annotation. Here is my code. This is the main class.

public class UserImp extends BaseModel implements IUser, Serializable

45. org.hibernate.InstantiationException: How to tell Hibernate which class to use for object instantiation

I am currently trying to replace my own database controller implementation with Hibernate and I have the following problem creating an appropriate mapping file. (I am very new to Hibernate, so please ...

46. Can I use annotations to set up "Table per subclass: using a discriminator: fetch=select"

I found that I can query without any joins when using "Table per subclass: using a discriminator". It is nice for my condition to consider some performance problems. However, I could not ...

47. With Hibernate joined-subclasses, is it possible to duplicate columns in super and sub-tables *and* keep them in sync?

So I have an interesting situation. I've inherited a big mess of code where the original developer decided to forego using inheritance in favor of enums and switch's a ...

48. Hibernate discriminator column with table per subclass

Right now I am using a table per subclass approach to model my data. A simplification of my hierarchy is:

abstract class Abstract {
    /* common data stored ...

49. JPA. How do I subclass existing entity and keep it's ID?

Assume I have two classic non-abstract JPA classes: Person and Student.

@Inheritance(strategy = InheritanceType.JOINED)
public class Person {
  @GeneratedValue(strategy = GenerationType.AUTO)
  private String id;
  // ...

public class Student ...

50. Fluent Hibernate Mapping hitch - passing wrong column name for subclass

I finished following the summer of nhibernate screen casts and am trying to convert it to fluent, just for the sake of knowledge. I have two classes (very simple)

public class Customer { ...

51. How do I override mappedBy in subclasses when using Single table inhertiance with Hibernate?

I've got 2 implementations of and abstract class Group - AGroup & BGroup

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Group(){

   public List<User> getUsers(){...};


public class AGroup extends Group(){


public ...

52. Mixing and

I'm actually adding a functionality in a product (Hibernate & Spring intensive use) and I have to modify the actual Hibernate mapping, here's the deal : the "Mother" entity (abstract) class and ...

53. Find on different properties on multi subclass

I have 3 entities Agent, Person and Machine

abstract Agent
  Long id

Person extends Agent
  String firstName
  String lastName

Machine extends Agent
   String label
I searched for a solution to ...

54. JPA/Hibernate - How is subclass type determined when querying by superclass?

I just noticed this bevahior in Hibernate and found it a bit surprising (in a welcome kind of way). I have a single entity type called SyncItem that serves as ...

55. How to distinguish subclasses as property in other entity in Single Table Inheritance

@Inheritance (strategy=InheritanceType.SINGLE_TABLE) 
public class Disc implements Serializable { 

    private Long discId;

    @GeneratedValue (strategy=GenerationType.AUTO) 

56. Unknown entity issue for joined subclass when using annotation

I met exception when using annotated joined subclass, i don't know how to correct it, please help.
Exception is: org.hibernate.MappingException: Unknown entity: B
Class A

public class A {

57. Possible to query a database with subclass entity

Ok, lets say I have a model.

  public class Person extends Model {
        public String name;

58. Hibernate subclass to override a column

We have an Entity library untouched by our web stack, but our web stack would like to extend existing columns on existing entities with validation constraints. Here's one example of ...

59. Does it make sense to use JPA's 2.0 @Inheritance annotation to a subclass?

I was wondering if it makes any sense (or even if it is correct) to annotate every class of a hierarchy with @Inheritance. For example, suppose we have the following simple ...

60. Hibernate: Change instance of base class to subclass

I would like to change a concrete superclass to one of its subclass. I've included an example below:

@Table(name = "employees")
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee {


61. How to persist an entity from an non-entity subclass in Hibernate

I am trying to extend an entity into a non-entity which is used to fill the superclass's fields. The problem is that when I try to save it Hibernate throws a ...

62. Hibernate : scroll on subclasses of a given class

Is it possible through Hibernate to perform a scroll on subclasses of a given class which doesn't have an hibernate mapping ? My classes are like : Publication - Blog - Question ...

63. JPA/Hibernate with InheritanceType.JOINED doing select on subclass

I have a basic inheritance hierarchy setup using JPA and Hibernate that looks something like the following:

@Inheritance(strategy = InheritanceType.JOINED)
public class User {

    @GeneratedValue(strategy = ...

64. Hibernate: table per concrete class vs. table per subclass

im planing / designing my database for my current projct (java). For this project im going to use hibernate. In my project i have a base class/interface called Command. In fact the ...

65. Table Per Subclass Vs Table Per concrete class in hibernate?

In most of the web application, i have seen one base class consisting common properties and number of subclasses extending base class . So my question here is which strategy ...

66. Hibernate joined-subclass update

I have a base class, A, and two subclasses, B and C. I have an instance of B that I am trying to update and change the type to C. The ...

67. Netbeans Hibernate Autogenerate Joined Subclasses

Hello Everyone , I am starting into the world of hibernate. I am using the autogenerate pojos from database mappings but am not getting what I want. When the generator runs ...

68. Hibernate - Using subclass and joined-subclass element

Hi, Im having a problem with using and . I have a base class Question (mapped onto table: Question) from which 2 classes extend namely, EssayQuestion and MCQ. I have mapped MCQ onto the same table: Question. Thus I have used for MCQ mapping. For this I have specified a discriminator column in Question table and discriminator values in ...

69. Hibernate: Simpler query with many subclasses

We have object relationship that basically a common entity as the parent with many child classes which specialize with different attribute. We map those relationship using joined-subclass so parent and each children have their own table. And there are cases where we would like to query just information that is available in parent table only. How can we do that? Currently ...

70. Hibernate: subclass with foreign key as discriminator

G'day to all! I'm currently trying to implement some kind different kind of Lookup Values in one Table. So for instance a Currency, Country, Cities etc, etc all in one table. Looking something like this: id, code, description, type_id where type is an id pointing to a meta lookupvalue expressing the type like Currency, Country, City etc. I want to implement ...

71. hibernate HibernateTemplate.get() returns incorrect subclass instance

Hi all, I've just run into an unexpected problem with hibernate: i make a HibernateTemplate.get(myClass.class, id) call to get back an instance of my object. While testing for incorrect results, i tried the id of a sibling subclass...and it returned an object of the incorrect type! This is most strange to me. Hopefully it is just a problem with my hibernate ...

73. hibernate mapping to subclass

hi guys this must be simple, but i am new to this filed. This is my xml file in hibernate Select * from DBWD.FMT_CASEADR WHERE ATTYID = ? +++++++++++++++++++++++ Now Inside TestVO , i have only the ...

74. JPA / JPQL : Order by Subclass

Hi, i have a question regarding JPA and "Order By". I have the following class hierarchy: - BaseAccount | |-> CustomerAccount |-> BusinessAccount The inheritance strategy is JOINED. I want to retrieve a list of BaseAccount's ordered by their subclass. E.g. first all BusinessAccounts and then all CustomerAccounts. Is there a way to formulate this in JPQL? This is the first ...

75. caching the subclass in Hibernate

76. Hibernate: Select only from "Parent"-table in union-subclass

Hi fellow saloonists! I have class A and Class B extends A, both are mapped using union-subclass in Hibernate. I'm using a defined in A. My program uses a "from A a where =:var1" query, which should return one or zero rows. If zero rows are returned then save A. If one row is returned, then create ...

78. Multiple subclasses, one superclass in Hibernate.

Hi, thanks for another quick reply. The scenario I've depicted is one I made up to illustrate the hypothetical situation of a non-linear inheritance. I used your example from Hibernate made easy as a base. The problem is that the book only deals with the linear case of inheritance (Ancestor <- Parent <- Child). Let's say that I have a Person-class ...

80. Hibernate Table per subclass: using a discriminator

The argument here is: "Hibernate's implementation of table per subclass does not require a discriminator column. Other object/relational mappers use a different implementation of table per subclass that requires a type discriminator column in the superclass table. The approach taken by Hibernate is much more difficult to implement, but arguably more correct from a relational point of view." One of the ...

81. Hibernate: multiple subclasses not using fetchmode join

Hi, I'm having weird issues trying to use fetch="join" on the properties of more than one subclass of my hibernate mapping file. First, the situation: I have a constellation of one-to-many tables related by foreign key to a single parent table. The parent table defines the superclass, then overlapping subsets of the related tables define the subclasses. So subclass A will ...

82. JPA: Joined Table ID of subclass

Hi everyone, How do I override the column name of the ID of my subclass entity? Say the superclass Parent is mapped to table "parent" and its id is mapped to column "id", then I have this subclass called Child mapped to table "child" having its id mapped to column "id". I want my subclass Child to get its id mapped ...

83. Hibernate subclasses

84. Hibernate table-per-subclass mapping

86. name-enity not used in subclass mapping definition.

Due to a bug in version 3.3.2 (and earlier version maybe), the entity-name attribute isn't used correctly in a subclass mapping. Due to this bug, Hibernate will throw an exceptions when performing a save/update actions on the "first run". It will the perform a second run to find the entity name (= class name), and if you are not using entity-name ...

87. Using joined-subclass, and inheritance - Help -

I am trying to write a sample app that uses the joined-subclass feature in hibernate. I am using the Empleado (Empleyed) and Contador (Counter). I can only run the method to add a counter, I can not delete, or list. I dont know what is the problem, help me please!!! Any help would be get me is very welcome. Sorry about ...

88. Blob problem ONLY when using joined subclass

Newbie Joined: Fri Aug 14, 2009 8:06 am Posts: 19 I have implemented a subclass ImageBlob of BlobImpl in order to store images in the db. This works perfectly if I map the class containing this blob with . However, since most of my persistent classes are subclasses of PersistentObject I wanted to map it as a as soon as ...

89. Table per subclass with discriminator

90. problem in union subclass startgey

91. mapping subclass w/ discriminator, join table & one-to-one

the following mapping is wrong because inside it isn't allowed to have both and can someone hint me on the correct way to get this working ? I think it would work if i map both service types to diferent tables but i don't want to repeat the shared columns. Another way would be to ...

92. Relationship Column per Subclass with Discriminator Column

Code: @Entity @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public abstract class TE { @Id @GenericGenerator(name = "idGenerator", strategy = "assigned") @GeneratedValue(generator = "idGenerator") @Column(name="TE_KEY") private String id; @OneToMany @JoinColumn(name="TA_KEY", referencedColumnName="TE_KEY", insertable=false, updatable=false) private List ...

93. joined subclass issues

94. Inheritance with more than one subclasses

95. Union subclass mapping.

Hi, everyone. I've got a problem with Hibernate union-subclass mapping. Sorry, I've changed table and entity names but the whole meaning is the same. The problem is that the properties of particular union subclass are not being resolved at all. I get nulls when I try to get vendorConfirmationNum from some.entity.ChildEntityTwo, though I've executed queries from hibernate SQL logs for this ...

96. how to persist an object of a subclass from an existing supe

Newbie Joined: Tue Feb 09, 2010 7:32 am Posts: 1 Hello, I have a user entity which inherits from collaborator entity, and i use @Inheritance(strategy = InheritanceType.JOINED) in the Collaborator entity. I do not find the way to persist a user entity that belongs to an existing collaborator, I can add a user object and it results to 2 new created ...

97. Inconsistent behaviour when fetching cached subclass entitie

Hi all, Enabling the hibernate second-level cache is leading to inconsistent behaviour when fetching cached subclass entities as opposed to fetching non-cached subclass entities. Consider a simple scenario having a parent entity: Code: @Entity @Table(name = "tt_test") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public abstract class ParentEntity implements Serializable { private static final long serialVersionUID = ...

98. Unique index and foreign keys with union-subclass

Given, Abstract ClassA. - id property - property-1 - property-2 - many-to-one to ClassX ClassB extends ClassA - id property - property-3 ClassC extends ClassA - id property - property-4 ClassX -id property -property-5 Above situation is mapped using three hibernate hbm xml files: - one for abstract ClassA, mapped with attribute abstract="true". - one for concrete ClassB, mapped with union-subclass ...

99. Subclass one-to-one - Unknown column issue

Hi Team, Below are my pojos Code: class SuperA{} abstract class SubA extends SuperA{} class SubB extends SubA{ SubC subC; } class SubC extends SubA{ SubB subB; } And below are my mapping files Code: ...

100. Unknown column on subclass object

Hi, I am trying to fetch an object of a subclass. But it throws Unknown column error, even though the particular column is available in the subclass table. If i add the same in the super class table it is not throwing the error. But it won't work properly. Please help me resolve this. Let me know, If you want to ...