LocalSessionFactoryBuilder provides Hibernate 5.3 BeanContainer support

LocalSessionFactoryBean automatically registers its containing BeanFactory as a BeanContainer when Hibernate 5.3 or higher is on the classpath.

Issue: SPR-16305
This commit is contained in:
Juergen Hoeller
2018-07-04 19:23:19 +02:00
parent 5dc8b5de6d
commit c0d4cb55c7
6 changed files with 256 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 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,6 +19,7 @@ package org.springframework.orm.jpa.domain;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@@ -26,6 +27,7 @@ import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import org.springframework.context.ApplicationContext;
import org.springframework.tests.sample.beans.TestBean;
/**
@@ -34,6 +36,7 @@ import org.springframework.tests.sample.beans.TestBean;
* @author Rod Johnson
*/
@Entity
@EntityListeners(PersonListener.class)
public class Person {
@Id
@@ -52,6 +55,8 @@ public class Person {
@Basic(fetch = FetchType.LAZY)
private String last_name;
public transient ApplicationContext postLoaded;
public Integer getId() {
return id;
@@ -91,8 +96,8 @@ public class Person {
@Override
public String toString() {
return getClass().getName() + ":(" + hashCode() + ") id=" + id + "; firstName=" + first_name + "; lastName="
+ last_name + "; testBean=" + testBean;
return getClass().getName() + ":(" + hashCode() + ") id=" + id + "; firstName=" + first_name +
"; lastName=" + last_name + "; testBean=" + testBean;
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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.domain;
import javax.persistence.PostLoad;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
/**
* @author Juergen Hoeller
*/
public class PersonListener {
@Autowired
ApplicationContext context;
@PostLoad
public void postLoad(Person person) {
person.postLoaded = this.context;
}
}

View File

@@ -23,6 +23,7 @@ import org.hibernate.query.Query;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.jpa.AbstractContainerEntityManagerFactoryIntegrationTests;
import org.springframework.orm.jpa.EntityManagerFactoryInfo;
import org.springframework.orm.jpa.domain.Person;
@@ -40,6 +41,9 @@ public class HibernateNativeEntityManagerFactoryIntegrationTests extends Abstrac
@Autowired
private SessionFactory sessionFactory;
@Autowired
private ApplicationContext applicationContext;
@Override
protected String[] getConfigLocations() {
@@ -53,18 +57,29 @@ public class HibernateNativeEntityManagerFactoryIntegrationTests extends Abstrac
assertFalse("Must not have introduced config interface", entityManagerFactory instanceof EntityManagerFactoryInfo);
}
@Test
@SuppressWarnings("unchecked")
public void testEntityListener() {
String firstName = "Tony";
insertPerson(firstName);
List<Person> people = sharedEntityManager.createQuery("select p from Person as p").getResultList();
assertEquals(1, people.size());
assertEquals(firstName, people.get(0).getFirstName());
assertSame(applicationContext, people.get(0).postLoaded);
}
@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());
assertSame(applicationContext, people.get(0).postLoaded);
}
}