2016-04-07 33 views
0

Java'da bir web sistemi geliştirmesi için hazırda bekletme hakkında bilgi almaya başladım, ancak bazı sorunların haritalanması ve sınıfların kullanımı.(org.hibernate.LazyInitializationException) org.hibernate.LazyInitializationException: proxy'yi başlatamadı - Oturum Yok

İki tablom var: tblusuario ve tblperfilusuario, tblusuario tblperfilusuario ile yabancı anahtar, yani, bir kullanıcı conté bir profil yapar.

Postgres veritabanı kullanarak + NetBeans ve 4.3.1

TblUsuario varlık kipi:

@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 varlık:

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

} 

Ben TblUsuario.tblperfilusuario kullanmayı deneyin her zaman. getNome() aşağıdaki hatayı aldı:

e = (org.hibernate.LazyInitializationException) org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Herhangi bir yardım için teşekkür ederiz. Teşekkür ederiz

cevap

0

@ManyToOne (fetch = FetchType.LAZY) ek açıklamalarını @ManyToOne (fetch = FetchType.EAGER) sınıfında TblUsuario olarak değiştirin.

Örnek:

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

İlgili konular