简体   繁体   English

休眠和Glassfish延迟加载

[英]Hibernate and Glassfish Lazyload

I have a project in Java Web Application and the plan is to output the functionallity with Glassfish to PHP (nuSOAP) . 我在Java Web Application中有一个项目,计划是将Glassfish的功能输出到PHP(nuSOAP)。

I have created my BD in mySQL , all tables , relationships, Primary Keys and Foreign Keys. 我已经在mySQL,所有表,关系,主键和外键中创建了BD。

After that i open my NetBeans, New Project -> Java Web APP. 之后,我打开我的NetBeans,新建项目-> 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) 首先创建Hibernate配置向导,然后创建Hibernate逆向工程向导,然后从数据库以及最后的HibernateUtil(命名连接)中创建Hibernate映射文件和POJO。

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. 所以,现在我所有的实体都放在我的Java项目中。 And created a DAO for Club: 并为俱乐部创建了一个DAO:

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. 之后,我想创建一个WebService来输出我所有俱乐部的列表。

@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: 当我在glassfish页面中测试了我的WebService之后,出现此错误:

> 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.. 我已经在互联网上搜索了此错误,我已经解决了将实体的xml更改为default-lazy ='false'的问题,但是当这将所有俱乐部信息返回给PHP时,我的nuSOAP会分解大量数据。

How can i use the lazzyLoad and send my data to WebService ? 如何使用lazzyLoad并将数据发送到WebService?

Thanks for the help! 谢谢您的帮助!

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. 您需要通过删除冗余关系或将所有此类成员标记为瞬态来禁止其序列化来解决此问题。

PS If you don't close your Hibernate session it won't be released and not returns to connection pool. PS:如果不关闭您的Hibernate会话,它将不会被释放,也不会返回连接池。 After a few time your connection pool will be empty and all your future queries will hangs up while waiting for free session. 一段时间后,您的连接池将为空,并且在等待免费会话时,所有将来的查询都将挂断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM