【发布时间】: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