【问题标题】:Main Hibernate Session Closing automatically主休眠会话自动关闭
【发布时间】:2017-04-20 16:54:09
【问题描述】:

我有一个父子实体如下

@Entity
@Table(name = "PARENT")
public class Parent implements Comparable<Parent>, Serializable, Cloneable {

    private static final long serialVersionUID = 1584115734363172986L;

    public static final int NAME_LENGTH = 300;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "gen-seq")
    @GenericGenerator(name = "gen-seq", strategy = "native",
        parameters = {@Parameter(name = "sequence_name", value = "parent_id_seq")})
    private Long id;

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    @BatchSize(size = 50)
    @SortNatural
    private SortedSet<Child> child = new TreeSet<>();
}

子实体如下

@Entity
@Table(name = "Child")
@Inheritance(strategy= InheritanceType.JOINED)
public class Child implements Comparable<Object>, Serializable, Cloneable, Step<Child> {

    private static final long serialVersionUID = -9091312680432977997L;

    public static final int NAME_LENGTH = 255;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "gen-seq")
    @GenericGenerator(name = "gen-seq", strategy = "native",
        parameters = {@Parameter(name = "sequence_name", value = "child_id_seq")})
    private Long id;

    @ManyToOne()
    @JoinColumn(name = "parentId", nullable = false)
    private Parent parent;
}

我已经配置了 Spring 和 Hibernate 来处理数据库操作,并且为了处理 Parent 我在 serviceimpl 类中添加了以下方法。

public void processParent(final Long parentId, String userEmail) throws Exception {
getHibernateTemplate().execute(new HibernateCallback<Set<Void>>() {
    public void doInHibernate(Session session) throws HibernateException    {
        Parent parent = (Parent) session.get(Parent.class, parentId);           
        if (parent!=null && parent.getChild()!=null) {
            for (Child child: parent.getChild()) {

                // here the session is still Open when I call
                child.getParent();

                //But when I call this
                Parent p = getParent(child.getParent().getId());

                //now here the session got closed automatically, and now I cannot process the parent object

                }
            }
        }
    }
}}

在上面的方法中,我已经打开了会话,直到我调用 child.getParent() 但是如果我调用下面的方法,它有另一个会话本身不为空,在返回下面的方法之后,然后上面的会话方法自动变为空。

public Parent getParent(final Long id) throws Exception {
Parent actionPoint = (Parent) getHibernateTemplate().execute(new HibernateCallback<Parent>() {

    /* (non-Javadoc)
    * @see org.springframework.orm.hibernate5.HibernateCallback#doInHibernate(org.hibernate.Session)
    */
    public Parent doInHibernate(Session session) throws HibernateException {
        Parent parent = (ActionPoint) session.get(Parent.class, id);

        //here also session is still open
        return parent;
    }
}}

任何人都可以帮助我如何修复主会话不应该关闭或变为空。我知道这是一个配置问题,但我不知道应该配置什么。

在此先感谢,因为我在过去 2 周内一直在努力解决这个问题。

【问题讨论】:

  • 只需引入 Spring TransactionManager 并使用 @Transactional 注释您的 Service 方法,以避免手动会话/事务处理。如果您将传播设置为 REQUIRED,则将使用相同的事务。
  • 如果您使用的是 JTA - 将 Hibernate.cfg 中的属性“current_session_context_class”更改为“managed”
  • @PRATHAPS 这个问题已经在一定程度上解决了,现在我没有收到任何会话异常,即会话已打开。但是当我调用其他方法 Parent parent = (Parent) getHibernateTemplate().get(Parent.class, parentId); if(parent != null) { Hibernate.initialize(parent.getChildren()); } } 时,我在 Hibernate.initialize(parent.getChildren()); 没有收到会话错误;

标签: java spring hibernate session lazy-initialization


【解决方案1】:

您可以在上面的方法中添加@Transactional,事务将在方法结束时关闭。要启用@Transactional 注释,您可以在context-persistence.xml 中添加以下代码:

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Session Factory -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"/> -->
    <property name="hibernateProperties">
        <map>
            <entry key="hibernate.default_schema" value="${database.default_schema}" />
            <entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}" />
            <entry key="show_sql" value="true" />
            <entry key="hibernate.dialect" value="${hibernate.dialect}" />
            <entry key="transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory" />
        </map>
    </property>
</bean>

【讨论】:

  • 谢谢伙计..它帮助我解决了 transaction.factory_class 的问题
猜你喜欢
  • 2012-03-10
  • 2011-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 2011-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多