Skip to content

Commit 620e83c

Browse files
committed
SpringBeanContainer is public for custom JPA configuration purposes
Issue: SPR-16305
1 parent 39d5874 commit 620e83c

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

spring-orm/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBean.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,21 @@
5757
* SessionFactory can then be passed to data access objects via dependency injection.
5858
*
5959
* <p>Compatible with Hibernate 5.0/5.1 as well as 5.2/5.3, as of Spring 5.1.
60+
* Set up with Hibernate 5.3, {@code LocalSessionFactoryBean} is an immediate alternative
61+
* to {@link org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean} for common
62+
* JPA purposes: In particular with Hibernate 5.3, the Hibernate {@code SessionFactory}
63+
* will natively expose the JPA {@code EntityManagerFactory} interface as well, and
64+
* Hibernate {@code BeanContainer} integration will be registered out of the box.
65+
* In combination with {@link HibernateTransactionManager}, this naturally allows for
66+
* mixing JPA access code with native Hibernate access code within the same transaction.
6067
*
6168
* @author Juergen Hoeller
6269
* @since 4.2
6370
* @see #setDataSource
6471
* @see #setPackagesToScan
72+
* @see HibernateTransactionManager
6573
* @see LocalSessionFactoryBuilder
74+
* @see org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
6675
*/
6776
public class LocalSessionFactoryBean extends HibernateExceptionTranslator
6877
implements FactoryBean<SessionFactory>, ResourceLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean {
@@ -446,6 +455,7 @@ public ResourceLoader getResourceLoader() {
446455
* it if possible. This requires a Spring {@link ConfigurableListableBeanFactory}
447456
* and Hibernate 5.3 or higher on the classpath.
448457
* @since 5.1
458+
* @see SpringBeanContainer
449459
* @see LocalSessionFactoryBuilder#setBeanContainer
450460
*/
451461
@Override

spring-orm/src/main/java/org/springframework/orm/hibernate5/LocalSessionFactoryBuilder.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,30 @@
6767
/**
6868
* A Spring-provided extension of the standard Hibernate {@link Configuration} class,
6969
* adding {@link SpringSessionContext} as a default and providing convenient ways
70-
* to specify a DataSource and an application class loader.
70+
* to specify a JDBC {@link DataSource} and an application class loader.
7171
*
72-
* <p>This is designed for programmatic use, e.g. in {@code @Bean} factory methods.
73-
* Consider using {@link LocalSessionFactoryBean} for XML bean definition files.
72+
* <p>This is designed for programmatic use, e.g. in {@code @Bean} factory methods;
73+
* consider using {@link LocalSessionFactoryBean} for XML bean definition files.
74+
* Typically combined with {@link HibernateTransactionManager} for declarative
75+
* transactions against the {@code SessionFactory} and its JDBC {@code DataSource}.
7476
*
7577
* <p>Compatible with Hibernate 5.0/5.1 as well as 5.2/5.3, as of Spring 5.1.
78+
* Set up with Hibernate 5.2/5.3, this builder is also a convenient way to set up
79+
* a JPA {@code EntityManagerFactory} since the Hibernate {@code SessionFactory}
80+
* natively exposes the JPA {@code EntityManagerFactory} interface as well now.
81+
*
82+
* <p>This builder supports Hibernate 5.3 {@code BeanContainer} integration,
83+
* {@link MetadataSources} from custom {@link BootstrapServiceRegistryBuilder}
84+
* setup, as well as other advanced Hibernate configuration options beyond the
85+
* standard JPA bootstrap contract.
7686
*
7787
* @author Juergen Hoeller
7888
* @since 4.2
89+
* @see HibernateTransactionManager
7990
* @see LocalSessionFactoryBean
91+
* @see #setBeanContainer
92+
* @see #LocalSessionFactoryBuilder(DataSource, ResourceLoader, MetadataSources)
93+
* @see BootstrapServiceRegistryBuilder
8094
*/
8195
@SuppressWarnings("serial")
8296
public class LocalSessionFactoryBuilder extends Configuration {
@@ -235,6 +249,7 @@ else if (jtaTransactionManager instanceof TransactionManager) {
235249
* <p>Note: Bean container integration requires Hibernate 5.3 or higher.
236250
* It enables autowiring of Hibernate attribute converters and entity listeners.
237251
* @since 5.1
252+
* @see SpringBeanContainer
238253
* @see AvailableSettings#BEAN_CONTAINER
239254
*/
240255
public LocalSessionFactoryBuilder setBeanContainer(ConfigurableListableBeanFactory beanFactory) {

spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringBeanContainer.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,63 @@
2626
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
2727
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2828
import org.springframework.lang.Nullable;
29+
import org.springframework.util.Assert;
2930
import org.springframework.util.ConcurrentReferenceHashMap;
3031

3132
/**
3233
* Spring's implementation of Hibernate 5.3's {@link BeanContainer} SPI,
3334
* delegating to a Spring {@link ConfigurableListableBeanFactory}.
3435
*
36+
* <p>Auto-configured by {@link LocalSessionFactoryBean#setBeanFactory},
37+
* programmatically supported via {@link LocalSessionFactoryBuilder#setBeanContainer},
38+
* and manually configurable through a "hibernate.resource.beans.container" entry
39+
* in JPA properties, e.g.:
40+
*
41+
* <pre class="code">
42+
* &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
43+
* ...
44+
* &lt;property name="jpaPropertyMap"&gt;
45+
* &lt;map>
46+
* &lt;entry key="hibernate.resource.beans.container"&gt;
47+
* &lt;bean class="org.springframework.orm.hibernate5.SpringBeanContainer"/&gt;
48+
* &lt;/entry&gt;
49+
* &lt;/map&gt;
50+
* &lt;/property&gt;
51+
* &lt;/bean&gt;</pre>
52+
*
53+
* Or in Java-based JPA configuration:
54+
*
55+
* <pre class="code">
56+
* LocalContainerEntityManagerFactoryBean emfb = ...
57+
* emfb.getJpaPropertyMap().put(AvailableSettings.BEAN_CONTAINER, new SpringBeanContainer(beanFactory));
58+
* </pre>
59+
*
60+
* Please note that Spring's {@link LocalSessionFactoryBean} is an immediate alternative
61+
* to {@link org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean} for common
62+
* JPA purposes: In particular with Hibernate 5.3, the Hibernate {@code SessionFactory}
63+
* will natively expose the JPA {@code EntityManagerFactory} interface as well, and
64+
* Hibernate {@code BeanContainer} integration will be registered out of the box.
65+
*
3566
* @author Juergen Hoeller
3667
* @since 5.1
68+
* @see LocalSessionFactoryBean#setBeanFactory
69+
* @see LocalSessionFactoryBuilder#setBeanContainer
70+
* @see org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean#setJpaPropertyMap
71+
* @see org.hibernate.cfg.AvailableSettings#BEAN_CONTAINER
3772
*/
38-
final class SpringBeanContainer implements BeanContainer {
73+
public final class SpringBeanContainer implements BeanContainer {
3974

4075
private final ConfigurableListableBeanFactory beanFactory;
4176

4277
private final Map<Object, SpringContainedBean<?>> beanCache = new ConcurrentReferenceHashMap<>();
4378

4479

80+
/**
81+
* Instantiate a new SpringBeanContainer for the given bean factory.
82+
* @param beanFactory the Spring bean factory to delegate to
83+
*/
4584
public SpringBeanContainer(ConfigurableListableBeanFactory beanFactory) {
85+
Assert.notNull(beanFactory, "ConfigurableListableBeanFactory is required");
4686
this.beanFactory = beanFactory;
4787
}
4888

0 commit comments

Comments
 (0)