<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> <base href="https://hibernate.atlassian.net" /> 
        <title>Message Title</title> 
    </head> 
    <body class="jira" style="color: #333; font-family: Arial, sans-serif; font-size: 14px; line-height: 1.429"> 
        <table id="background-table" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: #f5f5f5; border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt"> 
            <!-- header here --> 
            <tr> 
                <td id="header-pattern-container" style="padding: 0px; border-collapse: collapse; padding: 10px 20px"> 
                    <table id="header-pattern" cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt"> 
                        <tr> 
                            <td id="header-avatar-image-container" valign="top" style="padding: 0px; border-collapse: collapse; vertical-align: top; width: 32px; padding-right: 8px"> <img id="header-avatar-image" class="image_fix" src="https://secure.gravatar.com/avatar/651482feeb0f526d380aa43a179e4c17?d=mm&amp;s=48" height="32" width="32" border="0" style="border-radius: 3px; vertical-align: top" /> 
                            </td> 
                            <td id="header-text-container" valign="middle" style="padding: 0px; border-collapse: collapse; vertical-align: middle; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 1px"> <a class="user-hover" rel="katmai2000" id="email_katmai2000" href="https://hibernate.atlassian.net/secure/ViewProfile.jspa?name=katmai2000" style="color:#6c797f;; color: #3b73af; text-decoration: none">Stephan van Hugten</a> <strong>commented</strong> on <a href="https://hibernate.atlassian.net/browse/HHH-7457" style="color: #3b73af; text-decoration: none"><img src="https://hibernate.atlassian.net/images/icons/issuetypes/newfeature.png" height="16" width="16" border="0" align="absmiddle" alt="New Feature" /> HHH-7457</a> 
                            </td> 
                        </tr> 
                    </table> 
                </td> 
            </tr> 
            <tr> 
                <td id="email-content-container" style="padding: 0px; border-collapse: collapse; padding: 0 20px"> 
                    <table id="email-content-table" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-spacing: 0; border-collapse: separate"> 
                        <tr> 
                            <!-- there needs to be content in the cell for it to render in some clients --> 
                            <td class="email-content-rounded-top mobile-expand" style="padding: 0px; border-collapse: collapse; color: #fff; padding: 0 15px 0 16px; height: 15px; background-color: #fff; border-left: 1px solid #ccc; border-top: 1px solid #ccc; border-right: 1px solid #ccc; border-bottom: 0; border-top-right-radius: 5px; border-top-left-radius: 5px; height: 10px; line-height: 10px; padding: 0 15px 0 16px; mso-line-height-rule: exactly">
                                &nbsp;
                            </td> 
                        </tr> 
                        <tr> 
                            <td class="email-content-main mobile-expand " style="padding: 0px; border-collapse: collapse; border-left: 1px solid #ccc; border-right: 1px solid #ccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #fff"> 
                                <table class="page-title-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt"> 
                                    <tr> 
                                        <td style="vertical-align: top;; padding: 0px; border-collapse: collapse; padding-right: 5px; font-size: 20px; line-height: 30px; mso-line-height-rule: exactly" class="page-title-pattern-header-container"> <span class="page-title-pattern-header" style="font-family: Arial, sans-serif; padding: 0; font-size: 20px; line-height: 30px; mso-text-raise: 2px; mso-line-height-rule: exactly; vertical-align: middle"> <a href="https://hibernate.atlassian.net/browse/HHH-7457" style="color: #3b73af; text-decoration: none">Re: Enable loading of collections thats been detached from session</a> </span> 
                                        </td> 
                                    </tr> 
                                </table> 
                            </td> 
                        </tr> 
                        <tr> 
                            <td id="text-paragraph-pattern-top" class="email-content-main mobile-expand  comment-top-pattern" style="padding: 0px; border-collapse: collapse; border-left: 1px solid #ccc; border-right: 1px solid #ccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #fff; border-bottom: none; padding-bottom: 0"> 
                                <table class="text-paragraph-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 2px"> 
                                    <tr> 
                                        <td class="text-paragraph-pattern-container mobile-resize-text " style="padding: 0px; border-collapse: collapse; padding: 0 0 10px 0"> 
                                            <p style="margin: 10px 0 0 0">I'm wondering why this isn't done via the logic in AbstractPersistentCollection:isConnectedToSession()?<br /> So instead of:</p> 
                                            <div class="code panel" style="border-style: solid;border-width: 1px;; border: 1px solid #ccc; background: #f5f5f5; font-size: 12px; line-height: 1.333; font-family: monospace; border: 1px solid #ccc; -moz-border-radius: 3px 3px 3px 3px; border-radius: 3px 3px 3px 3px; margin: 9px 0">
                                                <div class="codeHeader panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;; border-bottom: 1px solid #ccc; padding: 9px 12px">
                                                    <b>AbstractPersistentCollection.java</b>
                                                </div>
                                                <div class="codeContent panelContent" style="padding: 9px 12px"> 
                                                    <pre class="code-java" style="margin: 10px 0 0 0; max-height: 30em; overflow: auto; white-space: pre-wrap; word-wrap: normal">
<span class="code-keyword" style="color: #000091">private</span> &lt;T&gt; T withTemporarySessionIfNeeded(LazyInitializationWork&lt;T&gt; lazyInitializationWork) {
...
        <span class="code-keyword" style="color: #000091">if</span> ( session == <span class="code-keyword" style="color: #000091">null</span> ) {
            <span class="code-keyword" style="color: #000091">if</span> ( specjLazyLoad ) {
                session = openTemporarySessionForLoading();
                isTempSession = <span class="code-keyword" style="color: #000091">true</span>;
            }
            <span class="code-keyword" style="color: #000091">else</span> {
                throwLazyInitializationException( <span class="code-quote" style="color: #009100">&quot;could not initialize proxy - no Session&quot;</span> );
            }
        }
...
}
</pre> 
                                                </div>
                                            </div> 
                                            <p style="margin: 10px 0 0 0">use:</p> 
                                            <div class="code panel" style="border-style: solid;border-width: 1px;; border: 1px solid #ccc; background: #f5f5f5; font-size: 12px; line-height: 1.333; font-family: monospace; border: 1px solid #ccc; -moz-border-radius: 3px 3px 3px 3px; border-radius: 3px 3px 3px 3px; margin: 9px 0">
                                                <div class="codeHeader panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;; border-bottom: 1px solid #ccc; padding: 9px 12px">
                                                    <b>AbstractPersistentCollection.java</b>
                                                </div>
                                                <div class="codeContent panelContent" style="padding: 9px 12px"> 
                                                    <pre class="code-java" style="margin: 10px 0 0 0; max-height: 30em; overflow: auto; white-space: pre-wrap; word-wrap: normal">
<span class="code-keyword" style="color: #000091">private</span> &lt;T&gt; T withTemporarySessionIfNeeded(LazyInitializationWork&lt;T&gt; lazyInitializationWork) {
...
        <span class="code-keyword" style="color: #000091">if</span> ( !isConnectedToSession() ) {
            <span class="code-keyword" style="color: #000091">if</span> ( specjLazyLoad ) {
                session = openTemporarySessionForLoading();
                isTempSession = <span class="code-keyword" style="color: #000091">true</span>;
            }
            <span class="code-keyword" style="color: #000091">else</span> {
                throwLazyInitializationException( <span class="code-quote" style="color: #009100">&quot;could not initialize proxy - no Session&quot;</span> );
            }
        }
...
}
</pre> 
                                                </div>
                                            </div> 
                                            <p style="margin: 10px 0 0 0">I'm also wondering why you're opening a new session while the Session Factory may still have a current session? This might cause <a href="https://hibernate.atlassian.net/browse/HHH-7524" title="Enabling AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS results in leaking DB-connections" class="issue-link" data-issue-key="HHH-7524" style="color: #3b73af; text-decoration: none"></a></p>
                                            <del>
                                                HHH-7524
                                            </del>. 
                                            <p style="margin: 10px 0 0 0">In the code I also see that you're only adding the actual collection to a new session with </p> 
                                            <div class="code panel" style="border-style: solid;border-width: 1px;; border: 1px solid #ccc; background: #f5f5f5; font-size: 12px; line-height: 1.333; font-family: monospace; border: 1px solid #ccc; -moz-border-radius: 3px 3px 3px 3px; border-radius: 3px 3px 3px 3px; margin: 9px 0">
                                                <div class="codeHeader panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;; border-bottom: 1px solid #ccc; padding: 9px 12px">
                                                    <b>AbstractPersistentCollection.java</b>
                                                </div>
                                                <div class="codeContent panelContent" style="padding: 9px 12px"> 
                                                    <pre class="code-java" style="margin: 10px 0 0 0; max-height: 30em; overflow: auto; white-space: pre-wrap; word-wrap: normal">
<span class="code-keyword" style="color: #000091">private</span> &lt;T&gt; T withTemporarySessionIfNeeded(LazyInitializationWork&lt;T&gt; lazyInitializationWork) {
...
session.getPersistenceContext().addUninitializedDetachedCollection(
                    session.getFactory().getCollectionPersister( getRole() ),
                    <span class="code-keyword" style="color: #000091">this</span>
            );
...
}
</pre> 
                                                </div>
                                            </div> 
                                            <p style="margin: 10px 0 0 0">What if it is a child collection? When I wrote my own patch for this problem (Guilty!) I got problems with the fact that a child collection added this way without its owner would be marked as dereferenced and deleted( ! ).</p> 
                                            <p style="margin: 10px 0 0 0">To be complete, this is my code (using Spring):</p> 
                                            <div class="code panel" style="border-style: solid;border-width: 1px;; border: 1px solid #ccc; background: #f5f5f5; font-size: 12px; line-height: 1.333; font-family: monospace; border: 1px solid #ccc; -moz-border-radius: 3px 3px 3px 3px; border-radius: 3px 3px 3px 3px; margin: 9px 0">
                                                <div class="codeHeader panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;; border-bottom: 1px solid #ccc; padding: 9px 12px">
                                                    <b>AbstractPersistentCollection.java</b>
                                                </div>
                                                <div class="codeContent panelContent" style="padding: 9px 12px"> 
                                                    <pre class="code-java" style="margin: 10px 0 0 0; max-height: 30em; overflow: auto; white-space: pre-wrap; word-wrap: normal">

    <span class="code-keyword" style="color: #000091">private</span> <span class="code-object" style="color: #910091">boolean</span> isConnectedToSession() {
        <span class="code-comment" style="color: #808080">// Begin patch
</span>        <span class="code-keyword" style="color: #000091">if</span> (session == <span class="code-keyword" style="color: #000091">null</span>) {
            <span class="code-comment" style="color: #808080">// If there is no session on the object, <span class="code-keyword" style="color: #000091; color: #808080">try</span> and get it from the ApplicationContext
</span>            ApplicationContext context = StaticBeanRegistry.getInstance().getApplicationContext();
            <span class="code-keyword" style="color: #000091">if</span> (context != <span class="code-keyword" style="color: #000091">null</span>) {
                SessionFactory sf = context.getBean(SessionFactory.class);
                session = (SessionImplementor) sf.getCurrentSession();
                <span class="code-keyword" style="color: #000091">if</span> (session == <span class="code-keyword" style="color: #000091">null</span>) {
                    session = (SessionImplementor) sf.openSession();
                }
                LOGGER.warn(<span class="code-quote" style="color: #009100">&quot;Patch: Detached object had no session, obtained one from the Session Factory&quot;</span>);
                <span class="code-keyword" style="color: #000091">if</span> (!session.getPersistenceContext().containsCollection(<span class="code-keyword" style="color: #000091">this</span>)) {
                    <span class="code-comment" style="color: #808080">// Reattach it
</span>                    CollectionPersister persister = session.getFactory().getCollectionPersister(role);
                    <span class="code-keyword" style="color: #000091">if</span> (owner != <span class="code-keyword" style="color: #000091">null</span> &amp;&amp; session.getPersistenceContext().getEntry(owner) == <span class="code-keyword" style="color: #000091">null</span>) {
                        /*
                         * Also attach owner, <span class="code-keyword" style="color: #000091">else</span> the collection will be marked as dereferenced (orphaned) which will delete the collection!
                         */
                        EntityPersister ownerPersister = persister.getOwnerEntityPersister();
                        Serializable ownerId = ownerPersister.getIdentifier(owner, session);
                        <span class="code-object" style="color: #910091">Object</span> version = ownerPersister.getVersion(owner);
                        <span class="code-object" style="color: #910091">Object</span>[] loadedState = ownerPersister.getPropertyValues(owner);
                        Status status = ownerPersister.isMutable() ? Status.MANAGED : Status.READ_ONLY;

                        session.getPersistenceContext().addEntry(owner, status, loadedState, <span class="code-keyword" style="color: #000091">null</span>, ownerId, version, LockMode.NONE, <span class="code-keyword" style="color: #000091">true</span>, ownerPersister,
                                ownerPersister.isVersioned(), <span class="code-keyword" style="color: #000091">true</span>);
                    }
                    <span class="code-keyword" style="color: #000091">if</span> (key != <span class="code-keyword" style="color: #000091">null</span>) {
                        session.getPersistenceContext().addUninitializedCollection(persister, <span class="code-keyword" style="color: #000091">this</span>, key);
                        session.initializeCollection(<span class="code-keyword" style="color: #000091">this</span>, <span class="code-keyword" style="color: #000091">false</span>);
                    } <span class="code-keyword" style="color: #000091">else</span> {
                        session.getPersistenceContext().addUninitializedDetachedCollection(persister, <span class="code-keyword" style="color: #000091">this</span>);
                    }
                }
            }
        }
        <span class="code-comment" style="color: #808080">// End patch
</span>        <span class="code-keyword" style="color: #000091">return</span> session != <span class="code-keyword" style="color: #000091">null</span> &amp;&amp;
                session.isOpen() &amp;&amp;
                session.getPersistenceContext().containsCollection( <span class="code-keyword" style="color: #000091">this</span> );
    }
</pre> 
                                                </div>
                                            </div> 
                                            <p style="margin: 10px 0 0 0">StaticBeanRegistry is a singleton holder of the Application Context via the ApplicationContextAware interface.</p> 
                                            <p style="margin: 10px 0 0 0">(Using version 4.2.7.SP1, upgraded from 3.6.9)</p> 
                                        </td> 
                                    </tr> 
                                </table> 
                            </td> 
                        </tr> 
                        <tr> 
                            <td class="email-content-main mobile-expand " style="padding: 0px; border-collapse: collapse; border-left: 1px solid #ccc; border-right: 1px solid #ccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #fff"> 
                                <table id="actions-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 1px"> 
                                    <tr> 
                                        <td id="actions-pattern-container" valign="middle" style="padding: 0px; border-collapse: collapse; padding: 10px 0 10px 24px; vertical-align: middle; padding-left: 0"> 
                                            <table align="left" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt"> 
                                                <tr> 
                                                    <td class="actions-pattern-action-icon-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 0px; vertical-align: middle"> <a href="https://hibernate.atlassian.net/browse/HHH-7457#add-comment" target="_blank" title="Add Comment" style="color: #3b73af; text-decoration: none"> <img class="actions-pattern-action-icon-image" src="https://hibernate.atlassian.net/images/mail/comment-icon.png" alt="Add Comment" title="Add Comment" height="16" width="16" border="0" style="vertical-align: middle" /> </a> 
                                                    </td> 
                                                    <td class="actions-pattern-action-text-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 4px; padding-left: 5px"> <a href="https://hibernate.atlassian.net/browse/HHH-7457#add-comment" target="_blank" title="Add Comment" style="color: #3b73af; text-decoration: none">Add Comment</a> 
                                                    </td> 
                                                </tr> 
                                            </table> 
                                        </td> 
                                    </tr> 
                                </table> 
                            </td> 
                        </tr> 
                        <!-- there needs to be content in the cell for it to render in some clients --> 
                        <tr> 
                            <td class="email-content-rounded-bottom mobile-expand" style="padding: 0px; border-collapse: collapse; color: #fff; padding: 0 15px 0 16px; height: 5px; line-height: 5px; background-color: #fff; border-top: 0; border-left: 1px solid #ccc; border-bottom: 1px solid #ccc; border-right: 1px solid #ccc; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; mso-line-height-rule: exactly">
                                &nbsp;
                            </td> 
                        </tr> 
                    </table> 
                </td> 
            </tr> 
            <tr> 
                <td id="footer-pattern" style="padding: 0px; border-collapse: collapse; padding: 12px 20px"> 
                    <table id="footer-pattern-container" cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt"> 
                        <tr> 
                            <td id="footer-pattern-text" class="mobile-resize-text" width="100%" style="padding: 0px; border-collapse: collapse; color: #999; font-size: 12px; line-height: 18px; font-family: Arial, sans-serif; mso-line-height-rule: exactly; mso-text-raise: 2px">
                                 This message was sent by Atlassian JIRA <span id="footer-build-information">(v6.2-OD-03#6206-<span title="14444503aa87c48a61ab99005f20a349f3a79824" data-commit-id="14444503aa87c48a61ab99005f20a349f3a79824}">sha1:1444450</span>)</span> 
                            </td> 
                            <td id="footer-pattern-logo-desktop-container" valign="top" style="padding: 0px; border-collapse: collapse; padding-left: 20px; vertical-align: top"> 
                                <table style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt"> 
                                    <tr> 
                                        <td id="footer-pattern-logo-desktop-padding" style="padding: 0px; border-collapse: collapse; padding-top: 3px"> <img id="footer-pattern-logo-desktop" src="https://hibernate.atlassian.net/images/mail/footer-desktop-logo.png" alt="Atlassian logo" title="Atlassian logo" width="169" height="36" class="image_fix" /> 
                                        </td> 
                                    </tr> 
                                </table> 
                            </td> 
                        </tr> 
                    </table> 
                </td> 
            </tr> 
        </table>   
    </body>
</html>