简体   繁体   English

春天如何在包含在对象中的列表上做出选择语句?

[英]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.

相关问题 如何检查给定地图的所有键是否存在于列表中包含的地图之一中<Map<String, Object> &gt; 在 Java 中有效吗? - How to check if all Keys of the given Map are present in a one of the Maps contained within a List<Map<String, Object>> in Java efficiently? 如何使用 Java Z38008DD81C2F4D79185ECF6E0CE8AF1 从 XML 制作 object 列表 - How to make an object list from XML with Java Spring? 如何在Spring MVC中制作属性文件的List对象? - How to make List object of properties file in Spring MVC? 选择实体中包含的列表的子集 - Select subset of the list contained in an entity 在Java中将包含MediaPlayer的对象设置为null后,如何使它停止? - How to make a MediaPlayer stop after the object that contained it was set to null in Java? 如何在Thymeleaf中使用对象中包含的HashMap填充下拉列表? - How to populate dropdown list with HashMap contained in an object, in Thymeleaf? Java:如何访问对象列表中包含的2D数组 - Java: How to access a 2D array contained in an object list 如何将具有列表属性的对象列表转换为包含所有列表属性迭代的列表 - how to transform object list with list attribute to one list with all list attribute itemes contained 如何使用Spring从数据库中另一个对象的属性列表中选择一个对象 - how to select an object from a list which is an attribute of another object in a database with spring 视图未正确包含在单个列表项中 - Views not properly contained within single list item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM