- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下类(class):
公司类别:
public class Company {
@JoinTable(name = "company_employee", joinColumns = @JoinColumn(name = "company_id") , inverseJoinColumns = @JoinColumn(name = "employee_id") )
@ManyToMany(fetch = FetchType.LAZY)
private Set<Employee> employees;
@Column(name = "score")
private BigDecimal score;
}
和 Employee.class
public class Employee {
@ManyToMany(fetch = FetchType.EAGER, mappedBy="employees")
private Set<Company> companies;
}
Company 的 Score 列在数据库中始终为空,并且从未通过 dao 更新,因为还有其他表包含每个唯一的 Company-Employee 对的分数。我需要 Score 的值,仅适用于通过 id 获取 Employee 的情况,因此这种情况下 Set 中的所有 Company 实例都应包含分数,因此我将在获取 Employee 时获得 Employee-Company 分数对。我有以下代码来实现这一点:
public Employee get(Long id) {
Employee emp = (Employee) dao.find(id);
List<Company> compList = compnanyService.getByEmpId(id);
Set<Company> compSet = new HashSet<Company>(compList);
emp.setCompanies(compSet);
return emp;
}
而Company Dao包含方法:
public List<Company> getByEmpId(Long id) {
final Query query = this.entityManager.createNativeQuery("select company.comp_id, ...some other fields, score.score from company join score on company.company_id=score.company_id where score.employee_id=:employee_id",
Company.class);
query.setParameter("employee_id", id);
List<Company> comps = query.getResultList();
return comps;
}
问题是 getByEmpId(id)
给出 ResultList
哪里company.score
虽然在数据库中执行但它不为空,但为空。
我怀疑存在一些缓存干预,因此我尝试从 native 查询中删除一些列,并且它应该在映射时调用带有“未找到列”(或类似)消息的异常,但此方法仍然给出List<Company>
尽管 Hibernate 会在控制台中打印出我的 native 查询以及我所做的所有更改,但所有字段都已就位。我在这里做错了什么以及如何实现我需要的?谢谢。
最佳答案
它可能与一级缓存相关,在使用 native SQL 查询时可能会不同步。来自 here :
If you bypass JPA and execute DML directly on the database, either through native SQL queries, JDBC, or JPQL UPDATE or DELETE queries, then the database can be out of synch with the 1st level cache. If you had accessed objects before executing the DML, they will have the old state and not include the changes. Depending on what you are doing this may be ok, otherwise you may want to refresh the affected objects from the database.
因此,您可以尝试使用 EntityManager
中的 refresh
方法。
关于java - JPA nativeQuery 返回缓存的 resultList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34484203/
我使用以下方式获取查询结果: String query = "select a from TABLE a"; Query q = em.createQuery(query); List res = q
我已经尝试这个一周了。我的 session bean 中有一个工作方法,它工作得非常好,并显示 MySQL 表中的用户列表。 我想使用 radio 输入类型选择特定用户并将其详细信息插入到另一个表中。
我有以下类(class): 公司类别: public class Company { @JoinTable(name = "company_employee", joinColumns = @
我已经更新了事务中的一个对象,它已成功更新并与数据库同步...但是每当我使用 query.getResultList() 检索实体时,它都不会返回包含更新的对象和值的列表。 这里的代码指的是我在做什么
我是一名优秀的程序员,十分优秀!