JAVA-33620 Split or move hibernate-jpa module (#16362)

This commit is contained in:
timis1 2024-04-16 11:09:12 +03:00 committed by GitHub
parent ebd95b4840
commit 960af85d50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
29 changed files with 297 additions and 6 deletions

View File

@ -0,0 +1,10 @@
## Hibernate JPA
This module contains articles specific to use of Hibernate as a JPA implementation, such as Locking, Bootstrapping, One-to-One Relationship, Persistence Context, and more.
### Relevant articles:
- [JPA/Hibernate Persistence Context](https://www.baeldung.com/jpa-hibernate-persistence-context)
- [Quick Guide to EntityManager#getReference()](https://www.baeldung.com/jpa-entity-manager-get-reference)
- [JPA Entities and the Serializable Interface](https://www.baeldung.com/jpa-entities-serializable)
- [The @Struct Annotation Type in Hibernate Structured User-Defined Types](https://www.baeldung.com/java-hibernate-struct-annotation)

View File

@ -0,0 +1,95 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>hibernate-jpa-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hibernate-jpa-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>persistence-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring-boot.version}</version>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>ch.vorburger.mariaDB4j</groupId>
<artifactId>mariaDB4j</artifactId>
<version>${mariaDB4j.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-testing</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
</dependencies>
<properties>
<mysql.version>8.2.0</mysql.version>
<hibernate.version>6.4.2.Final</hibernate.version>
<mariaDB4j.version>2.6.0</mariaDB4j.version>
<spring-boot.version>3.0.4</spring-boot.version>
<h2.version>2.1.214</h2.version>
<org.slf4j.version>2.0.7</org.slf4j.version>
<logback.version>1.4.6</logback.version>
</properties>
</project>

View File

@ -0,0 +1,3 @@
create table Game (id bigint not null, name varchar(255), primary key (id));
create table Player (id bigint not null, name varchar(255), game_id bigint, primary key (id));
alter table Player add constraint FKohr86afuapoujklti79wo27aa foreign key (game_id) references Game(id);

View File

@ -0,0 +1,5 @@
insert into Game (id, name) values (1, 'Game 1');
insert into Game (id, name) values (2, 'Game 2');
insert into Player (game_id, name, id) values (null, 'Player 1', 1);
insert into Player (game_id, name, id) values (null, 'Player 2', 2);
insert into Player (game_id, name, id) values (null, 'Player 3', 3);

View File

@ -0,0 +1,2 @@
drop table if exists Player;
drop table if exists Game;

View File

@ -0,0 +1,128 @@
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<!-- Persistence unit for H2 -->
<persistence-unit name="com.baeldung.hibernate.entitymanager.game_player_h2">
<description>EntityManager getReference persistence unit</description>
<class>com.baeldung.hibernate.entitymanager.getreference.Game</class>
<class>com.baeldung.hibernate.entitymanager.getreference.Player</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- even if we set exclude-unlisted-classes=true, strangely it's not enough to exclude defined hbm files in hibernate.cfg.xml.
So, some entities out of the scope unwillingly managed by our persistence unit. In order to prevent this, we disable autodetection
of the hbm files completely. When we set this property, hibernate ignores the exclude-unlisted-classes property. -->
<property name="hibernate.archive.autodetection" value=""/>
<property name="hibernate.jpa.compliance.proxy" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.generate_statistics" value="false"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="jakarta.persistence.jdbc.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1"/>
<property name="jakarta.persistence.jdbc.user" value="sa"/>
<property name="jakarta.persistence.jdbc.password" value=""/>
<property name="hibernate.id.db_structure_naming_strategy" value="legacy" />
<!-- ensure backward compatibility -->
<property name="hibernate.type.preferred_duration_jdbc_type" value="BIGINT" />
<property name="hibernate.type.preferred_instant_jdbc_type" value="TIMESTAMP" />
</properties>
</persistence-unit>
<!-- Persistence unit for MySQL -->
<persistence-unit name="com.baeldung.hibernate.entitymanager.game_player_mysql">
<description>EntityManager getReference persistence unit</description>
<class>com.baeldung.hibernate.entitymanager.getreference.Game</class>
<class>com.baeldung.hibernate.entitymanager.getreference.Player</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- even if we set exclude-unlisted-classes=true, strangely it's not enough to exclude defined hbm files in hibernate.cfg.xml.
So, some entities out of the scope unwillingly managed by our persistence unit. In order to prevent this, we disable autodetection
of the hbm files completely. When we set this property, hibernate ignores the exclude-unlisted-classes property. -->
<property name="hibernate.archive.autodetection" value=""/>
<property name="hibernate.jpa.compliance.proxy" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.generate_statistics" value="false"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/baeldung"/>
<property name="jakarta.persistence.jdbc.user" value="root"/>
<property name="jakarta.persistence.jdbc.password" value="password"/>
<property name="jakarta.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="jakarta.persistence.schema-generation.create-script-source" value="/META-INF/create-db.sql"/>
<property name="jakarta.persistence.schema-generation.drop-script-source" value="/META-INF/drop-db.sql"/>
<property name="jakarta.persistence.sql-load-script-source" value="/META-INF/data.sql"/>
<property name="hibernate.id.db_structure_naming_strategy" value="legacy" />
<!-- ensure backward compatibility -->
<property name="hibernate.type.preferred_duration_jdbc_type" value="BIGINT" />
<property name="hibernate.type.preferred_instant_jdbc_type" value="TIMESTAMP" />
</properties>
</persistence-unit>
<!-- Persistence unit for PostgreSQL -->
<persistence-unit name="com.baeldung.hibernate.entitymanager.game_player_postgresql">
<description>EntityManager getReference persistence unit</description>
<class>com.baeldung.hibernate.entitymanager.getreference.Game</class>
<class>com.baeldung.hibernate.entitymanager.getreference.Player</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- even if we set exclude-unlisted-classes=true, strangely it's not enough to exclude defined hbm files in hibernate.cfg.xml.
So, some entities out of the scope unwillingly managed by our persistence unit. In order to prevent this, we disable autodetection
of the hbm files completely. When we set this property, hibernate ignores the exclude-unlisted-classes property. -->
<property name="hibernate.archive.autodetection" value=""/>
<property name="hibernate.jpa.compliance.proxy" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.generate_statistics" value="false"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="jakarta.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres"/>
<property name="jakarta.persistence.jdbc.user" value="postgres"/>
<property name="jakarta.persistence.jdbc.password" value=""/>
<property name="jakarta.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="jakarta.persistence.schema-generation.create-script-source" value="/META-INF/create-db.sql"/>
<property name="jakarta.persistence.schema-generation.drop-script-source" value="/META-INF/drop-db.sql"/>
<property name="jakarta.persistence.sql-load-script-source" value="/META-INF/data.sql"/>
<property name="hibernate.id.db_structure_naming_strategy" value="legacy" />
<!-- ensure backward compatibility -->
<property name="hibernate.type.preferred_duration_jdbc_type" value="BIGINT" />
<property name="hibernate.type.preferred_instant_jdbc_type" value="TIMESTAMP" />
</properties>
</persistence-unit>
<persistence-unit name="com.baeldung.hibernate.serializable.h2_persistence_unit">
<description>EntityManager serializable persistence unit</description>
<class>com.baeldung.hibernate.serializable.Email</class>
<class>com.baeldung.hibernate.serializable.Account</class>
<class>com.baeldung.hibernate.serializable.User</class>
<class>com.baeldung.hibernate.serializable.UserId</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.generate_statistics" value="false"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="jakarta.persistence.jdbc.url" value="jdbc:h2:mem:db2;DB_CLOSE_DELAY=-1"/>
<property name="jakarta.persistence.jdbc.user" value="sa"/>
<property name="jakarta.persistence.jdbc.password" value=""/>
<property name="hibernate.id.db_structure_naming_strategy" value="legacy" />
<!-- ensure backward compatibility -->
<property name="hibernate.type.preferred_duration_jdbc_type" value="BIGINT" />
<property name="hibernate.type.preferred_instant_jdbc_type" value="TIMESTAMP" />
<property name="hibernate.globally_quoted_identifiers" value="true"/>
</properties>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false" xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="org.hibernate" additivity="false">
<level value="ERROR" />
<appender-ref ref="console" />
</logger>
<root>
<level value="INFO" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -12,7 +12,6 @@ import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;

View File

@ -0,0 +1,17 @@
hibernate.connection.driver_class=org.h2.Driver
hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1
hibernate.connection.username=sa
hibernate.connection.autocommit=true
jdbc.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
# enable to see Hibernate generated SQL
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.acquire_increment=5
hibernate.c3p0.timeout=1800

View File

@ -10,8 +10,4 @@ This module contains articles specific to use of Hibernate as a JPA implementati
- [Optimistic Locking in JPA](https://www.baeldung.com/jpa-optimistic-locking)
- [Criteria API An Example of IN Expressions](https://www.baeldung.com/jpa-criteria-api-in-expressions)
- [One-to-One Relationship in JPA](https://www.baeldung.com/jpa-one-to-one)
- [Enabling Transaction Locks in Spring Data JPA](https://www.baeldung.com/java-jpa-transaction-locks)
- [JPA/Hibernate Persistence Context](https://www.baeldung.com/jpa-hibernate-persistence-context)
- [Quick Guide to EntityManager#getReference()](https://www.baeldung.com/jpa-entity-manager-get-reference)
- [JPA Entities and the Serializable Interface](https://www.baeldung.com/jpa-entities-serializable)
- [The @Struct Annotation Type in Hibernate Structured User-Defined Types](https://www.baeldung.com/java-hibernate-struct-annotation)
- [Enabling Transaction Locks in Spring Data JPA](https://www.baeldung.com/java-jpa-transaction-locks)

View File

@ -36,6 +36,7 @@
<module>hibernate-exceptions</module>
<module>hibernate-libraries</module>
<module>hibernate-jpa</module>
<module>hibernate-jpa-2</module>
<module>hibernate-queries</module>
<module>hibernate-enterprise</module>
<module>influxdb</module>