LocalSessionFactoryBean and HibernateTransactionManager for JPA setup
SessionHolder extends EntityManagerHolder now, allowing for @PersistenceContext and co to interact with HibernateTransactionManager's thread-bound transactions, and SpringSessionContext is capable of interacting with JpaTransactionManager by detecting a plain EntityManagerHolder as well. Issue: SPR-17002
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,7 +19,6 @@ package org.springframework.orm.jpa;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.List;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.FlushModeType;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
@@ -39,14 +38,14 @@ import static org.junit.Assert.*;
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public abstract class AbstractContainerEntityManagerFactoryIntegrationTests extends AbstractEntityManagerFactoryIntegrationTests {
|
||||
public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
|
||||
extends AbstractEntityManagerFactoryIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void testEntityManagerFactoryImplementsEntityManagerFactoryInfo() {
|
||||
assertTrue(Proxy.isProxyClass(entityManagerFactory.getClass()));
|
||||
assertTrue("Must have introduced config interface", entityManagerFactory instanceof EntityManagerFactoryInfo);
|
||||
EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory;
|
||||
// assertEquals("Person", emfi.getPersistenceUnitName());
|
||||
assertEquals("Person", emfi.getPersistenceUnitName());
|
||||
assertNotNull("PersistenceUnitInfo must be available", emfi.getPersistenceUnitInfo());
|
||||
assertNotNull("Raw EntityManagerFactory must be available", emfi.getNativeEntityManagerFactory());
|
||||
}
|
||||
@@ -78,7 +77,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests exte
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({ "unused", "unchecked" })
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testEntityManagerProxyIsProxy() {
|
||||
assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass()));
|
||||
Query q = sharedEntityManager.createQuery("select p from Person as p");
|
||||
@@ -107,14 +106,13 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests exte
|
||||
try {
|
||||
Person notThere = sharedEntityManager.getReference(Person.class, 666);
|
||||
|
||||
// We may get here (as with Hibernate).
|
||||
// Either behaviour is valid: throw exception on first access
|
||||
// or on getReference itself.
|
||||
// We may get here (as with Hibernate). Either behaviour is valid:
|
||||
// throw exception on first access or on getReference itself.
|
||||
notThere.getFirstName();
|
||||
fail("Should have thrown an EntityNotFoundException");
|
||||
fail("Should have thrown an EntityNotFoundException or ObjectNotFoundException");
|
||||
}
|
||||
catch (EntityNotFoundException ex) {
|
||||
// expected
|
||||
catch (Exception ex) {
|
||||
assertTrue(ex.getClass().getName().endsWith("NotFoundException"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,6 +207,8 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests exte
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testQueryNoPersonsNotTransactional() {
|
||||
endTransaction();
|
||||
|
||||
EntityManager em = entityManagerFactory.createEntityManager();
|
||||
Query q = em.createQuery("select p from Person as p");
|
||||
List<Person> people = q.getResultList();
|
||||
@@ -223,12 +223,12 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests exte
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({ "unused", "unchecked" })
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testQueryNoPersonsShared() {
|
||||
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory);
|
||||
Query q = em.createQuery("select p from Person as p");
|
||||
Query q = this.sharedEntityManager.createQuery("select p from Person as p");
|
||||
q.setFlushMode(FlushModeType.AUTO);
|
||||
List<Person> people = q.getResultList();
|
||||
assertEquals(0, people.size());
|
||||
try {
|
||||
assertNull(q.getSingleResult());
|
||||
fail("Should have thrown NoResultException");
|
||||
@@ -243,7 +243,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests exte
|
||||
public void testQueryNoPersonsSharedNotTransactional() {
|
||||
endTransaction();
|
||||
|
||||
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory);
|
||||
EntityManager em = this.sharedEntityManager;
|
||||
Query q = em.createQuery("select p from Person as p");
|
||||
q.setFlushMode(FlushModeType.AUTO);
|
||||
List<Person> people = q.getResultList();
|
||||
|
||||
@@ -48,12 +48,8 @@ import static org.junit.Assert.*;
|
||||
public abstract class AbstractEntityManagerFactoryIntegrationTests {
|
||||
|
||||
protected static final String[] ECLIPSELINK_CONFIG_LOCATIONS = new String[] {
|
||||
"/org/springframework/orm/jpa/eclipselink/eclipselink-manager.xml", "/org/springframework/orm/jpa/memdb.xml",
|
||||
"/org/springframework/orm/jpa/inject.xml"};
|
||||
|
||||
protected static final String[] HIBERNATE_CONFIG_LOCATIONS = new String[] {
|
||||
"/org/springframework/orm/jpa/hibernate/hibernate-manager.xml", "/org/springframework/orm/jpa/memdb.xml",
|
||||
"/org/springframework/orm/jpa/inject.xml"};
|
||||
"/org/springframework/orm/jpa/eclipselink/eclipselink-manager.xml",
|
||||
"/org/springframework/orm/jpa/memdb.xml", "/org/springframework/orm/jpa/inject.xml"};
|
||||
|
||||
|
||||
private static ConfigurableApplicationContext applicationContext;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -31,12 +31,6 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
public class EclipseLinkEntityManagerFactoryIntegrationTests extends AbstractContainerEntityManagerFactoryIntegrationTests {
|
||||
|
||||
@Override
|
||||
protected String[] getConfigLocations() {
|
||||
return ECLIPSELINK_CONFIG_LOCATIONS;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCanCastNativeEntityManagerFactoryToEclipseLinkEntityManagerFactoryImpl() {
|
||||
EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -41,7 +41,8 @@ public class HibernateEntityManagerFactoryIntegrationTests extends AbstractConta
|
||||
|
||||
@Override
|
||||
protected String[] getConfigLocations() {
|
||||
return HIBERNATE_CONFIG_LOCATIONS;
|
||||
return new String[] {"/org/springframework/orm/jpa/hibernate/hibernate-manager.xml",
|
||||
"/org/springframework/orm/jpa/memdb.xml", "/org/springframework/orm/jpa/inject.xml"};
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,6 +23,7 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.orm.jpa.AbstractContainerEntityManagerFactoryIntegrationTests;
|
||||
import org.springframework.orm.jpa.EntityManagerFactoryInfo;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -44,6 +45,15 @@ public class HibernateMultiEntityManagerFactoryIntegrationTests extends Abstract
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEntityManagerFactoryImplementsEntityManagerFactoryInfo() {
|
||||
assertTrue("Must have introduced config interface", this.entityManagerFactory instanceof EntityManagerFactoryInfo);
|
||||
EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) this.entityManagerFactory;
|
||||
assertEquals("Drivers", emfi.getPersistenceUnitName());
|
||||
assertNotNull("PersistenceUnitInfo must be available", emfi.getPersistenceUnitInfo());
|
||||
assertNotNull("Raw EntityManagerFactory must be available", emfi.getNativeEntityManagerFactory());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEntityManagerFactory2() {
|
||||
EntityManager em = this.entityManagerFactory2.createEntityManager();
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.orm.jpa.hibernate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.query.Query;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.orm.jpa.AbstractContainerEntityManagerFactoryIntegrationTests;
|
||||
import org.springframework.orm.jpa.EntityManagerFactoryInfo;
|
||||
import org.springframework.orm.jpa.domain.Person;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Hibernate-specific JPA tests with native SessionFactory setup and getCurrentSession interaction.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 5.1
|
||||
*/
|
||||
public class HibernateNativeEntityManagerFactoryIntegrationTests extends AbstractContainerEntityManagerFactoryIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
|
||||
@Override
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] {"/org/springframework/orm/jpa/hibernate/hibernate-manager-native.xml",
|
||||
"/org/springframework/orm/jpa/memdb.xml", "/org/springframework/orm/jpa/inject.xml"};
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEntityManagerFactoryImplementsEntityManagerFactoryInfo() {
|
||||
assertFalse("Must not have introduced config interface", entityManagerFactory instanceof EntityManagerFactoryInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testCurrentSession() {
|
||||
// Add with JDBC
|
||||
String firstName = "Tony";
|
||||
insertPerson(firstName);
|
||||
|
||||
Query q = sessionFactory.getCurrentSession().createQuery("select p from Person as p");
|
||||
List<Person> people = q.getResultList();
|
||||
|
||||
assertEquals(1, people.size());
|
||||
assertEquals(firstName, people.get(0).getFirstName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,4 +24,8 @@
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
||||
<property name="entityManagerFactory" ref="entityManagerFactory"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -22,5 +22,9 @@
|
||||
</props>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
||||
<property name="entityManagerFactory" ref="entityManagerFactory"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="entityManagerFactory" name="Person" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" primary="true">
|
||||
<property name="annotatedClasses">
|
||||
<list>
|
||||
<value>org.springframework.orm.jpa.domain.DriversLicense</value>
|
||||
<value>org.springframework.orm.jpa.domain.Person</value>
|
||||
</list>
|
||||
</property>
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="hibernateProperties">
|
||||
<props>
|
||||
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
|
||||
<prop key="hibernate.hbm2ddl.auto">update</prop>
|
||||
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
|
||||
</props>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
|
||||
<property name="sessionFactory" ref="entityManagerFactory"/>
|
||||
</bean>
|
||||
|
||||
<bean id="hibernateStatistics" factory-bean="entityManagerFactory" factory-method="getStatistics"/>
|
||||
|
||||
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
|
||||
|
||||
</beans>
|
||||
@@ -15,6 +15,7 @@
|
||||
</property>
|
||||
<property name="jpaPropertyMap">
|
||||
<props>
|
||||
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
|
||||
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
|
||||
</props>
|
||||
</property>
|
||||
@@ -23,6 +24,10 @@
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
||||
<property name="entityManagerFactory" ref="entityManagerFactory"/>
|
||||
</bean>
|
||||
|
||||
<bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory"/>
|
||||
|
||||
<bean id="hibernateStatistics" factory-bean="sessionFactory" factory-method="getStatistics"/>
|
||||
|
||||
@@ -7,10 +7,6 @@
|
||||
<property name="entityManagerFactory" ref="entityManagerFactory"/>
|
||||
</bean>
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
||||
<property name="entityManagerFactory" ref="entityManagerFactory"/>
|
||||
</bean>
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
||||
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
|
||||
<property name="url" value="jdbc:hsqldb:mem:xdb"/>
|
||||
@@ -18,14 +14,4 @@
|
||||
<property name="password" value=""/>
|
||||
</bean>
|
||||
|
||||
<!-- Datasource for using an existing database. make sure you turn off generateDLL otherwise
|
||||
on multiple runs it will break it.
|
||||
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
|
||||
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
|
||||
<property name="url" value="jdbc:hsqldb:file:target/classes/db/test"/>
|
||||
<property name="username" value="sa"/>
|
||||
<property name="password" value=""/>
|
||||
</bean>
|
||||
-->
|
||||
|
||||
</beans>
|
||||
|
||||
Reference in New Issue
Block a user