【问题标题】:(org.hibernate.LazyInitializationException) org.hibernate.LazyInitializationException: could not initialize proxy - no Session(org.hibernate.LazyInitializationException) org.hibernate.LazyInitializationException: 无法初始化代理 - 没有会话
【发布时间】:2016-07-29 01:39:40
【问题描述】:

我开始学习 hibernate 以使用 Java 开发 Web 系统,但我在映射和使用类时遇到了一些问题。

我有两个表:tblusuario 和 tblperfilusuario,tblusuario 用 tblperfilusuario 制作外键,即用户连接个人资料。

使用 Postgres 数据库 + Netbeans 和 Hibernate 4.3.1

TblUsuario 实体:

    @Entity
@Table(name = "tblusuario", schema = "public")
@AttributeOverride(name = "id", column = @Column(name = "codigousuario"))
@SequenceGenerator(name = "id_sequence",
        sequenceName = "tblusuario_codigousuario_seq",
        allocationSize = 1)
public class TblUsuario extends BaseEntityInt {

    private TblPerfilUsuario tblperfilusuario;
    private String nome;
    private String usuario;
    private String senha;

    public TblUsuario() {
    }

    public TblUsuario(String nome, String usuario, String senha) {
        this.nome = nome;
        this.usuario = usuario;
        this.senha = senha;
    }

    public TblUsuario(TblPerfilUsuario tblperfilusuario, String nome, String usuario, String senha) {
        this.tblperfilusuario = tblperfilusuario;
        this.nome = nome;
        this.usuario = usuario;
        this.senha = senha;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "codigoperfilusuario")
    public TblPerfilUsuario getTblperfilusuario() {
        return this.tblperfilusuario;
    }

    public void setTblperfilusuario(TblPerfilUsuario tblperfilusuario) {
        this.tblperfilusuario = tblperfilusuario;
    }

    @Column(name = "nome", nullable = false, length = 100)
    public String getNome() {
        return this.nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    @Column(name = "usuario", nullable = false, length = 40)
    public String getUsuario() {
        return this.usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    @Column(name = "senha", nullable = false, length = 20)
    public String getSenha() {
        return this.senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

}

TblPerfilUsuario 实体:

    @Entity
@Table(name = "tblperfilusuario", schema = "public")
@AttributeOverride(name = "id", column = @Column(name = "codigoperfilusuario"))
@SequenceGenerator(name = "id_sequence",
        sequenceName = "tblperfilusuario_codigoperfilusuario_seq",
        allocationSize = 1)
public class TblPerfilUsuario extends BaseEntityInt {

    private String nome;
    private boolean ativo;
    private Set<TblUsuario> tblusuarios = new HashSet(0);

    public TblPerfilUsuario() {
    }

    public TblPerfilUsuario(String nome, boolean ativo) {
        this.nome = nome;
        this.ativo = ativo;
    }

    public TblPerfilUsuario(String nome, boolean ativo, Set<TblUsuario> tblusuarios) {
        this.nome = nome;
        this.ativo = ativo;
        this.tblusuarios = tblusuarios;
    }

    @Column(name = "nome", nullable = false, length = 100)
    public String getNome() {
        return this.nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    @Column(name = "ativo", nullable = false)
    public boolean isAtivo() {
        return this.ativo;
    }

    public void setAtivo(boolean ativo) {
        this.ativo = ativo;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "tblperfilusuario")
    public Set<TblUsuario> getTblusuarios() {
        return this.tblusuarios;
    }

    public void setTblusuarios(Set<TblUsuario> tblusuarios) {
        this.tblusuarios = tblusuarios;
    }

}

每次我尝试使用 TblUsuario.tblperfilusuario.getNome() 都会出现以下错误:

e = (org.hibernate.LazyInitializationException) org.hibernate.LazyInitializationException: 无法初始化代理 - 没有会话

任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: java hibernate postgresql


    【解决方案1】:

    在类 TblUsuario 中将注解 @ManyToOne(fetch = FetchType.LAZY) 更改为 @ManyToOne(fetch = FetchType.EAGER)。

    例如:

    public class TblUsuario extends BaseEntityInt { ... @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "codigoperfilusuario") public TblPerfilUsuario getTblperfilusuario() { return this.tblperfilusuario; } ...

    【讨论】:

      猜你喜欢
      • 2011-12-01
      • 2014-04-21
      • 2019-05-10
      • 2015-11-09
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      相关资源
      最近更新 更多