<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    AS7, EJB3, JSF, JPA2: LazyInitializationException
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="https://community.jboss.org/people/jabu10245">jabu10245</a> in <i>JBoss AS 7 Development</i> - <a href="https://community.jboss.org/message/648068#648068">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hello Forum,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I'm new to AS7 etc. and I'm trying to create a little EAR project in the Standalone environment.</p><p>Here's my project configuration (using Maven):</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="text-decoration: underline;"><strong>myapp.ear:</strong></span></p><p>&lt;ear&gt;/META-INF/application.xml:</p><pre class="jive-pre"><code class="jive-code jive-xml">[...]
<span class="jive-xml-tag">&lt;module&gt;</span><span class="jive-xml-tag">&lt;ejb&gt;</span>myapp-ejb.jar<span class="jive-xml-tag">&lt;/ejb&gt;</span><span class="jive-xml-tag">&lt;/module&gt;</span>
<span class="jive-xml-tag">&lt;module&gt;</span><span class="jive-xml-tag">&lt;web&gt;</span><span class="jive-xml-tag">&lt;web-uri&gt;</span>myapp-jsf.war<span class="jive-xml-tag">&lt;/web-uri&gt;</span><span class="jive-xml-tag">&lt;/web&gt;</span><span class="jive-xml-tag">&lt;/module&gt;</span>
<span class="jive-xml-tag">&lt;library-directory&gt;</span>lib<span class="jive-xml-tag">&lt;/library-directory&gt;</span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&lt;ear&gt;/lib/commons-lang.jar</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&lt;ear&gt;/myapp-ejb.jar</p><p>&lt;ear&gt;/myapp-jsf.war</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="text-decoration: underline;"><strong>myapp-ejb.jar:</strong></span></p><p>&lt;jar&gt;/META-INF/beans.xml =&gt; empty beans.xml</p><p>&lt;jar&gt;/META-INF/persistence.xml:</p><pre class="jive-pre"><code class="jive-code jive-xml">[...]
<span class="jive-xml-tag">&lt;persistence-unit name="primary"&gt;</span>
&#160;&#160;&#160; <span class="jive-xml-tag">&lt;jta-data-source&gt;</span>java:jboss/datasources/ExampleDS<span class="jive-xml-tag">&lt;/jta-data-source&gt;</span>
&#160;&#160;&#160; <span class="jive-xml-tag">&lt;properties&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;property name="hibernate.hbm2ddl.auto" value="create-drop" /&gt;</span>
&#160;&#160;&#160; <span class="jive-xml-tag">&lt;/properties&gt;</span>
<span class="jive-xml-tag">&lt;/persistence-unit&gt;</span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>and an EJB (for testing purpose as singleton):</p><pre class="jive-pre"><code class="jive-code jive-java">@LocalBean
@Named(<font color="red">"fooManager"</font>)
@Singleton @Startup
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> FooManagerBean <font color="navy"><b>implements</b></font> FooManager <font color="navy">{</font>
&#160;&#160;&#160; <font color="navy"><b>private</b></font> @PersistenceContext EntityManager entityManager;
&#160;
&#160;&#160;&#160; <font color="navy"><b>public</b></font> Foo getFoo(String name) <font color="navy">{</font> <font color="darkgreen">/* JPA lookup using entityManager */</font> <font color="navy">}</font>
<font color="navy">}</font>
</code></pre><p>The class Foo is just a mapped entity having Foos as children:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">@Entity @Table(name = <font color="red">"foo"</font>)
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> Foo <font color="navy"><b>implements</b></font> Serializable <font color="navy">{</font>
&#160;&#160;&#160; @Column(name = <font color="red">"name"</font>, nullable = false, unique = true, length = 64)
&#160;&#160;&#160; <font color="navy"><b>private</b></font> String name;
&#160;
&#160;&#160;&#160; @ManyToOne
&#160;&#160;&#160; <font color="navy"><b>private</b></font> Foo parent;&#160; <font color="darkgreen">// nullable</font>
&#160;
&#160;&#160;&#160; @OneToMany(mappedBy = <font color="red">"parent"</font>)
&#160;&#160;&#160; <font color="navy"><b>private</b></font> List&lt;Foo&gt; children;
&#160;
&#160;&#160;&#160; <font color="darkgreen">// Getters and Setters, hashCode, equals, toString</font>
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong style="text-decoration: underline;">myapp-jsf.war:</strong></p><p>&lt;war&gt;/WEB-INF/beans.xml =&gt; empty beans.xml</p><p>&lt;war&gt;/WEB-INF/faces-config.xml =&gt; empty JSF 2.0 config xml</p><p>&lt;war&gt;foo.xhtml =&gt; displays all root foos with all their children =&gt; <span style="color: #ff0000;"><em><strong>LazyInitializationException</strong></em></span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">@Named @RequestScoped
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> FooController <font color="navy"><b>implements</b></font> Serializable <font color="navy">{</font>
[...]
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="color: #000000;">The FooController uses the </span><pre class="jive-pre"><code class="jive-code jive-java">@EJB FooManager
</code></pre><span style="color: #000000;"> to load all root Foo objects, the foo.xhtml should display them with</span></p><p><span style="color: #000000;">all their children. The root Foo objects are being loaded correctly, but when I try to access the</span></p><p><span style="color: #000000;">children a LazyInitializationException is being thrown, because I access them outside the transaction</span></p><p><span style="color: #000000;">scope of the Entity Manager from the FooManager.</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="color: #000000;">My question is: Is there something like Spring's OpenEntityManagerInViewInterceptor?</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="color: #000000;">I googled it and found some descriptions using Seam, but I have no experience with that. Is Seam</span></p><p><span style="color: #000000;">party of JBoss AS7? Or do I have to include it in my WAR as a dependency? What version should I</span></p><p><span style="color: #000000;">use? Or is there any simpler way solving my problem?</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="color: #000000;">Thanks to anyone who can point me to a solution ;-)<br/></span></p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/648068#648068">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in JBoss AS 7 Development at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2225">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>