我正在spring / hibernate中创建一个基于Web的应用程序。在数据库中,我正在使用onetomany和manytoone关系。我在用户中有很多关系,可以为位置上的用户提供位置和onetomany关系。在两个实体中,我都启用了延迟加载选项。根据我的理解,如果启用此选项,则在显式调用之前不应执行获取位置的数据库查询,但每当我从userdao执行获取时,执行以下查询,这使得我认为即使在执行了懒惰之后也是如此选项打开它正在检索位置信息。任何人都可以让我知道我做错了什么,或者这是预期的行为。
Below is my user entity code
package com.kwisque.database.model;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
@Entity
@Table(name = "USERS")
public class User implements Serializable{
@Id
@Column(name = "USER_ID", unique = true, nullable = false)
private String userId;
@Column(name = "NAME", nullable = true, length = 32)
private String name;
@Column(name = "PASSWORD", nullable = false, length = 64)
private String password;
@Column(name = "EMAIL_ID", nullable = true, length = 128)
private String emailId;
@Column(name = "ACTIVE", nullable = false, length = 1)
private Integer active;
@Column(name = "PROVIDER", nullable = false, length = 32)
private String provider;
@ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinTable(
name = "USER_ROLE",
joinColumns = @JoinColumn(name = "USER_ID"),
inverseJoinColumns = @JoinColumn(name = "ROLE_ID")
)
private Set<Role> roles = new HashSet<>();
//@ManyToOne(fetch = FetchType.LAZY)
//@JoinColumn(name = "LOCATION_ID", nullable = true)
@ManyToOne(fetch=FetchType.LAZY, cascade={javax.persistence.CascadeType.ALL})
@JoinTable(name="USER_LOCATION_INFO",
joinColumns={@javax.persistence.JoinColumn(name="USER_ID")},
inverseJoinColumns={@javax.persistence.JoinColumn(name="LOCATION_ID")})
private Location location;
// @OneToMany(fetch = FetchType.LAZY)
// @JoinColumn(name = "SPECIALIZATION_ID", nullable = true)
@OneToMany(fetch=FetchType.LAZY, cascade={javax.persistence.CascadeType.ALL})
@JoinTable(name="USER_SPECIALIZATION_INFO",
joinColumns={@javax.persistence.JoinColumn(name="USER_ID")},
inverseJoinColumns={@javax.persistence.JoinColumn(name="SPECIALIZATION_ID")})
private Set<Specialization> specialization = new HashSet<>();
public User() {
}
public User(final String userId, final String name, final String password, final String emailId, final Integer active, final String provider, final Set<Role> roles, final Location location) {
this.userId = userId;
this.name = name;
this.password = password;
this.emailId = emailId;
this.active = active;
this.provider = provider;
this.roles = roles;
this.location = location;
}
public String getUserId() {
return userId;
}
public void setUserId(final String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(final String password) {
this.password = password;
}
public Integer getActive() {
return active;
}
public void setActive(final Integer active) {
this.active = active;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(final Set<Role> roles) {
this.roles = roles;
}
public String getProvider() {
return provider;
}
public void setProvider(final String provider) {
this.provider = provider;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(final String emailId) {
this.emailId = emailId;
}
public Location getLocation() {
return location;
}
public void setLocation(final Location location) {
this.location = location;
}
}
Location entity code
package com.kwisque.database.model;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Table(name = "LOCATION")
public class Location implements Serializable {
private static final long serialVersionUID = -7153748534015057865L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "LOCATION_ID", unique = true, nullable = false)
private Integer locationId;
@Column(name = "ZIP_CODE", nullable = true, length = 132)
private String zipCode;
@Column(name = "STATE_ABBR", nullable = true, length = 132)
private String stateAbbr;
@Column(name = "LATITUDE", nullable = true, length = 132)
private double latitude;
@Column(name = "LONGITUDE", nullable = true, length = 132)
private double longitude;
@Column(name = "CITY", nullable = true, length = 132)
private String city;
@Column(name = "STATE", nullable = true, length = 132)
private String state;
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "location")
private Set<User> users;
public double getLatitude() {
return this.latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return this.longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public Location() {
}
public Location(Integer locationId, long longitude, String city, long latitude, String zipCode, String state,
String stateAbbr, Set<User> users) {
this.locationId = locationId;
this.longitude = longitude;
this.latitude = latitude;
this.city = city;
this.state = state;
this.stateAbbr = stateAbbr;
this.users = users;
}
public Integer getLocationId() {
return this.locationId;
}
public void setLocationId(Integer locationId) {
this.locationId = locationId;
}
@JsonIgnore
public Set<User> getUser() {
return this.users;
}
@JsonIgnore
public void setUser(Set<User> users) {
this.users = users;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public String getZipCode() {
return this.zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getStateAbbr() {
return this.stateAbbr;
}
public void setStateAbbr(String stateAbbr) {
this.stateAbbr = stateAbbr;
}
}
Query being executed at my get from USerDao
Hibernate: select user0_.USER_ID as USER_ID1_3_0_, user0_.ACTIVE as ACTIVE2_3_0_, user0_.EMAIL_ID as EMAIL_ID3_3_0_, user0_.NAME as NAME4_3_0_, user0_.PASSWORD as PASSWORD5_3_0_, user0_.PROVIDER as PROVIDER6_3_0_, user0_1_.LOCATION_ID as LOCATION1_4_0_, roles1_.USER_ID as USER_ID1_3_1_, role2_.ROLE_ID as ROLE_ID2_5_1_, role2_.ROLE_ID as ROLE_ID1_1_2_, role2_.NAME as NAME2_1_2_ from USERS user0_ left outer join USER_LOCATION_INFO user0_1_ on user0_.USER_ID=user0_1_.USER_ID left outer join USER_ROLE roles1_ on user0_.USER_ID=roles1_.USER_ID left outer join ROLE role2_ on roles1_.ROLE_ID=role2_.ROLE_ID where user0_.USER_ID=?