0

I have a project in Java Web Application and the plan is to output the functionallity with Glassfish to PHP (nuSOAP) .

I have created my BD in mySQL , all tables , relationships, Primary Keys and Foreign Keys.

After that i open my NetBeans, New Project -> Java Web APP.

The steps that i made are
first create the Hibernate Configuration Wizard, and then , create Hibernate Reverse Engineering Wizard then the Hibernate mapping files and POJO's from Database and at the end HibernateUtil (named connection)

My entities are: Club one for many Grade one for many Team one for many Player

So, now i have all my Entities in my Java Project. And created a DAO for Club:

public List<Club> listClubs(){
    try{
        session.beginTransaction();
        List<Club> listClub = (List<Club>)session.createQuery("from Club").list();
        session.getTransaction().commit();
        session.close();
        return listClub;
    }catch(Exception e){
        System.out.println("Erro ao listar os clubes"); 
        System.out.println(e.getMessage());
    }
    return null;
}

After that i want to create a WebService to output a list of All my Clubs.

@WebMethod(operationName = "listAllClubs")
public List<Club> listAllClubs() {
    ClubDAO pdo = new ClubDAO();
    List<Club> temp = pdo.listClubs();
    System.out.println("[WS:::]Tamanho do array devolvido: " + temp.size());
    return temp;
}

And after i tested my WebService in glassfish page i get this error:

> Service invocation threw an exception with message : null; Refer to the server log for more details

When i got the server log i see this:

> SEVERE: failed to lazily initialize a collection of role: pt.dai.entities.Club.grades, no session or session was closed

I already search about this error in internet, i already solved changing the xml of entity to default-lazy='false' but when this returns all club info to PHP my nuSOAP break up with a lot of DATA..

How can i use the lazzyLoad and send my data to WebService ?

Thanks for the help!

4

1 回答 1

0

Looks like this is not lazy loading problem.

Lazy loading allows you to not read nested entities from database while it wouldn't be addressed explicitly.

In your case even with lazy loading you'll try send all the data, so all this data will be read from the DB.

If you don't want to send all of it then you should limit your query. Otherwise you can split your data to a few parts and send them separately.


Edit: Remove relationships is the simplest but not satisfactory solution.

The root of your problem is in serialization. Looks like your entities has bidirectional or cyclical relationships. You need to resolve it by removing redundant relationships or marking all such class members transient which prohibits their serialization.

P.S. If you don't close your Hibernate session it won't be released and not returns to connection pool. After a few time your connection pool will be empty and all your future queries will hangs up while waiting for free session.

于 2013-03-30T23:46:03.333 回答