【问题标题】:How to access a child element in a query using jpa and hibernate - SpringBoot如何使用 jpa 和 hibernate 访问查询中的子元素 - Spring Boot
【发布时间】:2021-11-16 11:12:02
【问题描述】:

我想知道如何在 Spring boot 中使用它们的名称作为参数来查询一些子对象。

假设我有一个班级家长,与孩子有一对多关系

孩子有一个叫名字的参数。所以我想使用like "%name%" 进行查询,这样查询会返回一个包含查询找到的所有子项的列表。

我想做这样的事情:

@Query("select c from Parent c where lower(c.name) LIKE lower(CONCAT('%', :name, '%')) ")
    List<Parent> findByNameLIKE(@Param("name") String name);

但是使用一个父母拥有的孩子,我会使用父母的 id 来搜索它。不知道我解释的好不好,问题是我一直没能实现类之间的join。

谢谢!

编辑。这些是我的课程:

公司

@Entity(name = "Compania")
@Table(
    name = "compania"
)
public class Compania {
    @Id
    @SequenceGenerator(
        name = "compania_sequence",
        sequenceName = "compania_sequence",
        allocationSize = 1
    )
    @GeneratedValue(
        strategy = GenerationType.AUTO,
        generator = "compania_sequence"
    )
    @Column(
        nullable = false
    )
    private Long id;


    @Column(
        name = "name",
        nullable = false,
        unique = true
    )
    private String name;

    @Column(
        name = "bajas"
    )
    private String bajas;

    @OneToMany(
        cascade = CascadeType.ALL,
        fetch = FetchType.LAZY,
        orphanRemoval = true
    )
    private List<DefaultGroup> default_group;

还有我的子班,default_group

@Entity
@Table
public class DefaultGroup {
    @Id
    @SequenceGenerator(
        name = "defaultGroup_sequence",
        sequenceName = "defaultGroup_sequence",
        allocationSize = 1
    )
    @GeneratedValue(
        strategy = GenerationType.SEQUENCE,
        generator = "defaultGroup_sequence"
    )
    @Column(
        nullable = false
    )
    @JsonIgnore
    private Long id;

    @Column(
        name = "idRef",
        nullable = false
    )
    private int idRef;

    @Column(
        name = "name",
        nullable = false
    )
    private String name;

    @Column(
        name = "path"
    )
    private String path;

    @ManyToOne()
    private Compania compania;

所以我想将 defaultGroups 分配给 ID 为 1 的 Compania,其中 defaultGroup 的名称类似于 %x%。我不知道如何实现这一点。谢谢!

【问题讨论】:

    标签: java spring-boot api hibernate jpa


    【解决方案1】:

    如果您在父实体Parent 中有一个Child 实体,如下所示:

    @Entity
    class Parent {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
        
        ...
    
        @OneToMany(mappedBy = "parent")
        private List<Child> childs;
    
        ....
    

    然后你可以使用这样的查询:

    @Repository
    public interface ParentRepository extends JpaRepository<Parent, Long> {
        List<Parent> getAllByChildsNameLike(String childName);
    }
    

    spring data 会为你生成查询。


    如果我们想通过 id 和其他约束来获取父母的孩子,我们可以这样做:

    @Repository
    public interface ChildRepository extends JpaRepository<Child, Long> {
        List<Child> getAllByParentIdAndNameLike(Long parentId, String name);
    }
    

    或者,如果您想在 ParentRepository 中使用 jpa 查询:

    @Query("select c from Child c where c.parent.id = ?1 and c.name like ?2")
    List<Child> getChilds(Long parentId, String name);
    

    您可以将childs 字段和实体替换为您的实体。

    【讨论】:

    • 我想我解释得不对,我想要与一个父母相关的子元素。但是那些孩子的名字就像我正在路过的那个。例如,对于 id 为 3 的 Parent,获取所有名称类似于 x 的孩子。谢谢! (我刚刚添加了更多信息,如果信息很少,请见谅)
    • @Varox 查看更新后的答案,它是否适合您?
    猜你喜欢
    • 2019-01-09
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多