虽然findAll可以正常工作,但我无法在JPA中保留实体 这里。 这是JpaDAO
package aop.web.teacher.dao;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.apache.log4j.Logger;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
public abstract class JpaDAO extends JpaDaoSupport {
protected Class entityClass;
private static Logger log = Logger.getLogger(JpaDAO.class);
@SuppressWarnings("unchecked")
public JpaDAO() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass()
.getGenericSuperclass();
this.entityClass = (Class) genericSuperclass
.getActualTypeArguments()[1];
}
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void persist(E entity) {
getJpaTemplate().persist(entity);
}
@Transactional
public void remove(E entity) {
getJpaTemplate().remove(entity);
}
@Transactional
public E merge(E entity) {
return getJpaTemplate().merge(entity);
}
@Transactional
public void refresh(E entity) {
getJpaTemplate().refresh(entity);
}
@Transactional
public E findById(K id) {
return getJpaTemplate().find(entityClass, id);
}
@Transactional
public E flush(E entity) {
getJpaTemplate().flush();
return entity;
}
@SuppressWarnings("unchecked")
@Transactional
public List findAll() {
Object res = getJpaTemplate().execute(new JpaCallback() {
public Object doInJpa(EntityManager em) throws PersistenceException {
Query q = em.createQuery("SELECT h FROM "
+ entityClass.getName() + " h");
return q.getResultList();
}
});
return (List) res;
}
@SuppressWarnings("unchecked")
@Transactional
public Integer removeAll() {
return (Integer) getJpaTemplate().execute(new JpaCallback() {
public Object doInJpa(EntityManager em) throws PersistenceException {
Query q = em.createQuery("DELETE FROM " + entityClass.getName()
+ " h");
return q.executeUpdate();
}
});
}
}
这是TestDao类
package aop.web.teacher.dao;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import aop.web.teacher.rmodels.Teachermaster;
@Service
@Repository
public class TestDaoImpl extends JpaDAO implements TestDao {
@Autowired
EntityManagerFactory entityManagerFactory;
@PersistenceContext
private EntityManager em;
@PostConstruct
public void init() {
super.setEntityManagerFactory(entityManagerFactory);
}
public int saveTeacher() {
List teacherList = findAll();
Teachermaster m1 = teacherList.get(0);
logger.info("Found " + m1.getId() + " and " + m1.getRace());
m1.setRace(m1.getRace() + "::" + System.currentTimeMillis());
logger.info("New " + m1.getId() + " and " + m1.getRace());
persist(m1);
return 0;
}
}
这是spring context xml
这里的findAll有效 但是当我们改变教师的属性时 然后坚持或合并似乎并没有拯救实体...... 如果我们冲洗它,我们会得到异常
javax.persistence.TransactionRequiredException: no transaction is in progress
请告知
答案 0 :(得分:5)
Spring使用基于代理的AOP,因此当从同一个类调用方法时,不应用方面(包括事务方面)。
一般来说
@Transactional
注释通常应放在服务方法而不是DAO方法saveTeacher()
看起来像服务方法,最好将其放在单独的服务类中并注释为@Transactional
persist()
中不需要saveTeacher()
- 对持久对象所做的更改应自动保存TestDao
) - 请参阅下面的链接另见:
答案 1 :(得分:2)
从测试类调用persist()
时,您正在调用本地方法。这样就不会调用会创建事务的代理,因此您对persist()
的调用没有事务。
正确执行此操作的方法是让测试类不扩展测试对象但是将其注入。这样,将触发代理并创建事务。
顺便说一句,我必须补充一点,我发现你的班级设计有点特别。我可以建议创建如下结构吗?
DAO界面:
public interface FooDao {
void persist(Foo foo);
// ...
}
DAO实施:
public class FooDaoImpl implements FooDao {
@PersistenceContext
private EntityManager entityManager;
@Transactional
public void persist(Foo foo) {
entityManager.persist(foo);
}
}
测试类:
@RunWith(SpringJunit4ClassRunner.class)
@ContextConfiguration(...)
public class FooDaoTest {
@Autowired
private FooDao fooDao;
@Test
public void testPersist() {
// do some testing
}
}
如果您愿意,您可以将DAO实现中的大部分逻辑提取到通用超类中。