<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://batmat.net/blog/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
  <title>Blogounage - ejb3</title>
  <link>http://batmat.net/blog/</link>
  <description></description>
  <language>fr</language>
  <pubDate>Sat, 28 Jan 2012 08:26:48 +0100</pubDate>
  <copyright></copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>How to retrieve the Hibernate Session from the EntityManager interface</title>
    <link>http://batmat.net/blog/post/2008/07/13/How-to-retrieve-the-Hibernate-Session-from-the-EntityManager-interface</link>
    <guid isPermaLink="false">urn:md5:318777f7c82d121b408a1abb41df548f</guid>
    <pubDate>Sun, 13 Jul 2008 23:42:00 +0200</pubDate>
    <dc:creator>batmat</dc:creator>
        <category>Technique</category>
        <category>ejb3</category><category>hibernate</category><category>java</category><category>jpa</category>    
    <description>    &lt;p&gt;Well, as I myself fell into this stupid gap, I guess I'll try to help. I was quite pushed in this trap by a fallacious part of the &lt;a href=&quot;http://docs.jboss.org/ejb3/app-server/reference/build/reference/en/html/hibernate.html#d0e241&quot; hreflang=&quot;en&quot;&gt;jboss documentation&lt;/a&gt; :&lt;/p&gt;
&lt;pre class=&quot;geshi&quot;&gt;&lt;code class=&quot;java&quot;&gt;@PersistenceContext EntityManager entityManager;&lt;br /&gt;
&lt;span class=&quot;kw2&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt; someMethod&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; org.&lt;span class=&quot;me1&quot;&gt;jboss&lt;/span&gt;.&lt;span class=&quot;me1&quot;&gt;ejb3&lt;/span&gt;.&lt;span class=&quot;me1&quot;&gt;entity&lt;/span&gt;.&lt;span class=&quot;me1&quot;&gt;HibernateSession&lt;/span&gt; hs = &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;HibernateSession&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;entityManager;&lt;br /&gt;
&amp;nbsp; org.&lt;span class=&quot;me1&quot;&gt;hibernate&lt;/span&gt;.&lt;span class=&quot;me1&quot;&gt;Session&lt;/span&gt; session = hs.&lt;span class=&quot;me1&quot;&gt;getHibernateSession&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;&lt;strong&gt;Let's be clear : this is totally WRONG!&lt;/strong&gt; Yes, it will work under JBoss, but as there's a standard way to do this, this is not the right way to do this.&lt;/p&gt;


&lt;p&gt;In fact, there's a dedicated method that lets you retrieve the underlying persistence manager : &lt;em&gt;EntityManager.getDelegate()&lt;/em&gt;. If you read this method's javadoc, you'll see the following :&lt;/p&gt;

&lt;pre class=&quot;geshi&quot;&gt;&lt;code class=&quot;java&quot;&gt;&lt;span class=&quot;coMULTI&quot;&gt;/**&lt;br /&gt;
&amp;nbsp;* Return the underlying provider object for the EntityManager,&lt;br /&gt;
&amp;nbsp;* if available. The result of this method is implementation&lt;br /&gt;
&amp;nbsp;* specific.&lt;br /&gt;
&amp;nbsp;* @throws IllegalStateException if this EntityManager has been closed.&lt;br /&gt;
&amp;nbsp;*/&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;kw2&quot;&gt;public&lt;/span&gt; &lt;a href=&quot;http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;amp;bntl=1&quot;&gt;&lt;span class=&quot;kw3&quot;&gt;Object&lt;/span&gt;&lt;/a&gt; getDelegate&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;And the &quot;underlying provider&quot;, for Hibernate, is obviously the Hibernate &lt;em&gt;Session&lt;/em&gt; instance...&lt;/p&gt;


&lt;p&gt;So, the right way to retrieve the Session is something like the following instead :&lt;/p&gt;

&lt;pre class=&quot;geshi&quot;&gt;&lt;code class=&quot;java&quot;&gt;@PersistenceContext EntityManager entityManager;&lt;br /&gt;
&lt;span class=&quot;kw2&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt; someMethod&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; org.&lt;span class=&quot;me1&quot;&gt;hibernate&lt;/span&gt;.&lt;span class=&quot;me1&quot;&gt;Session&lt;/span&gt; session = &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;Session&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;entityManager.&lt;span class=&quot;me1&quot;&gt;getDelegate&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Under JBoss, using this code won't make a big difference. But using the standard way will let you run your code under several appservers (namely, it works like a charm&lt;sup&gt;[&lt;a href=&quot;http://batmat.net/blog/post/2008/07/13/#pnote-293-1&quot; id=&quot;rev-pnote-293-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt; under JBoss 4.2.2.ga and Glassfishv2ur1 for example). If you use the bad way above, this will just crash under Glassfish, for example...&lt;/p&gt;


&lt;p&gt;I reported the &lt;a href=&quot;http://jira.jboss.com/jira/browse/JBDOCS-282&quot;&gt;corresponding documentation bug in the JBOSS tracker&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Hope this helps...&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://batmat.net/blog/post/2008/07/13/#rev-pnote-293-1&quot; id=&quot;pnote-293-1&quot;&gt;1&lt;/a&gt;] ça, c'est pour Seb&lt;/p&gt;&lt;/div&gt;
</description>
    
    
    
          <comments>http://batmat.net/blog/post/2008/07/13/How-to-retrieve-the-Hibernate-Session-from-the-EntityManager-interface#comment-form</comments>
      <wfw:comment>http://batmat.net/blog/post/2008/07/13/How-to-retrieve-the-Hibernate-Session-from-the-EntityManager-interface#comment-form</wfw:comment>
      <wfw:commentRss>http://batmat.net/blog/feed/rss2/comments/293</wfw:commentRss>
      </item>
    
</channel>
</rss>
