【问题标题】:How to write a Repository method for a parent entity that returns child entities?如何为返回子实体的父实体编写 Repository 方法?
【发布时间】:2017-01-14 03:21:01
【问题描述】:

我正在设计两个实体,一个叫Country,一个叫CountryDetail。从表的角度来看,COUNTRY 表将是父表,COUNTRY_DETAIL 表将是子表。在COUNTRY 表中,将有一个名为 COUNTRY_CODE 的唯一属性(注意这不是主键;主键将是基于数字序列的值)。此代码将是连接子表的外键,在此子表中,父表中的每个COUNTRY_CODE 将有 3 个条目,以 3 种不同的语言表示国家/地区名称。以下是实体类:

Country.java

    @Entity
    public class Country
    {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "COUNTRY_ID")
        private long id;

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

        /*public getters*/
    }

CountryDetail.java

  @Entity
    public class CountryDetail
    {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "COUNTRY_DETAIL_ID")
        private long id;

        @ManyToOne
        @JoinColumn(name="COUNTRY_CODE", referencedColumnName = "COUNTRY_CODE")
        private Country country;
        @Column(nullable = false)
        private String languageCode;
        @Column(nullable = false, unique = true)
        private String countryNameInLanguage;

        /*public getters*/
    }

我的问题是,如何在 JpaRepository 的扩展中编写自定义的“findBy...”接口方法,该方法键入 Country,它将返回与输入参数匹配的 CountryDetail 元素的集合对于CountryDetail 类的languageCode 属性?

public interface CountryRepository extends JpaRepository<Country, Long>

如果将存储库键入为 CountryDetail 而不是 Country,我知道该怎么做,但我想知道如何通过父实体而不是直接通过子实体进行操作,即使输入参数(languageCode)只存在于子实体中。

谢谢。

【问题讨论】:

  • CountryRepository 的查询方法将返回 Country...
  • 我知道返回另一种类型而不是 Repository 类型的唯一方法是创建自定义查询并自己实例化对象。比如:@Query(“SELECT new CountryDetails(c. countryCode) FROM Country c where c.id=:id”)
  • @alexbt 毕竟我最终写了一个自定义查询

标签: java spring hibernate jpa spring-boot


【解决方案1】:

这是可能的,但很复杂:请看下面的例子:

家长:

 @Entity
    @Table(name="PARENT")
    public class Parent {


        @Id
        @Column(name="PARENT_ID")
        private int parentId;

        @Column(name="PARENT_NAME")
        private String parentName;

       @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
       private Child child;

    }

孩子:

@Entity
@Table(name="CHILD")
public class Child {


    @Id
    @Column(name="CHILD_ID")
    private int childId;

    @Column(name="CHILD_NAME")
    private String childName;

    @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    private Parent parent;
}

定义一个接口以从父存储库加载子实体:

子实体:

public interface ChildEntity {

    @Value("#{target.child.childId}")
    int getChildId() ;

    @Value("#{target.child.childName}")
    String getChildName();

}

父存储库:

public interface ParentRepository extends JpaRepository<Parent, Integer> {


    public ChildEntity findByParentName(String parentName);

}

测试类:

ChildEntity chi=rep.findByParentName("<<NAME>>");
System.out.println(chi.getChildId()+" "+chi.getChildName());

输出:

CHILD ID 1000 CHILD NAME child1

【讨论】:

  • 我认为这对其他人会有所帮助,但就我而言,我在一对多关联(没有一对一关联)方面遇到了很多问题,所以我最终改用自定义查询.
猜你喜欢
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 2014-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多