Simple project demonstrating the use of JPA's @Inheritance(strategy = InheritanceType.JOINED)
for subclassing.
In reply to a question at StackOverflow
- Inheritance Strategies with JPA and Hibernate – The Complete Guide
- How to order entity subclasses by their class type using JPA and Hibernate
- Hibernate Inheritance Mapping
Uses Hibernate and H2 in-memory database:
- Main - persists 3 employees and executes the following query:
SELECT COUNT(e) FROM Employee e
Oct 22, 2019 5:48:23 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [name: jpa_inheritance_demo]
Oct 22, 2019 5:48:24 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.6.Final}
Oct 22, 2019 5:48:24 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
Oct 22, 2019 5:48:24 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Oct 22, 2019 5:48:25 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:mem:jpa_inheritance;DB_CLOSE_DELAY=-1]
Oct 22, 2019 5:48:25 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=sa}
Oct 22, 2019 5:48:25 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Oct 22, 2019 5:48:25 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Oct 22, 2019 5:48:25 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Hibernate:
drop table Employee if exists
Oct 22, 2019 5:48:26 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@4228bf58] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate:
drop table Person if exists
Hibernate:
create table Employee (
position varchar(255),
employee_id integer not null,
primary key (employee_id)
)
Oct 22, 2019 5:48:26 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@38ed139b] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate:
create table Person (
person_id integer generated by default as identity,
primary key (person_id)
)
Hibernate:
alter table Employee
add constraint FKcb8gcn8i0fjy6hmqcka70r0r4
foreign key (employee_id)
references Person
Oct 22, 2019 5:48:26 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate:
insert
into
Person
(person_id)
values
(null)
Hibernate:
insert
into
Employee
(position, employee_id)
values
(?, ?)
Hibernate:
insert
into
Person
(person_id)
values
(null)
Hibernate:
insert
into
Employee
(position, employee_id)
values
(?, ?)
Hibernate:
insert
into
Person
(person_id)
values
(null)
Hibernate:
insert
into
Employee
(position, employee_id)
values
(?, ?)
Oct 22, 2019 5:48:26 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PoolState stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:h2:mem:jpa_inheritance;DB_CLOSE_DELAY=-1]
Hibernate:
select
count(employee0_.employee_id) as col_0_0_
from
Employee employee0_
Employees: 3
Process finished with exit code 0