could not initialize proxy - no Session

UI Components for JSF
Locked
MFKprim
Posts: 10
Joined: 08 May 2013, 16:30

12 May 2013, 19:50

Hello everyone,
I'm working with hibernate 2.3.5 , jsf 2.0 and primeFaces 3.4 and I'm new at it I need your help please.
I'm using 2 tables "Destination" & "Client" that have a many-to-one relationship and I'm trying to display the data on a dataTable but I get this error.

Code: Select all

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
	at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
	at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
	at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
	at Beans.Destination$$EnhancerByCGLIB$$8c6f424e.getLibelle(<generated>)
Client.java

Code: Select all

package Beans;

import java.util.HashSet;
import java.util.Set;

public class Client  implements java.io.Serializable {

     private long idClient;
     private Destination destination;
     private String raisonSociale;
     private String adresse;
     private String commentaire;

    public Client() {
    }

	
    public Client(long idClient) {
        this.idClient = idClient;
    }
    public Client(long idClient, Destination destination, String raisonSociale, String adresse, String commentaire, Set bonlivraisons, Set stations) {
       this.idClient = idClient;
       this.destination = destination;
       this.raisonSociale = raisonSociale;
       this.adresse = adresse;
       this.commentaire = commentaire;
       this.bonlivraisons = bonlivraisons;
       this.stations = stations;
    }
   
    public long getIdClient() {
        return this.idClient;
    }
    
    public void setIdClient(long idClient) {
        this.idClient = idClient;
    }
    public Destination getDestination() {
        return this.destination;
    }
    
    public void setDestination(Destination destination) {
        this.destination = destination;
    }
    public String getRaisonSociale() {
        return this.raisonSociale;
    }
    
    public void setRaisonSociale(String raisonSociale) {
        this.raisonSociale = raisonSociale;
    }
    public String getAdresse() {
        return this.adresse;
    }
    
    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }
    public String getCommentaire() {
        return this.commentaire;
    }
    
    public void setCommentaire(String commentaire) {
        this.commentaire = commentaire;
    }
 
}
Destination.java

Code: Select all

package Beans;

import java.util.HashSet;
import java.util.Set;

public class Destination  implements java.io.Serializable {


     private Long idDestination;
     private String libelle;

    public Destination() {
    }

    public Destination(String libelle, Set clients, Set stations, Set prixes) {
       this.libelle = libelle;
       this.clients = clients;
    }
   
    public Long getIdDestination() {
        return this.idDestination;
    }
    
    public void setIdDestination(Long idDestination) {
        this.idDestination = idDestination;
    }
    public String getLibelle() {
        return this.libelle;
    }
    
    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }
    public Set<Client> getClients() {
        return this.clients;
    }
    
    public void setClients(Set<Client> clients) {
        this.clients = clients;
    }
}
The view xhtml

Code: Select all

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Gestion des Clients</title>
        <link rel="stylesheet" href="css/style.css "/>
    </h:head>
    <h:body style="font-size:85% !important;
            background-color: #DB0F1B;
            background-image:url(images/background13.jpg);
            background-repeat:no-repeat;
            background-position: center top;
            min-width:100px; min-height:600px;">
        <div id="global"  >
            <h:messages style="display: none;" /> 
            <h:form>
                <div id="header" >
                    <h:messages style="display: none;" /> 
                </div>
            </h:form>
            
            <h:messages style="display: none;" /> 

            <center>
                <h:form id="a">

                    <p:growl id="growl" showDetail="true" sticky="false" />
                    <p:dataTable var="client" value="#{clientController.listaClient}" id="AjoutTab" widgetVar="PersonneTable"
                                 emptyMessage="Client non trouvé" paginator="true" rows="6" paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
                                 rowsPerPageTemplate="6,8,10" style="width:800px;font-size:13px;margin-top: 8%;"> 


                        <p:column id="id" headerText="ID" filterBy="#{client.idClient}" filterMatchMode="exact" footerText=" ID exacte">  
                            <h:outputText value="#{client.idClient}" />  
                        </p:column>  
                        <p:column headerText="Raison Social" id="raisonSociale" filterBy="#{client.raisonSociale}" filterMatchMode="exact" footerText="nom exacte">  
                            <h:outputText value="#{client.raisonSociale}" />  
                        </p:column> 
                        <p:column headerText="Adresse" id="adresse" filterBy="#{client.adresse}" filterMatchMode="contains" footerText="contient">  
                            <h:outputText value="#{client.adresse}" />  
                        </p:column>  
                        <p:column headerText="Commentaire" id="commentaire" filterBy="#{client.commentaire}" filterMatchMode="contains" footerText="contient">  
                            <h:outputText value="#{client.commentaire}" />  
                        </p:column> 

                        <p:column headerText="Destination" id="destination" filterBy="#{client.destination.libelle}" filterMatchMode="contains" footerText="contient">  
                            <h:outputText value="#{client.destination.libelle}" />  
                        </p:column> 
                    </p:dataTable> 
                </h:form>
            </center>
            <br/>
        </div>
    </h:body>
</html>
ManagedBean ClientController

Code: Select all

package Controller;

import Dao.ClientDao;
import Dao.ClientDaoImp;
import Dao.DestinationDaoImp;
import Beans.Client;
import Beans.Destination;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

@ManagedBean
@SessionScoped
public class ClientController implements Serializable {

    private Client client;
    private DataModel<Client> listaClient;

    public ClientController(){
        client= new Client();
    }
   
    public DataModel<Client> getListaClient() {
        ClientDao dao=new ClientDaoImp();
        List<Client> lista = dao.list();
        listaClient = new ListDataModel<Client>(lista);
        return listaClient;
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }

}
ClientDaoImp

Code: Select all

package Dao;

import Beans.Client;
import Utils.HibernateUtil;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ClientDaoImp implements ClientDao {

    @Override
    public List<Client> list() {
        try{
        /Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        List lista = session.createQuery("from Client").list();
        t.commit();
        session.close();        
         return lista;
        }
        catch(Exception e){
            System.err.println("erreur list"+e.getMessage());
            session.close();
            return null;
            
        }   
    }


}
Thank you in advance!

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

12 May 2013, 20:48

Sorry but this is not related to primefaces.
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

MFKprim
Posts: 10
Joined: 08 May 2013, 16:30

13 May 2013, 01:55

Yeah ouppss! I really didn't pay attention! Sorry! :D I'll remove it! ^_^

Locked

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: Google [Bot] and 29 guests