1 16 17 package org.springframework.orm.jpa; 18 19 import javax.persistence.EntityManager; 20 import javax.persistence.EntityManagerFactory; 21 import javax.persistence.PersistenceException; 22 23 import org.springframework.beans.factory.InitializingBean; 24 import org.springframework.dao.support.DataAccessUtils; 25 26 43 public abstract class JpaAccessor extends EntityManagerFactoryAccessor implements InitializingBean { 44 45 private EntityManager entityManager; 46 47 private JpaDialect jpaDialect = new DefaultJpaDialect(); 48 49 private boolean flushEager = false; 50 51 52 55 public void setEntityManager(EntityManager entityManager) { 56 this.entityManager = entityManager; 57 } 58 59 62 public EntityManager getEntityManager() { 63 return entityManager; 64 } 65 66 71 public void setJpaDialect(JpaDialect jpaDialect) { 72 this.jpaDialect = (jpaDialect != null ? jpaDialect : new DefaultJpaDialect()); 73 } 74 75 79 public JpaDialect getJpaDialect() { 80 return this.jpaDialect; 81 } 82 83 97 public void setFlushEager(boolean flushEager) { 98 this.flushEager = flushEager; 99 } 100 101 104 public boolean isFlushEager() { 105 return this.flushEager; 106 } 107 108 112 public void afterPropertiesSet() { 113 EntityManagerFactory emf = getEntityManagerFactory(); 114 if (emf == null && getEntityManager() == null) { 115 throw new IllegalArgumentException ("entityManagerFactory or entityManager is required"); 116 } 117 if (emf instanceof EntityManagerFactoryInfo) { 118 JpaDialect jpaDialect = ((EntityManagerFactoryInfo) emf).getJpaDialect(); 119 if (jpaDialect != null) { 120 setJpaDialect(jpaDialect); 121 } 122 } 123 } 124 125 126 132 protected void flushIfNecessary(EntityManager em, boolean existingTransaction) throws PersistenceException { 133 if (isFlushEager()) { 134 logger.debug("Eagerly flushing JPA entity manager"); 135 em.flush(); 136 } 137 } 138 139 151 public RuntimeException translateIfNecessary(RuntimeException ex) { 152 return DataAccessUtils.translateIfNecessary(ex, getJpaDialect()); 153 } 154 155 } 156 | Popular Tags |