java – JPA查询为不同的参数返回相同的结果
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – JPA查询为不同的参数返回相同的结果,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3246字,纯文字阅读大概需要5分钟。
内容图文
我遇到了一个问题,我的查询方法是在foreach循环中,每次我传入一个不同的参数来检索不同的信息.但是,在循环的第一次迭代之后,查询数据被缓存(我认为)并为后续循环返回相同的数据.
这是我的代码:
@Transactional(readOnly = true)
public List<InitiativeReport> getInitiativeReports() throws Exception {
try {
List<InitiativeReport> ir = new ArrayList<InitiativeReport>();
List<Initiative> in = initiativeRepository.findAll();
for(Initiative i : in) {
i.getTheme().getId(); // lazy initialize
InitiativeReport report = new InitiativeReport();
report.setId(i.getId());
report.setInitiativeId(i.getInitiativeId());
report.setName(i.getName());
report.setTheme(i.getTheme());
// this is the call to the query, which is cached after the first iteration
List<InitiativeProfileQuestion> q = initiativeProfileQuestionRepository.getQuestionsAndAnswerLogs(i.getInitiativeId());
report.setQuestions(q);
ir.add(report);
}
return ir;
}
catch (Exception e) {
throw new Exception(e);
}
这是我的存储库界面:
public interface InitiativeProfileQuestionRepository extends JpaRepository<InitiativeProfileQuestion, Long> {
@Query("select distinct q from InitiativeProfileQuestion q "
+ "left join fetch q.answers "
+ "left join fetch q.answerLogs al "
+ "where al.initiative.initiativeId = ?1 "
+ "and al.revision = al.initiative.revision
+ "order by q.question asc")
public List<InitiativeProfileQuestion> getQuestionsAndAnswerLogs(String initiativeId);
}
这是我的application.yml文件:
spring:
datasource:
dataSourceClassName: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
url: jdbc:mysql://localhost/testdb
username: root
password: XXXXXXXXX
driverClassName: com.mysql.jdbc.Driver
testOnBorrow: true
validationQuery: SELECT 1
jpa:
database-platform: org.hibernate.dialect.MySQLInnoDBDialect
database: MYSQL
openInView: false
show_sql: true
generate-ddl: false
hibernate:
ddl-auto: none
naming-strategy: org.hibernate.cfg.EJB3NamingStrategy
这个问题与我在这里发现的帖子非常相似:Native Query (JPA ) not reset and return the same old result
但是,该用户正在使用EntityManager,我的应用程序中没有EntityManager的实现 – 我让JPA完成所有工作并且只有查询注释.
任何援助将不胜感激!
解决方法:
聚会的时间不多,但对于那些现在发现这一点的人来说,你需要做的就是解决这个问题:
当您在已经启动事务的循环中查询时,您需要分离从该循环内的查询返回的实体,这些查询共享相同的id但可能具有不同的数据.
下面是一个例子:
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
@Autowired
private ProductWarehouseRepository productWarehouseRepository;
@Autowired
private EntityManager entityManager;
@Transactional
public List<Product> getProducts(){
List<Product> products = this.productRepository.findAll();
products.forEach(product -> {
List<ProductWarehouse> warehouses = this.productWarehouseRepository.findAllByProductId(product.getId());
warehouses.forEach(warehouse -> {
//THIS IS THE IMPORTANT PART
//You have to detach the entity from the session
this.entityManager.detach(warehouse);
});
product.setWarehouses(warehouses);
});
return products;
}
}
在此示例中,产品A可以在仓库ID 1中,产品B也可以,但它们可能在仓库中具有不同的数量.
当返回的结果可能在@Id列上发生冲突时,您必须从会话中分离实体.这与Hibernate中1级缓存的工作方式有关.您可以查看此链接以获取更多信息http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/objectstate.html
内容总结
以上是互联网集市为您收集整理的java – JPA查询为不同的参数返回相同的结果全部内容,希望文章能够帮你解决java – JPA查询为不同的参数返回相同的结果所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。