1 16 17 package org.springframework.orm.jpa.support; 18 19 import javax.persistence.EntityManager; 20 import javax.persistence.EntityManagerFactory; 21 22 import org.springframework.beans.factory.FactoryBean; 23 import org.springframework.beans.factory.InitializingBean; 24 import org.springframework.orm.jpa.EntityManagerFactoryAccessor; 25 import org.springframework.orm.jpa.EntityManagerFactoryInfo; 26 import org.springframework.orm.jpa.EntityManagerPlus; 27 import org.springframework.orm.jpa.JpaDialect; 28 import org.springframework.orm.jpa.SharedEntityManagerCreator; 29 import org.springframework.util.Assert; 30 31 55 public class SharedEntityManagerBean extends EntityManagerFactoryAccessor implements FactoryBean, InitializingBean { 56 57 private Class entityManagerInterface; 58 59 private EntityManager shared; 60 61 62 70 public void setEntityManagerInterface(Class entityManagerInterface) { 71 Assert.notNull(entityManagerInterface, "entityManagerInterface must not be null"); 72 Assert.isAssignable(EntityManager.class, entityManagerInterface); 73 this.entityManagerInterface = entityManagerInterface; 74 } 75 76 77 public final void afterPropertiesSet() { 78 EntityManagerFactory emf = getEntityManagerFactory(); 79 if (emf == null) { 80 throw new IllegalArgumentException ("entityManagerFactory is required"); 81 } 82 Class [] ifcs = null; 83 if (emf instanceof EntityManagerFactoryInfo) { 84 EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf; 85 if (this.entityManagerInterface == null) { 86 this.entityManagerInterface = emfInfo.getEntityManagerInterface(); 87 } 88 JpaDialect jpaDialect = emfInfo.getJpaDialect(); 89 if (jpaDialect != null && jpaDialect.supportsEntityManagerPlusOperations()) { 90 ifcs = new Class [] {this.entityManagerInterface, EntityManagerPlus.class}; 91 } 92 else { 93 ifcs = new Class [] {this.entityManagerInterface}; 94 } 95 } 96 else { 97 if (this.entityManagerInterface == null) { 98 this.entityManagerInterface = EntityManager.class; 99 } 100 ifcs = new Class [] {this.entityManagerInterface}; 101 } 102 this.shared = SharedEntityManagerCreator.createSharedEntityManager(emf, getJpaPropertyMap(), ifcs); 103 } 104 105 106 public EntityManager getObject() { 107 return this.shared; 108 } 109 110 public Class getObjectType() { 111 return this.entityManagerInterface; 112 } 113 114 public boolean isSingleton() { 115 return true; 116 } 117 118 } 119 | Popular Tags |