在hibernate中加载相关对象

时间:2017-01-11 21:48:42

标签: java mysql hibernate proxy many-to-one

我想加载具有多对一字段类型的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;
}

但是我收到了一个错误:

  

线程“AWT-EventQueue-0”中的异常   org.hibernate.LazyInitializationException:无法初始化代理    - org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165)没有会话     在   org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286)     在   org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)     在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 个答案:

答案 0 :(得分:1)

这是一个不明确的问题,但我有两个猜测

  1. 您的findByHouseId函数正在关闭会话。
  2. 您在获取数据时未初始化已加入的实体。
  3. 我猜您的House实体包含Type实体,因此您可以在Type类上添加@Fetch(FetchMode.JOIN)。 (在某些情况下,它会复制值,因此您必须区分结果)