Merge pull request #16263 from DragomirAlin/master

BAEL-6265: PersistenceUnit vs PersistenceContext
This commit is contained in:
Vini 2024-04-22 18:07:40 +02:00 committed by GitHub
commit 378cf78e7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 208 additions and 1 deletions

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

@ -125,4 +125,21 @@
</properties>
</persistence-unit>
</persistence>
<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,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 PersistenceContextVsUnitProductServiceIntegrationTest {
@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);
}
}