我得到的映射实体总是为null,但是已经设置了FetchType.EAGER。我有一个Booking实体类,它映射到另外两个实体 - Slot和Subscriber。当我获取预订实体时,这两个实体都为空
预订课程
John = 10
Lucy = 2
Tom = 4,
Hardy = 7
tom = 5
tom = 3
hardy = 7
Christoper = 4
Kim = 9
Tom = 4
john = 5
John = 7
John = 6
广告位类
import java.util.Date;
import javax.persistence.CascadeType;
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.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
@Entity
@Table(name = "BOOKING")
public class Booking {
public Booking(){
}
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "title")
private String title;
@Column(name = "descr")
private String desc;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "slotid",insertable = false, updatable = false)
private Slot slot;
private Integer slotid;
private Integer subscriberid;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "subscriberid",insertable = false, updatable = false)
private User subscriber;
@Column(name = "created")
@Temporal(TemporalType.TIMESTAMP)
private Date created;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "slotid",referencedColumnName="slotid")
public Slot getSlot() {
return slot;
}
public void setSlot(Slot slot) {
this.slot = slot;
}
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "subscriberid",referencedColumnName="userid")
public User getSubscriber() {
return subscriber;
}
public void setSubscriber(User subscriber) {
this.subscriber = subscriber;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public Integer getSlotid() {
return slotid;
}
public void setSlotid(Integer slotid) {
this.slotid = slotid;
}
public Integer getSubscriberid() {
return subscriberid;
}
public void setSubscriberid(Integer subscriberid) {
this.subscriberid = subscriberid;
}
}
订阅者 - 用户类
import java.util.Date;
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.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
@Entity
@Table(name="SLOT")
public class Slot {
public Slot(){
}
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="slotid")
private Integer id;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="ownerid",insertable = false, updatable = false)
private User user;
@Column(name="startdate")
private Date startdate;
@Column(name="enddate")
private Date enddate;
@Column(name="status")
private String status;
private Integer ownerid;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created", nullable = false, updatable=false)
@Version
private Date created;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public User getUser() {
return this.user;
}
public void setUser(User owner) {
this.user = owner;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Integer getOwnerid() {
return ownerid;
}
public void setOwnerid(Integer ownerid) {
this.ownerid = ownerid;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getStartdate() {
return startdate;
}
public void setStartdate(Date startdate) {
this.startdate = startdate;
}
public Date getEnddate() {
return enddate;
}
public void setEnddate(Date enddate) {
this.enddate = enddate;
}
}
import java.util.Date;
import javax.persistence.CascadeType;
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.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name="users")
public class User {
public User(){
}
@Id@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="userid")
private Integer userid = 0;
@Column(name = "name")
private String name;
@Column(name = "mobile")
private String mobile;
@Column(name = "password")
private String password;
@Column(name = "email")
private String email;
@Column(name = "type")
private String userType;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="cityid",insertable = false, updatable = false)
private City city;
private String cityid;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="specialityid",insertable = false, updatable = false)
private Speciality speciality;
private Integer specialityid;
@Column(name="medregno")
private String regno;
@Column(name="refcode")
private String referalcode;
public String getRegno() {
return regno;
}
public void setRegno(String regno) {
this.regno = regno;
}
public String getReferalcode() {
return referalcode;
}
public void setReferalcode(String referalcode) {
this.referalcode = referalcode;
}
@Column(name = "gender")
private String gender;
@Column(name = "active")
private boolean active;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated")
private Date updated;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created")
private Date created;
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (userid != other.userid)
return false;
return true;
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getUpdated() {
return updated;
}
public void setUpdated(Date updated) {
this.updated = updated;
}
public String getCityid() {
return cityid;
}
public void setCityid(String cityid) {
this.cityid = cityid;
}
public Speciality getSpeciality() {
return speciality;
}
public void setSpeciality(Speciality speciality) {
this.speciality = speciality;
}
public Integer getSpecialityid() {
return specialityid;
}
public void setSpecialityid(Integer specialityid) {
this.specialityid = specialityid;
}
}
和booking.getSlot()
返回booking.getSubscriber()
如果我在映射时错过了一些配置,请告诉我
EDIT1 添加了实体加载方式的代码
null
我正在保存实体并重新加载它。
答案 0 :(得分:2)
它不起作用,因为Hibernate正在重新调整它已经在其第一级缓存中的相同实例,该缓存没有引用其他2个实体中的任何一个。
要解决此问题,您必须执行session.refresh(booking)
而不是执行查询。
答案 1 :(得分:1)
在您的代码中:
booking.setSlotid(slotid);
booking.setSubscriberid(subscriberid);
您只是设置整数值而不是对象。而不是这样,尝试设置对象:
booking.setSlot(new Slot(slotid));
booking.setSubscriber(new Subscriber(subscriberid));
但正如@Augusto所说,你在会话中所拥有的关联(Slot and Subscriber)并不是完整的对象,它们只包含它们的id。这就是为什么你不能得到这些对象的其他领域。