gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:30:19 25 4
gpt4 key购买 nike

医疗实体:

@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);
}

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

生成的日志:

hibernate :从t_med_areamedicalent0_中选择medicalent0_.med_area_id为med_area1_1_,medicalent0_.med_area_cd为med_area2_1_,medicalent0_.area_nm为area_nm3_1_

2020-02-11 15:26:01.377 TRACE 39096 --- [nio-8080-exec-2] o.s.t.i.TransactionInterceptor:正在完成 [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll] 的事务2020-02-11 15:26:01.383 TRACE 39096 --- [nio-8080-exec-2] .s.t.s.TransactionSynchronizationManager :删除了值 [org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$DefaultCrudMethodMetadata@2930e0de]来自线程 [http-nio-8080-exec-2] 的键 [public Abstract java.util.List org.springframework.data.jpa.repository.JpaRepository.findAll()]

hibernate :选择z0_.med_area_id为med_area6_0_0_,z0_.id为id1_0_0_,z0_.id为id1_0_1_,z0_.facility_code为facility2_0_1_,z0_.facility_id为facility3_0_1_,z0_.facility_nm为facility4_0_1_,z0_.insert_dt为插入_d5_0_1_,z0_。 med_area_id 为来自 t_facility z0_ 的 med_area6_0_1_,其中 z0_.med_area_id=?

hibernate :选择z0_.med_area_id为med_area6_0_0_,z0_.id为id1_0_0_,z0_.id为id1_0_1_,z0_.facility_code为facility2_0_1_,z0_.facility_id为facility3_0_1_,z0_.facility_nm为facility4_0_1_,z0_.insert_dt为插入_d5_0_1_,z0_。 med_area_id 为来自 t_facility z0_ 的 med_area6_0_1_,其中 z0_.med_area_id=?

hibernate :选择z0_.med_area_id为med_area6_0_0_,z0_.id为id1_0_0_,z0_.id为id1_0_1_,z0_.facility_code为facility2_0_1_,z0_.facility_id为facility3_0_1_,z0_.facility_nm为facility4_0_1_,z0_.insert_dt为插入_d5_0_1_,z0_。 med_area_id 为来自 t_facility z0_ 的 med_area6_0_1_,其中 z0_.med_area_id=?。

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

最佳答案

当您说“悬停”时,您是指调试时吗?如果您检查任何惰性元素,则类似于访问它们,因此 Hibernate 将尝试延迟加载实体。

关于java - 我在一个实体中有多个 OneToMany 映射。即使我没有请求该对象,Hibernate 也会加载第一个对象。这是预期的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60179097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com