[BAEL-12749] - Moved articles from persistence-modules/spring-jpa to persistence-modules/spring-hibernate-5
This commit is contained in:
@@ -7,9 +7,6 @@
|
||||
- [The DAO with JPA and Spring](http://www.baeldung.com/spring-dao-jpa)
|
||||
- [JPA Pagination](http://www.baeldung.com/jpa-pagination)
|
||||
- [Sorting with JPA](http://www.baeldung.com/jpa-sort)
|
||||
- [Hibernate Second-Level Cache](http://www.baeldung.com/hibernate-second-level-cache)
|
||||
- [Spring, Hibernate and a JNDI Datasource](http://www.baeldung.com/spring-persistence-jpa-jndi-datasource)
|
||||
- [Deleting Objects with Hibernate](http://www.baeldung.com/delete-with-hibernate)
|
||||
- [Self-Contained Testing Using an In-Memory Database](http://www.baeldung.com/spring-jpa-test-in-memory-database)
|
||||
- [A Guide to Spring AbstractRoutingDatasource](http://www.baeldung.com/spring-abstract-routing-data-source)
|
||||
- [A Guide to Hibernate with Spring 4](http://www.baeldung.com/the-persistence-layer-with-spring-and-jpa)
|
||||
|
||||
@@ -44,11 +44,6 @@
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-ehcache</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xml-apis</groupId>
|
||||
<artifactId>xml-apis</artifactId>
|
||||
|
||||
-66
@@ -1,66 +0,0 @@
|
||||
package org.baeldung.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.naming.NamingException;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jndi.JndiTemplate;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource("classpath:persistence-jndi.properties")
|
||||
@ComponentScan("org.baeldung.persistence")
|
||||
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
|
||||
public class PersistenceJNDIConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan("org.baeldung.persistence.model");
|
||||
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
||||
em.setJpaProperties(additionalProperties());
|
||||
return em;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() throws NamingException {
|
||||
return (DataSource) new JndiTemplate().lookup(env.getProperty("jdbc.url"));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
|
||||
return new JpaTransactionManager(emf);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||
return new PersistenceExceptionTranslationPostProcessor();
|
||||
}
|
||||
|
||||
final Properties additionalProperties() {
|
||||
final Properties hibernateProperties = new Properties();
|
||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||
hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", "false");
|
||||
return hibernateProperties;
|
||||
}
|
||||
}
|
||||
-89
@@ -1,89 +0,0 @@
|
||||
package org.baeldung.config;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Properties;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-h2.properties" })
|
||||
@ComponentScan({ "org.baeldung.persistence" })
|
||||
@EnableJpaRepositories(basePackages = { "org.baeldung.persistence.dao", "org.baeldung.persistence.repository" })
|
||||
public class PersistenceJPAConfigL2Cache {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public PersistenceJPAConfigL2Cache() {
|
||||
super();
|
||||
}
|
||||
|
||||
// beans
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan(getPackagesToScan());
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
em.setJpaProperties(additionalProperties());
|
||||
|
||||
return em;
|
||||
}
|
||||
|
||||
protected String[] getPackagesToScan() {
|
||||
return new String[] { "org.baeldung.persistence.model" };
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
||||
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
|
||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||
transactionManager.setEntityManagerFactory(emf);
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||
return new PersistenceExceptionTranslationPostProcessor();
|
||||
}
|
||||
|
||||
final Properties additionalProperties() {
|
||||
final Properties hibernateProperties = new Properties();
|
||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||
hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache"));
|
||||
hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache"));
|
||||
hibernateProperties.setProperty("hibernate.cache.region.factory_class", env.getProperty("hibernate.cache.region.factory_class"));
|
||||
hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
|
||||
return hibernateProperties;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,7 +7,4 @@ jdbc.pass=
|
||||
# hibernate.X
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
hibernate.show_sql=true
|
||||
hibernate.hbm2ddl.auto=create-drop
|
||||
hibernate.cache.use_second_level_cache=true
|
||||
hibernate.cache.use_query_cache=true
|
||||
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
||||
hibernate.hbm2ddl.auto=create-drop
|
||||
@@ -1,8 +0,0 @@
|
||||
# jdbc.X
|
||||
jdbc.url=java:comp/env/jdbc/BaeldungDatabase
|
||||
|
||||
# hibernate.X
|
||||
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
|
||||
hibernate.show_sql=false
|
||||
#hibernate.hbm2ddl.auto=create
|
||||
hibernate.hbm2ddl.auto=update
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfigL2Cache;
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
@@ -10,7 +10,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@WebAppConfiguration
|
||||
@DirtiesContext
|
||||
public class SpringContextIntegrationTest {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfigL2Cache;
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
@@ -10,7 +10,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@WebAppConfiguration
|
||||
@DirtiesContext
|
||||
public class SpringContextTest {
|
||||
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
package org.baeldung.persistence.deletion.config;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfigL2Cache;
|
||||
|
||||
public class PersistenceJPAConfigDeletion extends PersistenceJPAConfigL2Cache {
|
||||
|
||||
public PersistenceJPAConfigDeletion() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getPackagesToScan() {
|
||||
return new String[] { "org.baeldung.persistence.deletion.model", "org.baeldung.persistence.model" };
|
||||
}
|
||||
}
|
||||
-60
@@ -1,60 +0,0 @@
|
||||
package org.baeldung.persistence.deletion.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "BAR")
|
||||
public class Bar {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
List<Baz> bazList = new ArrayList<>();
|
||||
|
||||
public Bar() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Bar(final String name) {
|
||||
super();
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<Baz> getBazList() {
|
||||
return bazList;
|
||||
}
|
||||
|
||||
public void setBazList(final List<Baz> bazList) {
|
||||
this.bazList = bazList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Bar [name=").append(name).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
-47
@@ -1,47 +0,0 @@
|
||||
package org.baeldung.persistence.deletion.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "BAZ")
|
||||
public class Baz {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
public Baz() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Baz(final String name) {
|
||||
super();
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Bar [name=").append(name).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
-63
@@ -1,63 +0,0 @@
|
||||
package org.baeldung.persistence.deletion.model;
|
||||
|
||||
import org.hibernate.annotations.Where;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "FOO")
|
||||
@Where(clause = "DELETED = 0")
|
||||
public class Foo {
|
||||
|
||||
public Foo() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Foo(final String name) {
|
||||
super();
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "ID")
|
||||
private long id;
|
||||
|
||||
@Column(name = "NAME")
|
||||
private String name;
|
||||
|
||||
@Column(name = "DELETED")
|
||||
private Integer deleted = 0;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "BAR_ID")
|
||||
private Bar bar;
|
||||
|
||||
public Bar getBar() {
|
||||
return bar;
|
||||
}
|
||||
|
||||
public void setBar(final Bar bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setDeleted() {
|
||||
this.deleted = 1;
|
||||
}
|
||||
}
|
||||
-157
@@ -1,157 +0,0 @@
|
||||
package org.baeldung.persistence.service;
|
||||
|
||||
import org.baeldung.persistence.deletion.config.PersistenceJPAConfigDeletion;
|
||||
import org.baeldung.persistence.deletion.model.Bar;
|
||||
import org.baeldung.persistence.deletion.model.Baz;
|
||||
import org.baeldung.persistence.deletion.model.Foo;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfigDeletion.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@DirtiesContext
|
||||
public class DeletionIntegrationTest {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
@Autowired
|
||||
private PlatformTransactionManager platformTransactionManager;
|
||||
|
||||
@Before
|
||||
public final void before() {
|
||||
entityManager.getEntityManagerFactory().getCache().evictAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public final void givenEntityIsRemoved_thenItIsNotInDB() {
|
||||
Foo foo = new Foo("foo");
|
||||
entityManager.persist(foo);
|
||||
flushAndClear();
|
||||
|
||||
foo = entityManager.find(Foo.class, foo.getId());
|
||||
assertThat(foo, notNullValue());
|
||||
|
||||
entityManager.remove(foo);
|
||||
flushAndClear();
|
||||
|
||||
assertThat(entityManager.find(Foo.class, foo.getId()), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public final void givenEntityIsRemovedAndReferencedByAnotherEntity_thenItIsNotRemoved() {
|
||||
Bar bar = new Bar("bar");
|
||||
Foo foo = new Foo("foo");
|
||||
foo.setBar(bar);
|
||||
entityManager.persist(foo);
|
||||
flushAndClear();
|
||||
|
||||
foo = entityManager.find(Foo.class, foo.getId());
|
||||
bar = entityManager.find(Bar.class, bar.getId());
|
||||
entityManager.remove(bar);
|
||||
flushAndClear();
|
||||
|
||||
bar = entityManager.find(Bar.class, bar.getId());
|
||||
assertThat(bar, notNullValue());
|
||||
|
||||
foo = entityManager.find(Foo.class, foo.getId());
|
||||
foo.setBar(null);
|
||||
entityManager.remove(bar);
|
||||
flushAndClear();
|
||||
|
||||
assertThat(entityManager.find(Bar.class, bar.getId()), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public final void givenEntityIsRemoved_thenRemovalIsCascaded() {
|
||||
Bar bar = new Bar("bar");
|
||||
Foo foo = new Foo("foo");
|
||||
foo.setBar(bar);
|
||||
entityManager.persist(foo);
|
||||
flushAndClear();
|
||||
|
||||
foo = entityManager.find(Foo.class, foo.getId());
|
||||
entityManager.remove(foo);
|
||||
flushAndClear();
|
||||
|
||||
assertThat(entityManager.find(Foo.class, foo.getId()), nullValue());
|
||||
assertThat(entityManager.find(Bar.class, bar.getId()), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public final void givenEntityIsDisassociated_thenOrphanRemovalIsApplied() {
|
||||
Bar bar = new Bar("bar");
|
||||
Baz baz = new Baz("baz");
|
||||
bar.getBazList().add(baz);
|
||||
entityManager.persist(bar);
|
||||
flushAndClear();
|
||||
|
||||
bar = entityManager.find(Bar.class, bar.getId());
|
||||
baz = bar.getBazList().get(0);
|
||||
bar.getBazList().remove(baz);
|
||||
flushAndClear();
|
||||
|
||||
assertThat(entityManager.find(Baz.class, baz.getId()), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public final void givenEntityIsDeletedWithJpaBulkDeleteStatement_thenItIsNotInDB() {
|
||||
Foo foo = new Foo("foo");
|
||||
entityManager.persist(foo);
|
||||
flushAndClear();
|
||||
|
||||
entityManager.createQuery("delete from Foo where id = :id").setParameter("id", foo.getId()).executeUpdate();
|
||||
|
||||
assertThat(entityManager.find(Foo.class, foo.getId()), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public final void givenEntityIsDeletedWithNativeQuery_thenItIsNotInDB() {
|
||||
Foo foo = new Foo("foo");
|
||||
entityManager.persist(foo);
|
||||
flushAndClear();
|
||||
|
||||
entityManager.createNativeQuery("delete from FOO where ID = :id").setParameter("id", foo.getId()).executeUpdate();
|
||||
|
||||
assertThat(entityManager.find(Foo.class, foo.getId()), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public final void givenEntityIsSoftDeleted_thenItIsNotReturnedFromQueries() {
|
||||
Foo foo = new Foo("foo");
|
||||
entityManager.persist(foo);
|
||||
flushAndClear();
|
||||
|
||||
foo = entityManager.find(Foo.class, foo.getId());
|
||||
foo.setDeleted();
|
||||
flushAndClear();
|
||||
|
||||
assertThat(entityManager.find(Foo.class, foo.getId()), nullValue());
|
||||
}
|
||||
|
||||
private void flushAndClear() {
|
||||
entityManager.flush();
|
||||
entityManager.clear();
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -16,7 +16,6 @@ import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.baeldung.config.PersistenceJPAConfigL2Cache;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -28,7 +27,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@DirtiesContext
|
||||
public class FooPaginationPersistenceIntegrationTest {
|
||||
|
||||
|
||||
+1
-2
@@ -3,7 +3,6 @@ package org.baeldung.persistence.service;
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.baeldung.config.PersistenceJPAConfigL2Cache;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@@ -18,7 +17,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@DirtiesContext
|
||||
public class FooServicePersistenceIntegrationTest {
|
||||
|
||||
|
||||
+2
-2
@@ -10,7 +10,7 @@ import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfigL2Cache;
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.baeldung.persistence.model.Bar;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.junit.Test;
|
||||
@@ -21,7 +21,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@DirtiesContext
|
||||
@SuppressWarnings("unchecked")
|
||||
public class FooServiceSortingIntegrationTest {
|
||||
|
||||
+1
-2
@@ -10,7 +10,6 @@ import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfig;
|
||||
import org.baeldung.config.PersistenceJPAConfigL2Cache;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -21,7 +20,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@DirtiesContext
|
||||
public class FooServiceSortingWitNullsManualIntegrationTest {
|
||||
|
||||
|
||||
-81
@@ -1,81 +0,0 @@
|
||||
package org.baeldung.persistence.service;
|
||||
|
||||
import net.sf.ehcache.CacheManager;
|
||||
import org.baeldung.config.PersistenceJPAConfigL2Cache;
|
||||
import org.baeldung.persistence.model.Bar;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@DirtiesContext
|
||||
public class SecondLevelCacheIntegrationTest {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
@Autowired
|
||||
private FooService fooService;
|
||||
@Autowired
|
||||
private PlatformTransactionManager platformTransactionManager;
|
||||
|
||||
@Before
|
||||
public final void before() {
|
||||
entityManager.getEntityManagerFactory().getCache().evictAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenEntityIsLoaded_thenItIsCached() {
|
||||
final Foo foo = new Foo(randomAlphabetic(6));
|
||||
fooService.create(foo);
|
||||
fooService.findOne(foo.getId());
|
||||
final int size = CacheManager.ALL_CACHE_MANAGERS.get(0).getCache("org.baeldung.persistence.model.Foo").getSize();
|
||||
assertThat(size, greaterThan(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenBarIsUpdatedInNativeQuery_thenFoosAreNotEvicted() {
|
||||
final Foo foo = new Foo(randomAlphabetic(6));
|
||||
fooService.create(foo);
|
||||
fooService.findOne(foo.getId());
|
||||
|
||||
new TransactionTemplate(platformTransactionManager).execute(status -> {
|
||||
final Bar bar = new Bar(randomAlphabetic(6));
|
||||
entityManager.persist(bar);
|
||||
final Query nativeQuery = entityManager.createNativeQuery("update BAR set NAME = :updatedName where ID = :id");
|
||||
nativeQuery.setParameter("updatedName", "newName");
|
||||
nativeQuery.setParameter("id", bar.getId());
|
||||
nativeQuery.unwrap(org.hibernate.SQLQuery.class).addSynchronizedEntityClass(Bar.class);
|
||||
return nativeQuery.executeUpdate();
|
||||
});
|
||||
|
||||
final int size = CacheManager.ALL_CACHE_MANAGERS.get(0).getCache("org.baeldung.persistence.model.Foo").getSize();
|
||||
assertThat(size, greaterThan(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenCacheableQueryIsExecuted_thenItIsCached() {
|
||||
new TransactionTemplate(platformTransactionManager).execute(status -> {
|
||||
return entityManager.createQuery("select f from Foo f").setHint("org.hibernate.cacheable", true).getResultList();
|
||||
});
|
||||
|
||||
final int size = CacheManager.ALL_CACHE_MANAGERS.get(0).getCache("org.hibernate.cache.internal.StandardQueryCache").getSize();
|
||||
assertThat(size, greaterThan(0));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user