[英]How to make a select statement on a list contained within an object in spring?
How do you make a select statement or filter a List that is nested within an entity in spring? 您如何在春季创建select语句或过滤嵌套在实体内的列表? I have an object that looks like this... 我有一个看起来像这样的物体...
@Entity
@Table(name = "employee")
public class Employee {
...
@OneToMany(mappedBy = "_employee", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
@JsonManagedReference
Set<Deal> _deals;
@OneToMany(mappedBy = "_employee", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
@JsonManagedReference //This is simply to avoid a stackoverflow error according to this link http://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue
Set<Recommendation> _recommendations;
@OneToMany(mappedBy = "_employee", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
@JsonManagedReference //This is simply to avoid a stackoverflow error according to this link http://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue
Set<Event> _events;
public Employee() {
}
//getters and setters
....
I get employees with a repository that is accessed by a service class. 我为员工提供了一个由服务类访问的存储库。
The repository looks like this. 存储库如下所示。
public interface EmployeeRepository extends CrudRepository<Employee, Long> {
public Employee getEmployeeById(Long _id);
public Employee getEmployeeBy_username(String username);
}
So bascially when I get an employee by its id, it returns the above lists. 因此,特别是当我通过其ID获取一名雇员时,它会返回上述列表。 When an employee is retrieved I need to do a select statement or filter in some way _deals, _recommendations and _events. 找回员工时,我需要以某种方式进行选择语句或过滤器_deals,_recommendations和_events。 So that only those who have the boolean attribute _active=true returned. 这样只有那些具有boolean属性_active = true的人才返回。 As it is now, all deals recommendations and events are returned whether they are active or not. 到目前为止,无论是否有效,都会返回所有交易建议和事件。 How do I filter or select from these lists only active objects? 如何过滤或从这些列表中仅选择活动对象?
You almost always select a single Entity type per query, and preferably you would do the filtering in the database. 您几乎总是为每个查询选择一个Entity类型,最好在数据库中进行过滤。 If you want the Deals, Recommendations and Events belonging to a specific Employee, I would normally put these methods in the Repository belonging to entity type I'm trying to load, it could look like this: 如果您希望交易,推荐和事件属于特定的雇员,通常我会将这些方法放在属于我要加载的实体类型的存储库中,如下所示:
@Repository
public interface DealRepository extends JpaRepository<Deal, Long> {
@Query("select d from Deal d where d.active= true and d.employee.id = :employeeId")
List<Deal> findActiveDeals(@Param("employeeId") long employeeId);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.