2017-04-20 35 views
1

我有一個家長和孩子實體如下主要Hibernate的Session自動關閉

@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來處理數據庫操作,以及處理父我在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(),但如果我叫具有另一個會話本身,其不爲空,返回下面的方法後,則該方法在下面上述方法中的會話自動變爲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; 
    } 
}} 

誰能幫助我如何解決不應該被關閉或成爲空的主要會議。我知道這是一個配置問題,但我不知道應該配置什麼。

在此先感謝,因爲我試圖找出過去2周的這個問題。

+0

剛剛推出春季事務管理和註釋與'@ Transactional'避免手動會話/事務處理您的服務方法。如果您將傳播設置爲REQUIRED,則將使用相同的事務。 – StanislavL

+0

如果你正在使用JTA--將Hibernate.cfg「current_session_context_class」中的屬性更改爲「managed」 –

+0

@PRATHAPS,問題已在某種程度上得到解決,現在我沒有得到會話異常,即會話已打開。但是當我調用其他方法\t'父親=(父)getHibernateTemplate()。get(Parent.class,parentId); (parent!= null){ \t \t if(parent!= null)Hibernate.initialize(parent.getChildren());我在Hibernate.initialize(parent.getChildren())沒有得到會話錯誤; – user503285

回答

0

您可以在上面的方法中添加@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> 
+0

感謝Man..it幫助我解決了transaction.factory_class問題 – user503285