<?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>Technotrance, Illusions and Perspectives &#187; jpa</title>
	<atom:link href="http://myblog.shriharisc.com/tag/jpa/feed/" rel="self" type="application/rss+xml" />
	<link>http://myblog.shriharisc.com</link>
	<description>A dose of everyday bruises with Java/JEE</description>
	<lastBuildDate>Mon, 02 Aug 2010 17:45:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Writing Paginated Queries in JPA/Hibernate</title>
		<link>http://myblog.shriharisc.com/2010/07/31/writing-paginated-queries-in-jpahibernate/</link>
		<comments>http://myblog.shriharisc.com/2010/07/31/writing-paginated-queries-in-jpahibernate/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 18:25:34 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[jee-light]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[jpa]]></category>

		<guid isPermaLink="false">http://myblog.shriharisc.com/?p=296</guid>
		<description><![CDATA[If you ever had a requirement to implement a feature which would fetch a subset of records in a huge set of tuples from the database. you could end up fine tuning or modifying SQL queries which could be database specific. However you could do it using the JPA or Hibernate provided you know whats [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever had a requirement to implement a feature which would fetch a subset of records in a huge set of tuples from the database. you could end up fine tuning or modifying SQL queries which could be database specific. However you could do it using the JPA or Hibernate provided you know whats the size of page (number of records page should contain) and what page number the user is currently in.</p>
<p>The below code gives you the basic method to retrieve paginated results from a table for a given page, with a page capacity:</p>
<pre class="brush: java">
public List fetchRecordsForPage(Integer pageIndex, Integer pageSize)
{
   Query query = entityManager.createQuery(&quot;select record from DisplayRecord record order by record.id&quot;);
   //Specify where to start from
   query.setFirstResult((pageIndex==1)?0:(pageIndex-1)*pageSize);
  //Specify how much records to hold back
  query.setMaxResults(pageSize);
  return query.getResultList();
}
</pre>
<p>This can be extended to hold criteria based API in Hibernate as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2010/07/31/writing-paginated-queries-in-jpahibernate/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Injecting JPA EntityManager in Google Guice through JBoss Seam</title>
		<link>http://myblog.shriharisc.com/2009/07/31/injecting-entitymanager-in-google-guice-through-jboss-seam/</link>
		<comments>http://myblog.shriharisc.com/2009/07/31/injecting-entitymanager-in-google-guice-through-jboss-seam/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 18:17:44 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[jee-light]]></category>
		<category><![CDATA[google-guice]]></category>
		<category><![CDATA[jboss-seam]]></category>
		<category><![CDATA[jpa]]></category>

		<guid isPermaLink="false">http://schakrap.wordpress.com/?p=87</guid>
		<description><![CDATA[This cool tip describes a 3 step process to use JPA&#8217;s EntityManager initialized in JBoss Seam inside the Google Guice environment.
1). Define Seam&#8217;s components.xml with entity-manager-factory component as below

&#60;components&#62;
    &#60;persistence:entity-manager-factory name=&#34;my_persistence_unit&#34;/&#62;
    &#60;guice:init injector=&#34;#{myInjector}&#34;/&#62;
    &#60;guice:injector name=&#34;myInjector&#34;&#62;
        &#60;guice:modules&#62;
    [...]]]></description>
			<content:encoded><![CDATA[<p>This cool tip describes a 3 step process to use JPA&#8217;s EntityManager initialized in JBoss Seam inside the Google Guice environment.</p>
<p>1). Define Seam&#8217;s components.xml with entity-manager-factory component as below</p>
<pre class="brush: xml">
&lt;components&gt;
    &lt;persistence:entity-manager-factory name=&quot;my_persistence_unit&quot;/&gt;
    &lt;guice:init injector=&quot;#{myInjector}&quot;/&gt;
    &lt;guice:injector name=&quot;myInjector&quot;&gt;
        &lt;guice:modules&gt;
           &lt;value&gt;mypackage.MyEntityManagerModule&lt;/value&gt;
        &lt;/guice:modules&gt;
    &lt;/guice:injector&gt;
&lt;/components&gt;</pre>
<p>where my_persistence_unit is the persistence unit name under META-INF/persistence.xml.</p>
<p>2).   Define a Google Guice Module to wire-up Seam&#8217;s EnitiyManagerFactory by looking-up based on the expression-value and binding it to Guice context using a Provider</p>
<pre class="brush: java">
public class MyEntityManagerModule extends AbstractModule
{   public void configure()
    {   Expressions expressions = Expressions.instance();
        ValueExpression emfVE = expressions.createValueExpression(&quot;#{my_persistence_unit}&quot;);
        EntityManagerFactory emf = (EntityManagerFactory)emfVE.getValue();
        EntityManagerProvider.setEntityManagerFactory(emf);
        bind(EntityManager.class).toProvider(EntityManagerProvider.class).in(Scopes.SINGLETON);
     }
 }
 </pre>
<pre class="brush: java">
class EntityManagerProvider implements Provider
    {   static EntityManagerFactory entityManagerFactory = null;
        public EntityManager get()
        {   return entityManagerFactory.createEntityManager();
        }
    }
 </pre>
<p>3)   One could use the EntityManager anywhere in the Seam-Guice hybrid component:</p>
<pre class="brush: java">
@Name(&quot;hybrid&quot;)
@Guice
public class HybridDAO
{  @Inject EntityManager entityManager;
   //other DAO methods.
}
</pre>
<p>This makes Guice components use EntityManager normally use Seam established EntityManagerFactory</p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2009/07/31/injecting-entitymanager-in-google-guice-through-jboss-seam/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing a Unitils DAO testing infrastructure for Spring and JPA on TestNG</title>
		<link>http://myblog.shriharisc.com/2009/06/29/implementing-a-unitils-dao-testing-infrastructure-for-spring-and-jpa-on-testng/</link>
		<comments>http://myblog.shriharisc.com/2009/06/29/implementing-a-unitils-dao-testing-infrastructure-for-spring-and-jpa-on-testng/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 17:50:51 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[unitils]]></category>

		<guid isPermaLink="false">http://schakrap.wordpress.com/?p=47</guid>
		<description><![CDATA[Unitils provides an excellant infrastructure for unit testing DAO (and data-service) layers  for Spring as IoC Framework on all the component unit testing frameworks such as JUnit 3, JUnit4 and TestNG, without any boilerplate code or coniguration hassles. We will look at establishing a testing infrastructure for Spring with JPA on TestNG frmaework.
Suppose the custom [...]]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://unitils.org">Unitils</a></strong> provides an excellant infrastructure for unit testing DAO (and data-service) layers  for Spring as IoC Framework on all the component unit testing frameworks such as JUnit 3, JUnit4 and TestNG, without any boilerplate code or coniguration hassles. We will look at establishing a testing infrastructure for Spring with JPA on TestNG frmaework.</p>
<p>Suppose the custom DAO and its implementation definition are like this (with regard to semantics of Spring JPA integration)</p>
<pre><span style="color:#0000ff;"><code>package mypackage;
public CustomDAO
{    public List findAll();
     public void save(Entity eo);
}</code></span></pre>
<pre><span style="color:#0000ff;">package mypackage;
public CustomDAOImpl implements CustommmDAO
{   @PersistenceContext
    private EntityManager em;</span></pre>
<pre><span style="color:#0000ff;">    //all other method implementations..
      ....
}</span></pre>
<p>In order to setup a Unitils test infrastructure on TestNG, the following is the class-template:</p>
<pre><span style="color:#0000ff;"><code>public class CustomDAOTestCase extends UnitilsTestNG
{   /**
     * Injects a test specific application context configuration
     */
    @SpringApplicationContext
    public ConfigurableApplicationContext createApplicationContext()
    {  return new ClassPathXmlApplicationContext("applicationContext-test.xml");
    }</code></span></pre>
<pre><span style="color:#0000ff;">     @SpringBean("customDAO")
     private CustomDAO customDAO;</span></pre>
<pre><span style="color:#0000ff;">     //all @Test annotated methods..</span></pre>
<pre><span style="color:#0000ff;">      @Override
      protected void unitilsAfterTestTearDown(java.lang.reflect.Method method)
      {}
}</span></pre>
<p>for which the following would be the test specific Spring&#8217;s application context confguration (applicationContext-test.xml)</p>
<pre><span style="color:#0000ff;">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;

 &lt;bean id="entityManagerFactory" &gt;
 &lt;property name="persistenceXmlLocation" value="persistence-test.xml"/&gt;
 &lt;property name="persistenceUnitName" value="customservice-test"/&gt;
 &lt;/bean&gt;

 &lt;bean id="transactionManager"&gt;
 &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
 &lt;/bean&gt;

 &lt;tx:annotation-driven /&gt;

 &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/&gt;

 &lt;bean id="customDAO" class="mypackage.CustomDAOImpl"/&gt;
&lt;/beans&gt;</span></pre>
<p>Look specifically at entityManagerFactory bean definition, org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean allows us to load test specific persistence.xml which could allow us to maintain more than onne persistence context for a sample application.<br />
These two definitions extend the Spring&#8217;s JPA capabilities for unit testing DAO on Unitils infrastructure!</p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2009/06/29/implementing-a-unitils-dao-testing-infrastructure-for-spring-and-jpa-on-testng/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
