Java JPA FetchType.EAGER does not work(Java JPA FetchType.EAGER 不起作用)
问题描述
我正在使用 JPA 在 Java EE (Jersey) 中制作应用程序,但我遇到了未初始化实体的问题.我有 3 个实体 Car、Owner、House,其中汽车可以有多个车主,车主可以有多个房屋.当我返回(entityManager.find)汽车时,所有者被初始化.当我返回 House 时, Owner 被初始化,但 Car 没有.我希望能够调用诸如 House.getOwner().getCar().getId() 之类的东西.现在我必须在 House 上调用 find,然后在 Owner 上调用 find 来获取 Car.我该如何解决这个问题?
I'm making an application in Java EE (Jersey) with JPA where I have a problem with the uninitialized entities. I have 3 entities Car, Owner, House where the car can have multiple owners and owners can have multiple houses. When i return (entityManager.find) Car then owner is initialized. When i return House then Owner is initialized, but Car is not. I would like to be able to call something like House.getOwner().getCar().getId(). Now I must call find on House and then call find on Owner to get Car. How do I resolve this?
@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:抱歉映射有误,类运行良好.但是初始化问题仍然存在.
Sorry there was mistake in mapping, classes working well. But problem with initialization remains.
推荐答案
试试这个:
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 时,整个对象图将被填充.
When you fetch for Car entire object graph will be populated.
这篇关于Java JPA FetchType.EAGER 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java JPA FetchType.EAGER 不起作用
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01