Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you should not use the hibernate session transactional methods, but let spring do that.</p> <p>Add this to your spring conf:</p> <pre><code>&lt;bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;property name="sessionFactory" ref="mySessionFactory" /&gt; &lt;/bean&gt; &lt;bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"&gt; &lt;property name="transactionManager" ref="txManager"/&gt; &lt;/bean&gt; </code></pre> <p>and then I would modify your test method to use the spring transaction template:</p> <pre><code>public static void main(String[] args) { // init here (getting dao and transaction template) transactionTemplate.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { // do your hibernate stuff in here : call save, list method, etc } } } </code></pre> <p>as a side note, @OneToMany associations are lazy by default, so you don't need to annotate it lazy. (@*ToMany are LAZY by default, @*ToOne are EAGER by default)</p> <p>EDIT: here is now what is happening from hibernate point of view:</p> <ul> <li>open session (with transaction start)</li> <li>save a user and keep it in the session (see the session cache as an entity hashmap where the key is the entity id)</li> <li>save an event and keep it in the session</li> <li>save another event and keep it in the session</li> <li><p>... same with all the save operations ...</p></li> <li><p>then load all users (the "from Users" query)</p></li> <li>at that point hibernate see that it has already the object in its session, so discard the one it got from the request and return the one from the session.</li> <li>your user in the session does not have its event collection initialized, so you get null.</li> <li>...</li> </ul> <p>Here are some points to enhance your code:</p> <ul> <li>in your model, when collection ordering is not needed, use Set, not List for your collections (private Set events, not private List events)</li> <li>in your model, type your collections, otherwise hibernate won't which entity to fetch (private Set&lt;Event&gt; events)</li> <li>when you set one side of a bidirectional relation, and you wish to use the mappedBy side of the relation in the same transaction, set both sides. Hibernate will not do it for you before the next tx (when the session is a fresh view from the db state).</li> </ul> <p>So to address the point above, either do the save in one transaction, and the loading in another one :</p> <pre><code>public static void main(String[] args) { // init here (getting dao and transaction template) transactionTemplate.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { // save here } } transactionTemplate.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { // list here } } } </code></pre> <p>or set both sides:</p> <pre><code>... event1.setUser(user); ... event2.setUser(user); ... user.setEvents(Arrays.asList(event1,event2)); ... </code></pre> <p>(Also do not forget to address the code enhancement points above, Set not List, collection typing)</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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