简体   繁体   English

主Hibernate会话自动关闭

[英]Main Hibernate Session Closing automatically

I have a parent and child entities as below 我有一个父母和子女实体如下

@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<>();
}

Child entity as below 子实体如下

@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;
}

I have configured Spring and Hibernate to process database operations, and to process Parent I have the following method added in the serviceimpl class. 我已经配置了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

                }
            }
        }
    }
}}

In the above method, I have the session is opened until I call child.getParent() but if I call the below method which has another session itself which is not null, after returning the below method, then the session in above method is becoming null automatically. 在上面的方法中,我打开会话直到我调用child.getParent()但是如果我调用下面的方法,其中有另一个会话本身不为null,在返回下面的方法之后,上面方法中的会话正在变为自动为null。

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;
    }
}}

Can anyone help me how to fix the main session should not be closed or become null. 任何人都可以帮助我如何修复主会话不应该被关闭或变为空。 I know this is a configuration problem, but I don't know what should be configured. 我知道这是一个配置问题,但我不知道应该配置什么。

Thanks in Advance, as I am trying to figure it out this issue for past 2 weeks. 在此先感谢,因为我试图在过去两周内弄清楚这个问题。

You can add @Transactional above method and transaction will close at the end of the method. 您可以在方法上添加@Transactional ,并且在方法结束时将关闭事务。 To enable @Transactional annotation you can add following code in your context-persistence.xml : 要启用@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>

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

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