gpt4 book ai didi

java - 在 hibernate 中加载相关对象

转载 作者:行者123 更新时间:2023-11-28 23:20:58 25 4
gpt4 key购买 nike

我想加载具有多对一字段类型的 House 对象:

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="type_id")
public Type getType() {
return type;
}

我现在想像这样访问它:

House temp = DataBaseConnector.getInstance().findHouseByID(id);
Type type = temp.getType();

findHouseById() 方法如下所示:

public static House findHouseByID(Integer id) {
Session session = getSessionFactory().openSession();
House e = (House) session.load(House.class, id);
session.close();
return e;
}

但是我得到了一个错误:

Exception in thread "AWT-EventQueue-0" org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) at mapping.House_$$_jvst2a0_3.getType(House_$$_jvst2a0_3.java)

类型实体:

package mapping;

import java.util.List;

import javax.persistence.*;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name="Type")
public class Type {

private int type_id;
private String name;
private List <Account> accounts;
private List <House> houses;

public Type(String name)
{
this.name = name;
}

public Type()
{

}

@Id
@GeneratedValue
@Column(name="id")
public int getType_id() {
return type_id;
}

public void setType_id(int type_id) {
this.type_id = type_id;
}
@Column(name="name")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
@Fetch(FetchMode.JOIN)
@OneToMany(mappedBy="type")
public List <Account> getAccounts() {
return accounts;
}

public void setAccounts(List <Account> accounts) {
this.accounts = accounts;
}
@Fetch(FetchMode.JOIN)
@OneToMany(mappedBy="type")
public List <House> getHouses() {
return houses;
}

public void setHouses(List <House> houses) {
this.houses = houses;
}

}

和房子

package mapping;

import java.util.*;
import javax.persistence.*;

@Entity
@Table(name="House")
public class House {
private int id;
private String name;
private int sq_metrage;
private String address;
private float price;
private String description;
private Account owner;
private State state;
private Type type;
private List<Reservation> reservations;
private List<Opinion> opinions;

public House(){

}

public House(String name, int sq_metrage, String address, float price, String description)
{
this.name = name;
this.sq_metrage = sq_metrage;
this.address = address;
this.price = price;
this.description = description;
}

@Id
@GeneratedValue
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="sq_metrage")
public int getSq_metrage() {
return sq_metrage;
}
public void setSq_metrage(int sq_metrage) {
this.sq_metrage = sq_metrage;
}
@Column(name="address")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name="price")
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Column(name="description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}

@ManyToOne (fetch=FetchType.EAGER)
@JoinColumn(name="owner_id")
public Account getOwner() {
return owner;
}
public void setOwner(Account owner) {
this.owner = owner;
}

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="state_id")
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="type_id")
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}

@OneToMany(mappedBy="house")
public List<Reservation> getReservations() {
return reservations;
}

public void setReservations(List<Reservation> reservations) {
this.reservations = reservations;
}

@OneToMany(mappedBy="house")
public List<Opinion> getOpinions() {
return opinions;
}

public void setOpinions(List<Opinion> opinions) {
this.opinions = opinions;
}

}

任何帮助将不胜感激:)

最佳答案

这是一个不清楚的问题,但我有两个猜测

  1. 您的 findByHouseId 函数正在关闭 session 。
  2. 您在获取数据时没有初始化您的连接实体。

我猜您的 House 实体包含 Type 实体,因此您可以在 Type 类上添加 @Fetch(FetchMode.JOIN)。 (在某些情况下,它会重复值,因此您必须区分结果)

关于java - 在 hibernate 中加载相关对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41601303/

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