首页 > 解决方案 > 我在一个实体中有多个 OneToMany 映射。即使我没有请求该对象,Hibernate 也会加载第一个对象。这是预期的行为吗?

问题描述

医疗机构:

@Entity
@Table(name="t_med_area")

public class MedicalEntity {


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


@Column(name="med_area_cd")
private String area_code;

@Column(name="area_nm")
private String area_description;

@OneToMany(fetch = FetchType.LAZY,mappedBy="medical_area_id")
private List<ProviderEntity> resources;


@OneToMany(fetch = FetchType.LAZY,mappedBy="medAreaId")
private List<FacilityEntity> facility;



public List<ProviderEntity> getResources() {
    return resources;
}

public void setResources(List<ProviderEntity> resources) {
    this.resources = resources;
}

public List<FacilityEntity> getFacility() {
    return facility;
}

public void setFacility(List<FacilityEntity> facility) {
    this.facility = facility;
}

public Integer getMed_area_id() {
    return med_area_id;
}

public void setMed_area_id(Integer med_area_id) {
    this.med_area_id = med_area_id;
}

public String getArea_code() {
    return area_code;
}

public void setArea_code(String area_code) {
    this.area_code = area_code;
}

public String getArea_description() {
    return area_description;
}

public void setArea_description(String area_description) {
    this.area_description = area_description;
}

设施实体:

@Entity
@Table(name="t_facility")

public class FacilityEntity implements Serializable{

private static final long serialVersionUID = 1L;

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

@Column(name="facility_id")
private int facilityId;

@Column(name="facility_nm")
private String facilityName;

@Column(name="facility_code")
private String facilityCode;


@OneToMany(fetch = FetchType.LAZY,mappedBy="facility_id")
private List<ProviderEntity> resources;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="med_area_id")
private MedicalEntity medAreaId;

@Column(name="insert_dt")
private Date insertDt;



public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public int getFacilityId() {
    return facilityId;
}

public void setFacilityId(int facilityId) {
    this.facilityId = facilityId;
}

public String getFacilityName() {
    return facilityName;
}

public void setFacilityName(String facilityName) {
    this.facilityName = facilityName;
}

public String getFacilityCode() {
    return facilityCode;
}

public void setFacilityCode(String facilityCode) {
    this.facilityCode = facilityCode;
}

public List<ProviderEntity> getResources() {
    return resources;
}

public void setResources(List<ProviderEntity> resources) {
    this.resources = resources;
}

public MedicalEntity getMedAreaId() {
    return medAreaId;
}

public void setMedAreaId(MedicalEntity medAreaId) {
    this.medAreaId = medAreaId;
}

public Date getInsertDt() {
    return insertDt;
}

public void setInsertDt(Date insertDt) {
    this.insertDt = insertDt;
}


public FacilityEntity(Integer id, String facility_name, String facility_code) {
    super();
    this.id = id;
    this.facilityName = facility_name;
    this.facilityCode = facility_code;
}

public FacilityEntity() {
    super();
}

}

提供者实体:

@Entity
@Table(name="t_provider")
public class ProviderEntity implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="provider_id")
private Integer provider_id;

@Column(name="resource_cd")
private String resource_code;

@Column(name="first_nm")
private String first_name;

@Column(name="last_nm")
private String last_name;

@Column(name="middle_nm")
private String middle_name;

@Column(name="title_nm")
private String title;

@Column(name="department_nm")
private String department_name;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="home_med_area_id")
private MedicalEntity medical_area_id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="home_facility_id")
private FacilityEntity facility_id;


public Integer getProvider_id() {
    return provider_id;
}

public void setProvider_id(Integer provider_id) {
    this.provider_id = provider_id;
}

public String getResource_code() {
    return resource_code;
}

public void setResource_code(String resource_code) {
    this.resource_code = resource_code;
}

public String getFirst_name() {
    return first_name;
}

public void setFirst_name(String first_name) {
    this.first_name = first_name;
}

public String getLast_name() {
    return last_name;
}

public void setLast_name(String last_name) {
    this.last_name = last_name;
}

public String getMiddle_name() {
    return middle_name;
}

public void setMiddle_name(String middle_name) {
    this.middle_name = middle_name;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDepartment_name() {
    return department_name;
}

public void setDepartment_name(String department_name) {
    this.department_name = department_name;
}

public MedicalEntity getMedical_area_id() {
    return medical_area_id;
}

public void setMedical_area_id(MedicalEntity medical_area_id) {
    this.medical_area_id = medical_area_id;
}

public FacilityEntity getFacility_id() {
    return facility_id;
}

public void setFacility_id(FacilityEntity facility_id) {
    this.facility_id = facility_id;
}

public ProviderEntity() {
    super();

}

}

服务层:

    List<MedicalEntity> result=medicalAreaRepository.findAll();


    //transforming entity into DTO and setting properties based on UI requirements
    for(MedicalEntity medicalEntity:result)
    {

        MedicalDTO medicalDTO=new MedicalDTO();
        medicalDTO.setArea_code(medicalEntity.getArea_code());
        medicalDTO.setArea_description(medicalEntity.getArea_description());
        medicalDTO.setId(medicalEntity.getMed_area_id());
        //System.out.println(medicalEntity.getResources());
        medicalResponse.addElementsToList(medicalDTO);
    }

当我将鼠标悬停在我的列表结果上时,它会自动触发查询以加载设施。

生成的日志:

休眠:选择medicalent0_.med_area_id 作为med_area1_1_,medicalent0_.med_area_cd 作为med_area2_1_,medicalent0_.area_nm 作为area_nm3_1_ from t_med_area medicalent0_

2020-02-11 15:26:01.377 TRACE 39096 --- [nio-8080-exec-2] ostiTransactionInterceptor:为 [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll] 完成交易 2020-02- 11 15:26:01.383 TRACE 39096 --- [nio-8080-exec-2] .stsTransactionSynchronizationManager:删除了键 [公共抽象 java 的值 [org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$DefaultCrudMethodMetadata@2930e0de] .util.List org.springframework.data.jpa.repository.JpaRepository.findAll()] 来自线程 [http-nio-8080-exec-2]

Hibernate: select z0_.med_area_id as med_area6_0_0_, z0_.id as id1_0_0_, z0_.id as id1_0_1_, z0_.facility_code as facility2_0_1_, z0_.facility_id as facility3_0_1_, z0_.facility_nm as facility4_0_1_, z0_.insert_dt as insert_d5_0_1_, z0_.med_area_id as med_area6_0_1_从 t_facility z0_ where z0_.med_area_id=?

Hibernate: select z0_.med_area_id as med_area6_0_0_, z0_.id as id1_0_0_, z0_.id as id1_0_1_, z0_.facility_code as facility2_0_1_, z0_.facility_id as facility3_0_1_, z0_.facility_nm as facility4_0_1_, z0_.insert_dt as insert_d5_0_1_, z0_.med_area_id as med_area6_0_1_从 t_facility z0_ where z0_.med_area_id=?

Hibernate: select z0_.med_area_id as med_area6_0_0_, z0_.id as id1_0_0_, z0_.id as id1_0_1_, z0_.facility_code as facility2_0_1_, z0_.facility_id as facility3_0_1_, z0_.facility_nm as facility4_0_1_, z0_.insert_dt as insert_d5_0_1_, z0_.med_area_id as med_area6_0_1_来自 t_facility z0_ 其中 z0_.med_area_id=?。

我的问题是:它为什么要获取 FacilityEntity 的详细信息?我没有明确地进行任何调用来获取 FacilityEntity 的属性。

标签: javahibernatejpa

解决方案


当您说“悬停”时,您的意思是在调试时?如果您检查任何类似于访问它们的惰性元素,那么休眠将尝试延迟加载实体。


推荐阅读