2

我正在使用 JPA (Hibernate),当我尝试从数据库中获取对象时遇到问题。我正在使用 Glassfish 3.1.2、Hibernate 和 Oracle 数据库 10.2.0.4。

这是我的应用程序的概念:

答案是对表格的答案。一个表单属于一个类别。用户可以创建表单。用户可以回答。

所以:

  • 1 个表格有一个类别,所以 1 个类别可能有多个表格
  • 1 个答案有 1 个表格,1 个表格可能有多个答案
  • 1 个回答有 1 个用户(回答作者)
  • 1 个表单有 1 个用户(表单创建者)

如果我试图得到答案,我会得到一个错误(见帖子底部),但奇怪的是:

  • 如果数据库中没有答案;它有效(至少没有抛出错误)

  • 如果答案存在于数据库中且 answer.form_id 列中没有任何值;它有效(至少没有抛出错误)

  • 如果我从答案类中删除表单属性并让用户属性;有用!答案是从数据库中获取的,并且还获取了用户。

  • 如果我从答案类中删除用户属性并让表单属性;它失败!答案不是从数据库中得到的。(在本文末尾抛出错误)

  • 如果我从表单类中删除类别和用户属性;有用!答案是从数据库中获取的,并且还获取了一个用户和一个表单!但是我不能这样,我需要在表单实体中有一个类别和一个用户。

  • 如果我删除任一类别属性或任一用户属性,并让另一个在表单类中;它失败。

似乎 hibernate 无法获取比其他实体更远的信息:

  • 回答 > 用户:好的
  • 答案 > 没有类别或用户的表单:OK
  • 表格 > 类别:OK
  • 表单 > 用户:好的
  • 答案 > 表格 > 类别:失败
  • 答案 > 表格 > 用户:失败

但是我<property name="hibernate.max_fetch_depth" value="3"/>在我的persistence.xml 中设置。

有什么问题的任何想法或建议?它似乎是表单实体或其属性类别或用户。

谢谢


这是我的实际代码,为了清楚起见,大大减少了。

表格类:

@Entity
@Table(name="FORM")
public class Form implements Serializable {

    private static final long serialVersionUID = -2101681231828548611L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "G_FORM")
    @SequenceGenerator(name = "G_FORM", sequenceName = "FORM_ID_SEQ")
    @Column(name = "ID", unique = true, nullable = false)
    private int id;

    private String name;

    @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER) @JoinColumn
    private Category category;

    @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER) @JoinColumn
    private User user;

    //more fields and usual getters and setters
}

用户等级:

@Entity
@Table(name = "USERS")
public class User implements Serializable {

    private static final long serialVersionUID = -821421797110076396L;

    @Id
    @Column(name = "ID")
    private String id;

    @NotNull
    @Column
    private String email;

    @NotNull
    private String password;

    @Column(name="PARENT_ID")
    private User parent;

    //more fields and usual getters and setters
}

答题类:

@Entity
public class Answer implements Serializable {

    private static final long serialVersionUID = -8514559401482729639L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "G_ANSWER")
    @SequenceGenerator(name = "G_ANSWER", sequenceName = "ANSWER_ID_SEQ")
    @Column(name = "ID", unique = true, nullable = false)
    private int id;

    @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER) @JoinColumn
    private Form form;

    @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER) @JoinColumn
    private User user;

    //more fields and usual getters and setters
}

AnswerDAO 方法失败:

public List<Answer> getAllAnswers() throws Exception{
    TypedQuery<Answer> query = em.createQuery( "SELECT a FROM Answer a ORDER BY a.id", Answer.class );
    return query.getResultList();
    //return em.createQuery("SELECT a FROM Answer a", Answer.class).getResultList();
}

在我的 persistence.xml 中:

<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.max_fetch_depth" value="3"/>  

hibernate生成的sql(似乎正确?):

Infos: Hibernate: select answer0_.ID as ID1_0_, answer0_.ANSWER_XML as ANSWER2_0_, answer0_.DATE_UPLOAD as DATE3_0_, answer0_.form_ID as form4_0_, answer0_.user_ID as user5_0_ from Answer answer0_ order by answer0_.ID
Infos: Hibernate: select form0_.ID as ID1_2_2_, form0_.category_NAME as category6_2_2_, form0_.DATE_UPLOAD as DATE2_2_2_, form0_.extention as extentio3_2_2_, form0_.name as name4_2_2_, form0_.QUESTION_XML as QUESTION5_2_2_, form0_.user_ID as user7_2_2_, category1_.NAME as NAME1_1_0_, category1_.DATE_CREATION as DATE2_1_0_, user2_.ID as ID1_5_1_, user2_.ANSWER_COUNT as ANSWER2_5_1_, user2_.DATE_INSCRIPTION as DATE3_5_1_, user2_.DATE_LAST_ANSWER as DATE4_5_1_, user2_.DATE_LAST_LOGIN as DATE5_5_1_, user2_.email as email6_5_1_, user2_.firstname as firstnam7_5_1_, user2_.FORM_COUNT as FORM8_5_1_, user2_.lastname as lastname9_5_1_, user2_.PARENT_ID as PARENT10_5_1_, user2_.password as passwor11_5_1_ from FORM form0_ left outer join Category category1_ on form0_.category_NAME=category1_.NAME left outer join USERS user2_ on form0_.user_ID=user2_.ID where form0_.ID=?

Eclipse 控制台中的错误也大大减少了:

WARN: SQL Error: 17027, SQLState: null
ERROR: Le flux de données est déjà fermé
Infos: HHH000327: Error performing load command : org.hibernate.exception.GenericJDBCException: could not load an entity: [be.adehis.bean.Form#96]
Avertissement: EJB5184:A system exception occurred during an invocation on EJB AnswerDAO, method: public java.util.List be.adehis.database.dao.AnswerDAO.getAllAnswers() throws java.lang.Exception
Avertissement: javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean
    at ...
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not load an entity: [be.adehis.bean.Form#96]
    at ... 
Caused by: org.hibernate.exception.GenericJDBCException: could not load an entity: [be.adehis.bean.Form#96]
    at ...
Caused by: java.sql.SQLException: Le flux de données est déjà fermé
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)

Avertissement: EJB5184:A system exception occurred during an invocation on EJB AnswerService, method: public java.util.List be.adehis.service.answer.AnswerService.getAllAnswers() throws java.lang.Exception
Avertissement: javax.ejb.EJBTransactionRolledbackException
    at ...
Caused by: javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean
    at ...
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not load an entity: [be.adehis.bean.Form#96]
    at ...
Caused by: org.hibernate.exception.GenericJDBCException: could not load an entity: [be.adehis.bean.Form#96]
    at ...
Caused by: java.sql.SQLException: Le flux de données est déjà fermé
4

3 回答 3

1

我发现了问题:

这篇文章说:

通过JDBC查询Oracle时,如果正在查询的表中包含Long或Long Raw数据,可能会抛出如下错误信息:

java.sql.SQLException:流已经关闭

该表单有一个属性 questionXml,其中包含格式为 xml 的问题。答案有一个属性 answerXml 包含格式为 xml 的答案。两者都保存在数据库中LONG。(我的错还是hibernate的错,我没有)

我更改了列的类型,VARCHAR2(255CHAR)现在它正在工作!

对我来说奇怪的是hibernate能够获取答案>表单或表单>用户,但不能获取答案>表单>用户。

无论如何将 LONG 更改为 VARCHAR2 并且它正在工作。

于 2013-05-16T13:11:09.667 回答
0

也许您可以在 Many - One 上尝试 @NotFound( action = NotFoundAction.IGNORE ) 看看是否可以解决问题。我没有尝试过,但有时损坏的数据可能会产生问题。

于 2013-05-15T15:27:15.673 回答
0

我不是 Hibernate 方面的专家,但看起来您的数据库连接在查询完成之前已关闭:

Le flux de données est déjà fermé => The data stream is already closed.

¿ 任何其他可以关闭它的线程?或者,由于泄漏检测,您的服务器可能在不吉利的时候回收它们。看看这个,看看调整这些设置是否能让你朝着正确的方向前进。

于 2013-05-15T15:39:59.623 回答