001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.orm.jpa.support; 018 019import javax.persistence.EntityManager; 020import javax.persistence.EntityManagerFactory; 021 022import org.springframework.beans.factory.FactoryBean; 023import org.springframework.beans.factory.InitializingBean; 024import org.springframework.lang.Nullable; 025import org.springframework.orm.jpa.EntityManagerFactoryAccessor; 026import org.springframework.orm.jpa.EntityManagerFactoryInfo; 027import org.springframework.orm.jpa.SharedEntityManagerCreator; 028import org.springframework.util.Assert; 029 030/** 031 * {@link FactoryBean} that exposes a shared JPA {@link javax.persistence.EntityManager} 032 * reference for a given EntityManagerFactory. Typically used for an EntityManagerFactory 033 * created by {@link org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean}, 034 * as direct alternative to a JNDI lookup for a Java EE server's EntityManager reference. 035 * 036 * <p>The shared EntityManager will behave just like an EntityManager fetched from an 037 * application server's JNDI environment, as defined by the JPA specification. 038 * It will delegate all calls to the current transactional EntityManager, if any; 039 * otherwise, it will fall back to a newly created EntityManager per operation. 040 * 041 * <p>Can be passed to DAOs that expect a shared EntityManager reference rather than an 042 * EntityManagerFactory. Note that Spring's {@link org.springframework.orm.jpa.JpaTransactionManager} 043 * always needs an EntityManagerFactory in order to create new transactional EntityManager instances. 044 * 045 * @author Juergen Hoeller 046 * @since 2.0 047 * @see #setEntityManagerFactory 048 * @see #setEntityManagerInterface 049 * @see org.springframework.orm.jpa.LocalEntityManagerFactoryBean 050 * @see org.springframework.orm.jpa.JpaTransactionManager 051 */ 052public class SharedEntityManagerBean extends EntityManagerFactoryAccessor 053 implements FactoryBean<EntityManager>, InitializingBean { 054 055 @Nullable 056 private Class<? extends EntityManager> entityManagerInterface; 057 058 private boolean synchronizedWithTransaction = true; 059 060 @Nullable 061 private EntityManager shared; 062 063 064 /** 065 * Specify the EntityManager interface to expose. 066 * <p>Default is the EntityManager interface as defined by the 067 * EntityManagerFactoryInfo, if available. Else, the standard 068 * {@code javax.persistence.EntityManager} interface will be used. 069 * @see org.springframework.orm.jpa.EntityManagerFactoryInfo#getEntityManagerInterface() 070 * @see javax.persistence.EntityManager 071 */ 072 public void setEntityManagerInterface(Class<? extends EntityManager> entityManagerInterface) { 073 Assert.notNull(entityManagerInterface, "'entityManagerInterface' must not be null"); 074 this.entityManagerInterface = entityManagerInterface; 075 } 076 077 /** 078 * Set whether to automatically join ongoing transactions (according 079 * to the JPA 2.1 SynchronizationType rules). Default is "true". 080 */ 081 public void setSynchronizedWithTransaction(boolean synchronizedWithTransaction) { 082 this.synchronizedWithTransaction = synchronizedWithTransaction; 083 } 084 085 086 @Override 087 public final void afterPropertiesSet() { 088 EntityManagerFactory emf = getEntityManagerFactory(); 089 if (emf == null) { 090 throw new IllegalArgumentException("'entityManagerFactory' or 'persistenceUnitName' is required"); 091 } 092 if (emf instanceof EntityManagerFactoryInfo) { 093 EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf; 094 if (this.entityManagerInterface == null) { 095 this.entityManagerInterface = emfInfo.getEntityManagerInterface(); 096 if (this.entityManagerInterface == null) { 097 this.entityManagerInterface = EntityManager.class; 098 } 099 } 100 } 101 else { 102 if (this.entityManagerInterface == null) { 103 this.entityManagerInterface = EntityManager.class; 104 } 105 } 106 this.shared = SharedEntityManagerCreator.createSharedEntityManager( 107 emf, getJpaPropertyMap(), this.synchronizedWithTransaction, this.entityManagerInterface); 108 } 109 110 111 @Override 112 @Nullable 113 public EntityManager getObject() { 114 return this.shared; 115 } 116 117 @Override 118 public Class<? extends EntityManager> getObjectType() { 119 return (this.entityManagerInterface != null ? this.entityManagerInterface : EntityManager.class); 120 } 121 122 @Override 123 public boolean isSingleton() { 124 return true; 125 } 126 127}