简体   繁体   English

如何使用子实体检索父实体-Spring数据JPA

[英]How to retrieve parent entity using child entity-Spring data JPA

Here is the DB table structure with one-to-many relation.这是具有一对多关系的数据库表结构。 EMP_DETAILS and MODEL_DETAILS are connected with action_id with a one-to-many relationship. EMP_DETAILS 和 MODEL_DETAILS 与 action_id 以一对多的关系连接。

Below are the entities create for both child and parent tables.下面是为子表和父表创建的实体。

EmpDetails.java EmpDetails.java

@Table(EMP_DETAILS)
@Entity
public class EmpDetails{

 @Id
 @Column(name="eid", nullable=false)
 private Integer eid;

 @OneToMany(cascade=CascadeType.ALL, mappedBy="empDetails"
 private Set<ModelDetails> modelDetailsSet;

 //other column mappings
}

ModelDetails.java型号详情.java

@Table(MODEL_DETAILS)
@Entity
public class ModelDetails{

  @Id
  @Column(name="id)
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Integer id;

  @ManyToOne(fetch=FetchType.LAZY)
  @JoinColumn(name="eid",referencedColumnName="eid", nullable=false)
  private EmpDetails empDetails;

  @Column(name="model_name")
  private String modelName;
  @Column(name="created_on")
  private Date createdOn;
  //Other column mappings and getters setters

}

The below method does returns me the Model_Details records.下面的方法确实返回了 Model_Details 记录。

List<ModelDetails> modelDetailList=findByModelNameOrderByCreatedOnDesc("Any_Existng_Name");

But when we try to get the parent record related to the child record I get null like below..,但是当我们尝试获取与子记录相关的父记录时,我得到 null 如下所示..,

modelDetailList.get(0).getEmpDetails();//THIS RETURNS NULL!

Any solution for this对此的任何解决方案

@ManyToOne(fetch=FetchType.LAZY)

This is why the parent is null.这就是为什么父级是 null。 You told JPA you don't want to automatically fetch the linked entity.您告诉 JPA 您不想自动获取链接实体。 This is generally a good thing as you don't want to load extra data that may not be needed all the time.这通常是一件好事,因为您不想加载可能一直不需要的额外数据。

You could change that to @ManyToOne(fetch=FetchType.EAGER) if you'll need EmpDetails every time to load ModelDetails.如果您每次都需要 EmpDetails 来加载 ModelDetails,则可以将其更改为@ManyToOne(fetch=FetchType.EAGER)

If you only need the EmpDeatils for this query you'll need to modify findByModelNameOrderByCreatedOnDesc to retrieve the associated entity.如果您只需要此查询的 EmpDeutils,则需要修改 findByModelNameOrderByCreatedOnDesc 以检索关联的实体。

Your query should look something like SELECT md FROM ModelDetails md JOIN FETCH EmpDetails = WHERE md.name =:name ORDER BY createdOn desc您的查询应该类似于SELECT md FROM ModelDetails md JOIN FETCH EmpDetails = WHERE md.name =:name ORDER BY createdOn desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 spring 数据 JPA 为现有父实体添加子实体? - How to add child entity for existing parent entity using spring data JPA? 如何从 JPA 中的父实体检索子数据 - How to retrieve child data from parent entity in JPA 如何使用 Spring JPA DATA 仅获取特定的子实体及其具有特定值的父实体 - How to fetch only the specific child entity along with its Parent entities with specific value using Spring JPA DATA 在 JPA 中使用父实体获取子实体 - Fetch a child entity using parent entity in JPA 如何使用JPA查询来检索父实体和最后一个子实体 - How to use JPA query to retrieve parent and last child entity 如何从spring data jpa中的子主键获取父实体? - How to get the parent entity from the child primary key in spring data jpa? 使用spring data jpa保存和更新实体父子的最佳方法 - Best way to save and update entity Parent and child using spring data jpa 如何在 Spring JPA/Hibernate 中使用 JoinTable 仅通过 ID 设置引用父实体的子实体 - How to set up a child entity referencing parent by only ID using a JoinTable in Spring JPA/Hibernate Spring JPA从新的子实体合并父实体 - Spring JPA merge parent entity from new child entity Spring boot JPA:删除多对一关系的父实体时如何保留子实体? - Spring boot JPA: How to keep the child entity when deleting the parent entity in a many-to-one relationship?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM