我试图通过测试一些CRUD操作来学习弹簧数据JPA JpaRepository.
我碰到两个方法save和saveAndFlush.我没有区分这两者.在调用时save我的更改也被保存到数据库中,因此有什么用处saveAndFlush.
我正在选择两个id列但指定了错误:
org.hibernate.QueryException: **query specified join fetching, but the owner of the fetched association was not present in the select list**
[FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=r,role=null,tableName=REVISIONS,tableAlias=revision1_,origin=ENTITY_CHANGED_IN_REVISION entitychan0_,columns={entitychan0_.REV_ID ,className=ru.csbi.registry.domain.envers.Revision}}] [ select ec.id as entityChangeId, r.id as revisionId from ru.csbi.registry.domain.envers.EntityChange as ec inner join fetch ec.revision as r where ec.groupEntityId = :groupEntityId and ec.groupName = :groupName and r.timestamp < :entityDateFrom and r.timestamp > :entityDateTo and ( ec.revisionType in (0, 5, 1, 4, 2 ) and not ( ec.otherGroupEntityModified = false and …Run Code Online (Sandbox Code Playgroud) 我在服务器日志中收到警告"使用集合提取指定的firstResult/maxResults;在内存中应用!" .然而一切正常.但我不想要这个警告.
public employee find(int id) {
return (employee) getEntityManager().createQuery(QUERY).setParameter("id", id).getSingleResult();
}
Run Code Online (Sandbox Code Playgroud)
QUERY = "from employee as emp left join fetch emp.salary left join fetch emp.department where emp.id = :id"
Run Code Online (Sandbox Code Playgroud) 我试图在Spring Data中使用JPQL优化基本查询,以避免对数据库进行多次查询,并使用JOIN Fetch检索一个查询中的所有信息,并且我不断收到此异常:
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=Business.Countries,tableAlias=country1_,origin=BUSINESS.COAPPLICANTS coapplican0_,columns={coapplican0_.Country_Id ,className=com.medifast.entity.core.Country}}] [select count(ca) from com.medifast.entity.core.CoApplicant ca LEFT JOIN FETCH ca.country LEFT JOIN FETCH ca.state where ca.client.id = :clientId]; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=Business.Countries,tableAlias=country1_,origin=BUSINESS.COAPPLICANTS coapplican0_,columns={coapplican0_.Country_Id …Run Code Online (Sandbox Code Playgroud) 我正在为实体列表实现搜索/过滤服务,使用具有规范和分页功能的 Spring Data JPA 存储库。我正在尝试减少查询次数(n+1 问题)并使用标准获取机制获取嵌套数据。
我有两个实体类:
@Entity
@Table(name = "delegations")
public class Delegation {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@ManyToOne
private Customer customer;
// more fields, getters, setters, business logic...
}
Run Code Online (Sandbox Code Playgroud)
和
@Entity
@Table(name = "customers")
public class Customer {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
// more fields, getters, setters, business logic...
}
Run Code Online (Sandbox Code Playgroud)
DTO过滤器类:
public class DelegationFilter {
private String customerName;
// more filters, getters, setters...
}
Run Code Online (Sandbox Code Playgroud)
和搜索/过滤服务:
public class DelegationService {
public Page<Delegation> findAll(DelegationFilter …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring Data JPA存储库(1.7.2),通常遇到以下情况:
fetch join)Page<Foo>而不是List<Foo>我需要提供countQuery给@Query在返回的存储库中使用提取联接的每个对象Page。此问题已在此StackOverflow问题中进行了讨论
我典型的存储库方法如下所示:
@Query(value = "SELECT e FROM Employee e LEFT JOIN FETCH e.addresses a " +
"WHERE e.company.id = :companyId " +
"AND e.deleted = false " +
"AND e.primaryAddress.deleted = false " +
"ORDER BY e.id, a.id",
countQuery="SELECT count(e) FROM Employee e WHERE e.companyId = :companyId AND e.deleted = false AND e.primaryAddress.deleted = false"
)
Page<Employee> findAllEmployeesWithAddressesForCompany(@Param("companyId") long …Run Code Online (Sandbox Code Playgroud)