1 16 17 package org.springframework.orm.jpa; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 23 import javax.persistence.EntityManager; 24 import javax.persistence.EntityManagerFactory; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.springframework.util.Assert; 30 import org.springframework.util.CollectionUtils; 31 32 43 public abstract class EntityManagerFactoryAccessor { 44 45 46 protected final Log logger = LogFactory.getLog(getClass()); 47 48 private EntityManagerFactory entityManagerFactory; 49 50 private final Map jpaPropertyMap = new HashMap (); 51 52 53 59 public void setEntityManagerFactory(EntityManagerFactory emf) { 60 this.entityManagerFactory = emf; 61 } 62 63 67 public EntityManagerFactory getEntityManagerFactory() { 68 return entityManagerFactory; 69 } 70 71 78 public void setJpaProperties(Properties jpaProperties) { 79 CollectionUtils.mergePropertiesIntoMap(jpaProperties, this.jpaPropertyMap); 80 } 81 82 88 public void setJpaPropertyMap(Map jpaProperties) { 89 if (jpaProperties != null) { 90 this.jpaPropertyMap.putAll(jpaProperties); 91 } 92 } 93 94 99 public Map getJpaPropertyMap() { 100 return jpaPropertyMap; 101 } 102 103 104 112 protected EntityManager createEntityManager() throws IllegalStateException { 113 EntityManagerFactory emf = getEntityManagerFactory(); 114 Assert.state(emf != null, "No EntityManagerFactory specified"); 115 Map properties = getJpaPropertyMap(); 116 return (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager()); 117 } 118 119 126 protected EntityManager getTransactionalEntityManager() throws IllegalStateException { 127 EntityManagerFactory emf = getEntityManagerFactory(); 128 Assert.state(emf != null, "No EntityManagerFactory specified"); 129 return EntityManagerFactoryUtils.getTransactionalEntityManager(emf, getJpaPropertyMap()); 130 } 131 132 } 133 | Popular Tags |