BAEL-6265: PersistenceUnit vs PersistenceContext

This commit is contained in:
Dragomir Alin 2024-03-29 13:35:44 +02:00
parent a822f2ed0c
commit 551df74b9c
10 changed files with 311 additions and 0 deletions

View File

@ -0,0 +1,68 @@
<?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-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>org.hibernate</groupId>
<artifactId>hibernate-testing</artifactId>
<version>${hibernate.version}</version>
</dependency>
</dependencies>
<properties>
<hibernate.version>6.4.2.Final</hibernate.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,15 @@
package com.baeldung.contextvsunit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.baeldung.contextvsunit")
public class PersistenceContextVsUnitApplication {
public static void main(String[] args) {
SpringApplication.run(PersistenceContextVsUnitApplication.class, args);
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.contextvsunit.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "PRODUCT")
public class Product {
@Id
private Long id;
private String name;
private double price;
public Product() {
}
public Product(Long id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.contextvsunit.service;
import org.springframework.stereotype.Service;
import com.baeldung.contextvsunit.entity.Product;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.PersistenceContextType;
import jakarta.transaction.Transactional;
@Service
public class PersistenceContextProductService {
@PersistenceContext(type = PersistenceContextType.TRANSACTION)
private EntityManager entityManagerTransactionType;
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager entityManagerExtendedType;
@Transactional
public void insertProductWithTransactionTypePersistenceContext(Product product) {
entityManagerTransactionType.persist(product);
}
public Product findWithTransactionTypePersistenceContext(long id) {
return entityManagerTransactionType.find(Product.class, id);
}
public void insertProductWithExtendedTypePersistenceContext(Product product) {
entityManagerExtendedType.persist(product);
}
public Product findWithExtendedTypePersistenceContext(long id) {
return entityManagerExtendedType.find(Product.class, id);
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.contextvsunit.service;
import org.springframework.stereotype.Service;
import com.baeldung.contextvsunit.entity.Product;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.PersistenceUnit;
import jakarta.transaction.Transactional;
@Service
public class PersistenceUnitProductService {
@PersistenceUnit(name = "com.baeldung.contextvsunit.h2_persistence_unit")
private EntityManagerFactory emf;
@Transactional
public void insertProduct(Product product) {
EntityManager entityManager = emf.createEntityManager();
entityManager.persist(product);
}
public Product find(long id) {
EntityManager entityManager = emf.createEntityManager();
return entityManager.find(Product.class, id);
}
}

View File

@ -0,0 +1,22 @@
<?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 name="com.baeldung.contextvsunit.h2_persistence_unit" transaction-type="RESOURCE_LOCAL">
<description>EntityManager serializable persistence unit</description>
<class>com.baeldung.contextvsunit.entity.Product</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=""/>
</properties>
</persistence-unit>
</persistence>

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

@ -0,0 +1,57 @@
package com.baeldung.contextvsunit;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.contextvsunit.entity.Product;
import com.baeldung.contextvsunit.service.PersistenceContextProductService;
import com.baeldung.contextvsunit.service.PersistenceUnitProductService;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = com.baeldung.contextvsunit.PersistenceContextVsUnitApplication.class)
public class PersistenceContextVsUnitProductServiceTest {
@Autowired
private PersistenceContextProductService persistenceContextProductService;
@Autowired
private PersistenceUnitProductService persistenceUnitProductService;
@Test
public void whenProductPersistWithTransactionPersistenceContext_thenShouldPersist() {
Product p = new Product(1L, "Product 1", 100.0);
persistenceContextProductService.insertProductWithTransactionTypePersistenceContext(p);
Product productFromTransactionScoped = persistenceContextProductService.findWithTransactionTypePersistenceContext(1L);
Assertions.assertNotNull(productFromTransactionScoped);
Product productFromExtendedScoped = persistenceContextProductService.findWithExtendedTypePersistenceContext(1L);
Assertions.assertNotNull(productFromExtendedScoped);
}
@Test
public void whenProductPersistWithExtendedPersistence_thenShouldPersist() {
Product product = new Product(2L, "Product 1", 100.0);
persistenceContextProductService.insertProductWithExtendedTypePersistenceContext(product);
Product productFromExtendedScoped = persistenceContextProductService.findWithExtendedTypePersistenceContext(2L);
Assertions.assertNotNull(productFromExtendedScoped);
Product productFromTransactionScoped = persistenceContextProductService.findWithTransactionTypePersistenceContext(2L);
Assertions.assertNull(productFromTransactionScoped);
}
@Test
public void whenProductPersistWithEntityManagerFactory_thenShouldPersist() {
Product p = new Product(1L, "Product 1", 100.0);
persistenceUnitProductService.insertProduct(p);
Product createdProduct = persistenceUnitProductService.find(1L);
assertNotNull(createdProduct);
}
}

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

@ -35,6 +35,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>