gpt4 book ai didi

Java JPA FetchType.EAGER 不起作用

转载 作者:行者123 更新时间:2023-12-02 07:39:25 26 4
gpt4 key购买 nike

我正在使用 JPA 在 Java EE( Jersey )中制作一个应用程序,但我遇到了未初始化实体的问题。我有 3 个实体“汽车”、“车主”、“房屋”,其中汽车可以有多个车主,车主可以有多个房屋。当我返回(entityManager.find)汽车时,所有者被初始化。当我返回 House 时,Owner 已初始化,但 Car 未初始化。我希望能够调用诸如 House.getOwner().getCar().getId() 之类的东西。现在我必须调用 House 上的 find,然后调用 Owner 上的 find 来获取汽车。我该如何解决这个问题?

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

编辑1:抱歉,映射时出现错误,类运行良好。但初始化问题仍然存在。

最佳答案

试试这个:

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 时,整个对象图将被填充。

关于Java JPA FetchType.EAGER 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26560999/

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