云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > jpatemplate

jpatemplate

  • 62 次阅读
  • 3 次下载
  • 2025/7/8 17:01:34

7. return (Collection) this.jpaTemplate.execute(new JpaCallback() {

8. public Object doInJpa(EntityManager em) throws PersistenceException {

9. Query query = em.createQuery(\y = :category\

10.query.setParameter(\11.List result = query.getResultList();

12.// do some further processing with the result list 13.return result; 14.} 15.}); 16.} 17.}

nick.s.ni 写道

spring 3已經不推薦使用JpaTemplate。

那看下spring 2.5.5中使用JpaTemplate的方式 第一種,使用JpaTemplate Java代码

1.

2. 5.

Java代码

1. public class JpaProductDao implements ProductDao { 2. private JpaTemplate jpaTemplate;

3. public void setEntityManagerFactory(EntityManagerFactory emf) {

4. this.jpaTemplate = new JpaTemplate(emf); 5. }

6. public Collection loadProductsByCategory(final String category) throws DataAccessException {

7. return (Collection) this.jpaTemplate.execute(new JpaCallback() {

8. public Object doInJpa(EntityManager em) throws PersistenceException {

9. Query query = em.createQuery(\y = :category\

10.query.setParameter(\11.List result = query.getResultList();

12.// do some further processing with the result list 13.return result; 14.} 15.}); 16.} 17.}

第二種

繼承JpaDaoSupport Java代码

1. public class ProductDaoImpl extends JpaDaoSupport implements ProductDao {

2. public Collection loadProductsByCategory(String category) throws DataAccessException {

3. Map params = new HashMap(); 4. params.put(\

5. return getJpaTemplate().findByNamedParams(\ere p.category = :category\6. } 7. }

JpaDaoSupport中需要注入EMF,而EM是內部創建的 Java代码

1. protected EntityManager createEntityManager() throws IllegalStateException {

2. EntityManagerFactory emf = getEntityManagerFactory(); 3. Assert.state(emf != null, \ified\

4. Map properties = getJpaPropertyMap(); 5. return (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager()); 6. }

這不是代碼問題是使用問題,每個DAO都有一個EM,EM中有一定的數據緩存,根據EM的實現,對數據庫中的新增數據會更新,對修改后的數據在查詢時不會更新。

例如一個DAO進行連表查詢,另一個DAO中進行子表的CRUD,修改的信息在查詢中不會更新的。

第二種

繼承JpaDaoSupport Java代码

1. public class ProductDaoImpl extends JpaDaoSupport implements ProductDao {

2. public Collection loadProductsByCategory(String category) throws DataAccessException {

3. Map params = new HashMap(); 4. params.put(\

5. return getJpaTemplate().findByNamedParams(\ere p.category = :category\6. } 7. }

JpaDaoSupport中需要注入EMF,而EM是內部創建的 Java代码

1. protected EntityManager createEntityManager() throws IllegalStateException {

2. EntityManagerFactory emf = getEntityManagerFactory(); 3. Assert.state(emf != null, \ified\

4. Map properties = getJpaPropertyMap(); 5. return (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager()); 6. }

這不是代碼問題是使用問題,每個DAO都有一個EM,EM中有一定的數據緩存,根據EM的實現,對數據庫中的新增數據會更新,對修改后的數據在查詢時不會更新。

例如一個DAO進行連表查詢,另一個DAO中進行子表的CRUD,修改的信息在查

詢中不會更新的。

這不是代碼問題是使用問題,每個DAO都有一個EM(这个如果是spring管理事务的,不是每个DAO一个,而是一个事务一个),EM中有一定的數據緩存(一级缓存),根據EM的實現,對數據庫中的新增數據會更新,對修改后的數據在查詢時不會更新(你这个查询是 修改之前?还是修改之后?(修改会自动清除二级缓存的,再次查询返回最新的数据))。

能否把您的用例发过来研究下 什么问题 admin@sishuok.com 1 楼 nick.s.ni 2012-03-23

JPA的實現我用的是EclipseLink(TopLink)實現,不會跟Hibernate混淆。你這篇應該直接提供 Java代码

1. public class ProductDaoImpl implements ProductDao { 2. @PersistenceContext

3. private EntityManager em;

4. public Collection loadProductsByCategory(String category) { 5. Query query = em.createQuery(\y = :category\

6. query.setParameter(\7. return query.getResultList(); 8. } 9. }

其他的刪掉或是一句話帶過就好。

发表评论

您还没有登录,请您登录后再发表评论

jinnianshilongnian

搜索更多关于: jpatemplate 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

7. return (Collection) this.jpaTemplate.execute(new JpaCallback() { 8. public Object doInJpa(EntityManager em) throws PersistenceException { 9. Query query = em.createQuery(\y = :category\10.query.setParameter(\11.List result = query.getResultList(); 12.// do some further processing with the result list 13.return result; 14.} 15.}); 16.} 17.}

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com