我正在使用JPA在Java EE(Jersey)中创建一个应用程序,我对未初始化的实体有疑问。我有3个实体汽车,车主,房子,车可以有多个业主,业主可以有多个房子。当我返回(entityManager.find)Car然后初始化所有者。当我返回House然后所有者被初始化,但Car不是。我希望能够调用像House.getOwner()。getCar()。getId()这样的东西。现在我必须在House上打电话找到然后打电话给所有者找到汽车。我该如何解决这个问题?
@Entity
@Table(name = "House")
public class HouseEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "owner_id", nullable = false)
private OwnerEntity owner;
}
@Entity
@Table(name = "Owner")
public class OwnerEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "car_id")
private CarEntity car;
@OneToMany(mappedBy = "owner")
@JoinColumn(name = "house", nullable = false)
private Set<HouseEntity> house;
}
@Entity
@Table(name = "Car")
public class CarEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToMany(mappedBy = "owner")
private Set<OwnerEntity> owner;
}
Edit1:抱歉映射错误,班级运作良好。但是初始化的问题仍然存在。
答案 0 :(得分:0)
试试这个:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "House")
public class HouseEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne
@JoinColumn(name = "owner_id", nullable = false)
private OwnerEntity owner;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public OwnerEntity getOwner() {
return owner;
}
public void setOwner(OwnerEntity owner) {
this.owner = owner;
}
}
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.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "Owner")
public class OwnerEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne
@JoinColumn(name = "car_id")
private CarEntity car;
@OneToMany(mappedBy = "owner", fetch = FetchType.EAGER)
private Set<HouseEntity> houses;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public CarEntity getCar() {
return car;
}
public void setCar(CarEntity car) {
this.car = car;
}
public Set<HouseEntity> getHouses() {
return houses;
}
public void setHouses(Set<HouseEntity> houses) {
this.houses = houses;
}
}
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;
@Entity
@Table(name = "Car")
public class CarEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToMany(mappedBy = "car", fetch = FetchType.EAGER)
private Set<OwnerEntity> owner;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Set<OwnerEntity> getOwner() {
return owner;
}
public void setOwner(Set<OwnerEntity> owner) {
this.owner = owner;
}
public void addOwner(OwnerEntity owner){
this.owner.add(owner);
}
}
当您获取Car时,将填充整个对象图。