具有版本控制的Hibernate merge()
我正在使用Hibernate3.6,XML中的映射 从以下模式开始具有版本控制的Hibernate merge(),hibernate,hibernate-search,Hibernate,Hibernate Search,我正在使用Hibernate3.6,XML中的映射 从以下模式开始 public class Card { @IndexedEmbedded private CardType cardType; private User user;//many-to-one ... } public class User { ... private int version;//Need to be strict about version on this table privat
public class Card {
@IndexedEmbedded
private CardType cardType;
private User user;//many-to-one
...
}
public class User {
...
private int version;//Need to be strict about version on this table
private Set<Card> cards = new HashSet<Card>();//cascade="all-delete-orphan"
...
}
测试代码如下
sessionFactory = HibernateUtil.getSessionFactory();
sessionFactory.getCurrentSession().beginTransaction();
//Find user here
sessionFactory.getCurrentSession().getTransaction().commit();
sessionFactory.getCurrentSession().close();
//Edit User, add tags out of session. (not using OSIV)
sessionFactory.getCurrentSession().beginTransaction();
user = sessionFactory.getCurrentSession().merge();//Only works if I do this
sessionFactory.getCurrentSession().saveOrUpdate(entity);
sessionFactory.getCurrentSession().getTransaction().commit();
sessionFactory.getCurrentSession().close();
据我所知,我的hibernate.cfg.xml中没有“非标准”的内容,只是列出了这3行以防万一
1.
线
org.hibernate.cache.NoCacheProvider
我希望有足够的代码来演示会话的使用。发布此消息后,我想知道拦截器是否可能影响会话管理?您如何处理会话?在索引期间,会话必须仍然打开,以便索引器可以加载延迟加载的关联。在webapps中,通常使用视图中的opensession模式在整个请求期间拥有一个opensession。您需要发布一些更具体的代码以获得更具体的反馈。另一种方法是立即加载关联(可能不是一个好主意)。为什么会失败?您希望复制版本属性。这正是保证刷新将抛出一个异常,如果版本已被另一个用户修改。我不觉得LazyInitializationException正常。。。我怀疑交易设置有问题,你当然是对的。我不知道我在想什么。所以问题仍然是,合并是正确的做法,还是像阿德里安建议的那样,我得到LazyInitException还有其他原因?在新会话完成之前,Hibernate Search是否应该在该会话中执行它需要的操作?对不起,我没有Hibernate Search的任何经验。另请参阅
sessionFactory = HibernateUtil.getSessionFactory();
sessionFactory.getCurrentSession().beginTransaction();
//Find user here
sessionFactory.getCurrentSession().getTransaction().commit();
sessionFactory.getCurrentSession().close();
//Edit User, add tags out of session. (not using OSIV)
sessionFactory.getCurrentSession().beginTransaction();
user = sessionFactory.getCurrentSession().merge();//Only works if I do this
sessionFactory.getCurrentSession().saveOrUpdate(entity);
sessionFactory.getCurrentSession().getTransaction().commit();
sessionFactory.getCurrentSession().close();