<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java crumbs &#187; Hibernate statistics</title>
	<atom:link href="http://blog.krecan.net/tag/hibernate-statistics/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.krecan.net</link>
	<description>Short remarks from Java world</description>
	<lastBuildDate>Tue, 31 Jan 2012 20:13:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Spring factory method</title>
		<link>http://blog.krecan.net/2008/01/16/spring-factory-method/</link>
		<comments>http://blog.krecan.net/2008/01/16/spring-factory-method/#comments</comments>
		<pubDate>Wed, 16 Jan 2008 17:18:18 +0000</pubDate>
		<dc:creator>Lukáš Křečan</dc:creator>
				<category><![CDATA[Articles in English]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Hibernate statistics]]></category>
		<category><![CDATA[session factory]]></category>

		<guid isPermaLink="false">http://blog.krecan.net/2008/01/16/spring-factory-method/</guid>
		<description><![CDATA[Today I am going to write about a small trick that I have used recently. Lets imagine following problem: we are using JPA (Hibernate) and Spring together. &#60;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&#62; &#60;property name="persistenceUnitName" value="testPU" /&#62; &#60;property name="dataSource" ref="dataSource" /&#62; &#60;property name="jpaProperties"&#62; &#60;props&#62; &#60;prop key="hibernate.hbm2ddl.auto"&#62;${hibernate.hbm2ddl.auto}&#60;/prop&#62; &#60;/props&#62; &#60;/property&#62; &#60;/bean&#62; It works nice, without any problem. But imagine that [...]]]></description>
			<content:encoded><![CDATA[<p>Today I am going to write about a small trick that I have used recently. Lets imagine following problem: we are using JPA (Hibernate) and Spring together.</p>
<div  class="xml">
<pre>
<code>
&lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
	&lt;property name="persistenceUnitName" value="testPU" /&gt;
	&lt;property name="dataSource" ref="dataSource" /&gt;
	&lt;property name="jpaProperties"&gt;
		&lt;props&gt;
			&lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}&lt;/prop&gt;
		&lt;/props&gt;
	&lt;/property&gt;
&lt;/bean&gt;
</code>
</pre>
</div>
<p>It works nice, without any problem. But imagine that one day you need to access <a href="http://www.javalobby.org/java/forums/t19807.html">Hibernate statistics</a>. To achieve this, you have to have access to the Hibernate session factory. But we have no session factory here, we are using the entity manager which hides session factory underneath. How to access it? The solution is simple, you can use factory method. </p>
<div  class="xml">
<pre>
<code>
&lt;!-- Publishing session factory to be able view statistics --&gt;
&lt;bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory" /&gt;
</code>
</pre>
</div>
<p>By this simple code we say to Spring: Hey, just call the method getSessionFactory on the entityManagerFactory bean and store the result as a bean with name sessionFactory.  Since we are using Hibernate as the JPA provider, entityManagerFactory is instance of <a href="http://www.hibernate.org/hib_docs/entitymanager/api/org/hibernate/ejb/HibernateEntityManagerFactory.html">HibernateEntityManagerFactory</a> and by a chance it has  getSessionFactory method. Now we have access to the session factory and we can use it however we like.</p>
<p><em>Note: If you wonder why the hell I have started to write in something that looks almost like English when apparently I do even more mistakes than in Czech, the answer is simple. I just need to practice my English (apart from that I want to be world famous, not just known in Czech Republic)</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.krecan.net/2008/01/16/spring-factory-method/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

