Browse Source
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-17002pull/1870/head
16 changed files with 226 additions and 70 deletions
@ -0,0 +1,70 @@
@@ -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()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,31 @@
@@ -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> |
||||
Loading…
Reference in new issue