Java hibernate inheritance

www.l‮ruttua‬i.com
Java hibernate inheritance

Hibernate is an open-source object-relational mapping (ORM) framework for the Java programming language that provides support for mapping objects to relational database tables. Hibernate allows you to define inheritance relationships between objects in your application and map them to corresponding database tables using a variety of inheritance mapping strategies.

There are several types of inheritance mapping strategies supported by Hibernate:

  • Single table per class hierarchy: This strategy maps the entire class hierarchy to a single database table, using a discriminator column to differentiate between the different classes.
  • Table per class: This strategy maps each class to a separate database table, with no inheritance relationship between the tables.
  • Table per concrete class: This strategy maps each concrete class (a class with no subclasses) to a separate database table, with no inheritance relationship between the tables.
  • Mixed inheritance: This strategy combines elements of the other inheritance mapping strategies, allowing you to use different strategies for different classes in the hierarchy.

To use inheritance mapping in Hibernate, you need to define the inheritance relationship between your classes using the @Inheritance annotation, and specify the inheritance strategy using the strategy attribute of the annotation. You also need to specify the discriminator column, if applicable, using the discriminatorColumn attribute.

Here is an example of how to use the single table per class hierarchy strategy in Hibernate:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class Shape {
    // ...
}

@Entity
@DiscriminatorValue("circle")
public class Circle extends Shape {
    // ...
}

@Entity
@DiscriminatorValue("rectangle")
public class Rectangle extends Shape {
    // ...
}

In this example, the Shape class is defined as an abstract entity class and annotated with the @Inheritance and @DiscriminatorColumn annotations. The InheritanceType.SINGLE_TABLE value is specified as the inheritance strategy, and the "type" column is defined as the discriminator column using the DiscriminatorType.STRING type. The Circle and Rectangle classes are defined as concrete entity classes and annotated with the @DiscriminatorValue annotation, specifying the values "circle" and "rectangle" as the discriminator values, respectively.

This configuration will map the Shape, Circle, and Rectangle classes to a single database table, with a type column used to differentiate between the different classes. The Circle and Rectangle objects will be stored in the table with the respective discriminator values, allowing Hibernate to determine the class of each object when it is retrieved from the database.

You can use this approach to map inheritance relationships between your objects and database tables using the single table per class hierarchy strategy in Hibernate. You can also customize this approach by using different inheritance strategies or by defining different discriminator columns and values.

Created Time:2017-11-03 23:27:14  Author:lautturi