Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate merge() with versioning
    primarykey
    data
    text
    <p>I am using hibernate 3.6, mappings in XML. </p> <p>Starting off with the following schema. </p> <pre><code>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&lt;Card&gt; cards = new HashSet&lt;Card&gt;();//cascade="all-delete-orphan" ... } </code></pre> <p>If I do the following : <br>1: Load an existing user <br>2: Close session , work in detached state client side. Add transient tags. <br>3: Return the user to server, openSession(), beginTransaction(), saveOrUpdate(user), commit().</p> <p>I get the following error "Error while indexing in Hibernate Search (before transaction completion)" ... Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session</p> <p>So far this makes sense to me. CardType &amp; Card need to have their indexes updated. So I expect to change my step 3 to merge() before saveOrUpdate(). </p> <p>If I do this it copies all properties from detached , including version, into the session aware object. This of course means my optimistic locking strategy fails - not warned of version problems. </p> <p>What is the strategy supposed to be in this situation?</p> <p>--Post updated to show some session handling code--</p> <pre><code>public synchronized static SessionFactory getSessionFactory() { if (sessionFactory == null) { final AuditLogInterceptor interceptor = new AuditLogInterceptor(); Configuration configuration = new Configuration(); configuration = configuration.configure("hibernate.cfg.xml"); configuration.setInterceptor(interceptor); sessionFactory = configuration.buildSessionFactory(); AbstractSessionAwareConstraintValidator.setSessionFactory(sessionFactory); } return sessionFactory; } </code></pre> <p>Test code something like this</p> <pre><code>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(); </code></pre> <p>As far as Im aware there is nothing "non standard" in my hibernate.cfg.xml, but just listing these 3 lines in case 1 thread org.hibernate.cache.NoCacheProvider</p> <p>I hope thats enough code to demonstrate session usage. Having posted this, Im wondering if its possible that the interceptor could affect session management?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload