<?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>dominikdorn.com</title>
	<atom:link href="http://dominikdorn.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://dominikdorn.com</link>
	<description>shit happens ;)</description>
	<lastBuildDate>Sat, 18 May 2013 18:41:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Split a collection into subcollections of a specific size</title>
		<link>http://dominikdorn.com/2013/05/oracle-ora-01795-expressions-in-list-split-collection/</link>
		<comments>http://dominikdorn.com/2013/05/oracle-ora-01795-expressions-in-list-split-collection/#comments</comments>
		<pubDate>Sat, 18 May 2013 18:41:37 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[persistence]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=377</guid>
		<description><![CDATA[If you run into the error "ORA-01795 maximum number of expressions in a list is 1000", you'll probably created a query that has to many elements in the IN-clause. To fix it, use the provided helper class to issue multiple queries with less than 1000 elements. ]]></description>
				<content:encoded><![CDATA[<p>Recently I had to work with a <strong>Oracle</strong> instance and had to use a query that uses a subquery like (the IN &#8230; statement) this</p>
<p><code><br />
SELECT * FROM table_a WHERE id IN (...)<br />
</code></p>
<p>The method I had to implement/fix takes an arbitrary number of ids as arguments.. if you get<br />
more than 1000 ids, Oracle will spill a message like this into your fa.. aehm.. onto your console:<br />
<code><br />
"ORA-01795 maximum number of expressions in a list is 1000"<br />
</code></p>
<p>To &#8220;fix&#8221; this problem, I wrote a small utility class that creates sub-collections of a specific maximum size out of a given collection.</p>
<p>Here it is:</p>
<script src="https://gist.github.com/4492616.js?file=CollectionHelper.java"></script><noscript><pre><code class="language-java java">
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class CollectionHelper {


    public static &lt;T, U extends Collection&lt;T&gt;&gt; List&lt;U&gt; splitCollectionBySize(final U collection, final int sizePerList) {
        if (collection == null)
            throw new IllegalArgumentException(&quot;given collection may not be null&quot;);

        if (sizePerList &lt; 1)
            throw new IllegalArgumentException(&quot;sizePerList must be at least 1&quot;);

        Iterator&lt;T&gt; iterator = collection.iterator();
        List&lt;U&gt; resultList = new ArrayList&lt;U&gt;();

        int counter = 0;
        try {
            U currentCollection = (U) collection.getClass().newInstance();
            while (iterator.hasNext()) {
                currentCollection.add(iterator.next());
                counter++;

                if (counter &gt; sizePerList - 1) {
                    resultList.add(currentCollection);
                    currentCollection = (U) collection.getClass().newInstance();
                    counter = 0;
                }
            }
            if (!currentCollection.isEmpty())
                resultList.add(currentCollection);

        } catch (InstantiationException e) {
            throw new IllegalStateException(&quot;could not create a instance of the given collection of type &quot; + collection.getClass());
        } catch (IllegalAccessException e) {
            throw new IllegalStateException(&quot;could not create a instance of the given collection of type &quot; + collection.getClass());
        }
        return resultList;
    }

}
</code></pre></noscript>
<script src="https://gist.github.com/4492616.js?file=CollectionHelperTest.java"></script><noscript><pre><code class="language-java java">
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.fest.assertions.Assertions.assertThat;

public class CollectionHelperTest {

    @Test(expected = IllegalArgumentException.class)
    public void testSplitCollectionBySize_nullArgsForCollection_shouldThrowException() throws Exception {
        List&lt;Integer&gt; input = null;

        CollectionHelper.splitCollectionBySize(input, 500);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testSplitCollectionBySize_splitSizeSmallerOne_shouldThrowException() throws Exception {
        CollectionHelper.splitCollectionBySize(new ArrayList&lt;Object&gt;(), 0);
    }

    @Test
    public void testSplitCollectionBySize_splitBySizeOne() throws Exception {
        List&lt;Integer&gt; input = new ArrayList&lt;Integer&gt;();
        for (int i = 0; i &lt; 10; i++) {
            input.add(i);
        }
        List&lt;List&lt;Integer&gt;&gt; output = CollectionHelper.splitCollectionBySize(input, 1);
        assertThat(output).isNotNull().isNotEmpty();
        assertThat(output.size()).isEqualTo(10);
    }

    @Test
    public void testSplitCollectionBySize_splitByBiggerSizeThanCollectionSize() throws Exception {
        List&lt;Integer&gt; input = new ArrayList&lt;Integer&gt;();
        for (int i = 0; i &lt; 100; i++) {
            input.add(i);
        }
        List&lt;List&lt;Integer&gt;&gt; output = CollectionHelper.splitCollectionBySize(input, 500);
        assertThat(output).isNotNull().isNotEmpty();
        assertThat(output.size()).isEqualTo(1);
        assertThat(output.get(0)).isNotNull().isNotEmpty();
        assertThat(output.get(0).get(0)).isEqualTo(0);
        assertThat(output.get(0).get(99)).isEqualTo(99);
    }

    @Test
    public void testSplitCollectionBySize_splitEmptyCollection() throws Exception {
        List&lt;Integer&gt; input = new ArrayList&lt;Integer&gt;();

        List&lt;List&lt;Integer&gt;&gt; output = CollectionHelper.splitCollectionBySize(input, 500);
        assertThat(output).isNotNull().isEmpty();
        assertThat(output.size()).isEqualTo(0);
    }

    @Test
    public void testSplitCollectionBySize_shouldSplit498into10Collections_first9ShouldContain50() throws Exception {
        List&lt;Integer&gt; input = new ArrayList&lt;Integer&gt;();
        for (int i = 0; i &lt; 499; i++) {
            input.add(i);
        }

        List&lt;List&lt;Integer&gt;&gt; output = CollectionHelper.splitCollectionBySize(input, 50);

        assertThat(output).isNotNull().isNotEmpty();
        assertThat(output.size()).isEqualTo(10);
        for (int i = 0; i &lt; 9; i++) {
            assertThat(output.get(i)).isNotNull().isNotEmpty();
            assertThat(output.get(i).size()).isEqualTo(50);
        }
    }

    @Test
    public void testSplitCollectionBySize_shouldSplit498into10Collections_lastOneShouldBeSmaller() throws Exception {
        List&lt;Integer&gt; input = new ArrayList&lt;Integer&gt;();
        for (int i = 0; i &lt; 499; i++) {
            input.add(i);
        }

        List&lt;List&lt;Integer&gt;&gt; output = CollectionHelper.splitCollectionBySize(input, 50);

        assertThat(output).isNotNull().isNotEmpty();
        assertThat(output.size()).isEqualTo(10);

        assertThat(output.get(9)).isNotNull().isNotEmpty();
        assertThat(output.get(9).size()).isEqualTo(49);
    }


    @Test
    public void testSplitCollectionBySize_shouldSplit500into10Collections() throws Exception {
        List&lt;Integer&gt; input = new ArrayList&lt;Integer&gt;();
        for (int i = 1; i &lt;= 500; i++) {
            input.add(i);
        }

        List&lt;List&lt;Integer&gt;&gt; output = CollectionHelper.splitCollectionBySize(input, 50);

        assertThat(output).isNotNull().isNotEmpty();
        assertThat(output.size()).isEqualTo(10);

        List&lt;Integer&gt; firstList = output.get(0);
        assertThat(firstList).isNotNull().isNotEmpty();
        assertThat(firstList.size()).isEqualTo(50);
        assertThat(firstList.get(0)).isEqualTo(1);
        assertThat(firstList.get(49)).isEqualTo(50);

        List&lt;Integer&gt; secondList = output.get(1);
        assertThat(secondList).isNotNull().isNotEmpty();
        assertThat(secondList.size()).isEqualTo(50);
        assertThat(secondList.get(0)).isEqualTo(51);
        assertThat(secondList.get(49)).isEqualTo(100);

        List&lt;Integer&gt; lastList = output.get(9);
        assertThat(lastList).isNotNull().isNotEmpty();
        assertThat(lastList.size()).isEqualTo(50);
        assertThat(lastList.get(0)).isEqualTo(451);
        assertThat(lastList.get(49)).isEqualTo(500);
    }

}</code></pre></noscript>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2013/05/oracle-ora-01795-expressions-in-list-split-collection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>orm.xml and persistence.xml templates</title>
		<link>http://dominikdorn.com/2012/07/orm-xml-and-persistence-xml-jpa-templates/</link>
		<comments>http://dominikdorn.com/2012/07/orm-xml-and-persistence-xml-jpa-templates/#comments</comments>
		<pubDate>Sat, 14 Jul 2012 15:19:32 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[persistence]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=378</guid>
		<description><![CDATA[XML templates for the JPA 1.0 and JPA 2.0 versions of the orm.xml and persistence.xml files. These should allow your IDE of choice to validate and auto-complete documents. ]]></description>
				<content:encoded><![CDATA[<p>As I keep finding myself searching for <strong>orm.xml templates</strong> as well as <strong>persistence.xml templates</strong> for <strong>JPA 1.0</strong> and <strong>JPA 2.0</strong>, I decided to post them here for my own reference and others to find <img src='http://dominikdorn.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<h2>JPA 1.0 orm.xml and persistence.xml templates</h2>
<p><strong>orm.xml template for JPA 1.0</strong><br />
<script src="https://gist.github.com/3111820.js?file=jpa1_orm.xml"></script><noscript><pre><code class="language-xml xml">&lt;?xml version=&quot;1.0&quot; ?&gt;
&lt;entity-mappings
        xmlns=&quot;http://java.sun.com/xml/ns/persistence/orm&quot;
        xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
        xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/persistence/orm
        http://java.sun.com/xml/ns/persistence/orm_1_0.xsd&quot;
                 version=&quot;1.0&quot;&gt;

&lt;/entity-mappings&gt;</code></pre></noscript></p>
<p><strong>persistence.xml template for JPA 1.0</strong><br />
<script src="https://gist.github.com/3111820.js?file=jpa1_persistence.xml"></script><noscript><pre><code class="language-xml xml">&lt;?xml version=&quot;1.0&quot; ?&gt;
&lt;persistence xmlns=&quot;http://java.sun.com/xml/ns/persistence&quot;
   xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
   xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd&quot; version=&quot;1.0&quot;&gt;

&lt;/persistence&gt;</code></pre></noscript></p>
<h2>JPA 2.0 orm.xml and persistence.xml templates</h2>
<p><strong>orm.xml template for JPA 2.0</strong><br />
<script src="https://gist.github.com/3111820.js?file=jpa2_orm.xml"></script><noscript><pre><code class="language-xml xml">&lt;?xml version=&quot;1.0&quot; ?&gt;
&lt;entity-mappings
        xmlns=&quot;http://java.sun.com/xml/ns/persistence/orm&quot;
        xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
        xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/persistence/orm
        http://java.sun.com/xml/ns/persistence/orm_2_0.xsd&quot;
                 version=&quot;2.0&quot;&gt;

&lt;/entity-mappings&gt;</code></pre></noscript></p>
<p><strong>persistence.xml template for jpa 2.0</strong><br />
<script src="https://gist.github.com/3111820.js?file=jpa2_persistence.xml"></script><noscript><pre><code class="language-xml xml">&lt;?xml version=&quot;1.0&quot; ?&gt;
&lt;persistence xmlns=&quot;http://java.sun.com/xml/ns/persistence&quot;
   xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
   xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd&quot; version=&quot;2.0&quot;&gt;

&lt;/persistence&gt;</code></pre></noscript></p>
<p>With these templates your favorite IDE should be able to autocomplete the allowed tags on its own.</p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2012/07/orm-xml-and-persistence-xml-jpa-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using JPA EntityListener Annotations with Avaje Ebean</title>
		<link>http://dominikdorn.com/2012/01/jpa-entity-listener-annotations-avaje-ebean/</link>
		<comments>http://dominikdorn.com/2012/01/jpa-entity-listener-annotations-avaje-ebean/#comments</comments>
		<pubDate>Tue, 03 Jan 2012 19:11:56 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[ebean]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[JPA2]]></category>
		<category><![CDATA[Persistence]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=359</guid>
		<description><![CDATA[This post shows how to use the JPA EntityListener Annotations with Avaje Ebean.]]></description>
				<content:encoded><![CDATA[<p>While migrating a project of mine from <strong>JPA2</strong> to <a href="http://www.avaje.org/" title="Avaje Ebean" target="_blank"><strong>Avaje Ebean</strong></a>, I encountered a issue, I wasn&#8217;t expecting.</p>
<p><a href="http://www.avaje.org/" title="Avaje Ebean" target="_blank">Avaje Ebean</a> does not support the <strong>JPA EntityListener Annotations</strong>, like<br />
<code><br />
<a href="http://docs.oracle.com/javaee/6/api/javax/persistence/PrePersist.html" title="PrePersist JavaDoc" target="_blank">@PrePersist</a>,<br />
<a href="http://docs.oracle.com/javaee/6/api/javax/persistence/PostPersist.html" title="PostPersist JavaDoc" target="_blank">@PostPersist</a>,<br />
<a href="http://docs.oracle.com/javaee/6/api/javax/persistence/PreUpdate.html" title="PreUpdate JavaDoc" target="_blank">@PreUpdate</a>,<br />
<a href="http://docs.oracle.com/javaee/6/api/javax/persistence/PostUpdate.html" title="PostUpdate JavaDoc" target="_blank">@PostUpdate</a>,<br />
<a href="http://docs.oracle.com/javaee/6/api/javax/persistence/PostLoad.html" title="PostLoad JavaDoc" target="_blank">@PostLoad</a><br />
</code></p>
<p>Some nice folks on the Ebean Mailinglist directed me to some Documentation about <a href="http://www.avaje.org/jpaapi.html#listener" title="EntityListeners in Ebean" target="_blank"><strong>EntityListeners in Ebean</strong></a> including a <a href="http://www.avaje.org/topic-114.html" title="Forum Post about BeanController and BeanListener" target="_blank">helpful forum link</a> which finally pointed me to the <a href="http://www.avaje.org/static/javadoc/pub/com/avaje/ebean/event/BeanPersistController.html" title="JavaDoc of the BeanPersistController Interface" target="_blank"><strong>BeanPersistController Interface in the Ebean Java API</strong></a> .<br />
With that Information, I was able to create a EntityListener that enables the use of the <strong>JPA EntityListener Annotations with Ebean</strong>.</p>
<p>This Gist shows how I&#8217;ve done it:<br />
<script src="https://gist.github.com/1547244.js"></script><noscript><pre><code class="language-java java">package models.sgcore;

import com.avaje.ebean.event.BeanPersistAdapter;
import com.avaje.ebean.event.BeanPersistRequest;

import javax.annotation.PreDestroy;
import javax.persistence.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * This is a &lt;code&gt;BeanPersistController&lt;/code&gt; that looks for methods annotated with the JPA Annotations
 * &lt;code&gt;@PrePersist&lt;/code&gt;
 * &lt;code&gt;@PostPersist&lt;/code&gt;
 * &lt;code&gt;@PreUpdate&lt;/code&gt;
 * &lt;code&gt;@PostUpdate&lt;/code&gt;
 * &lt;code&gt;@PreDestroy&lt;/code&gt;
 * &lt;code&gt;@PostLoad&lt;/code&gt;
 *
 * registers those methods with this Listener and calls them when necessary.
 */
public class SGBeanPersistController extends BeanPersistAdapter {
    
    private Map&lt;String,Method&gt; prePersistMap = new TreeMap&lt;String, Method&gt;();
    private Map&lt;String,Method&gt; postPersistMap = new TreeMap&lt;String, Method&gt;();

    private Map&lt;String,Method&gt; preUpdateMap = new TreeMap&lt;String, Method&gt;();
    private Map&lt;String,Method&gt; postUpdateMap = new TreeMap&lt;String, Method&gt;();

    private Map&lt;String,Method&gt; preDestroyMap = new TreeMap&lt;String, Method&gt;();

    private Map&lt;String,Method&gt; postLoadMap = new TreeMap&lt;String, Method&gt;();




    
    @Override
    public boolean isRegisterFor(Class&lt;?&gt; aClass) {
        if(aClass.getAnnotation(Entity.class) != null){
            System.out.println(&quot;Registering a Entity; Type is &quot; + aClass.toString());
            Method[] methods = aClass.getMethods();
            boolean hasListener = false;
            for(Method m : methods)
            {
//                System.out.println(&quot;looking if method &quot; + m.toString() + &quot; has Annotation on it. &quot;);
                if(m.isAnnotationPresent(PrePersist.class))
                {
                    prePersistMap.put(aClass.getName(), m);
                    hasListener = true;
                }
                
                if(m.isAnnotationPresent(PostPersist.class))
                {
                    postPersistMap.put(aClass.getName(), m);
                    hasListener = true;
                }
                
                if(m.isAnnotationPresent(PreDestroy.class))
                {
                    preDestroyMap.put(aClass.getName(), m);
                    hasListener = true;
                }
                
                if(m.isAnnotationPresent(PreUpdate.class))
                {
                    preUpdateMap.put(aClass.getName(), m);
                    hasListener = true;
                }
                
                if(m.isAnnotationPresent(PostUpdate.class))
                {
                    postUpdateMap.put(aClass.getName(), m);
                    hasListener = true;
                }

                if(m.isAnnotationPresent(PostLoad.class))
                {
                    postLoadMap.put(aClass.getName(), m);
                    hasListener = true;
                }


            }
            return hasListener;
        }
        return false;
    }

    
    private void getAndInvokeMethod(Map&lt;String,Method&gt; map, Object o)
    {
        Method m = map.get(o.getClass().getName());
        if(m != null)
            try {
                m.invoke(o);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
    }

    @Override
    public boolean preInsert(BeanPersistRequest&lt;?&gt; request) {
        getAndInvokeMethod(prePersistMap, request.getBean());
        return super.preInsert(request);
    }

    @Override
    public boolean preDelete(BeanPersistRequest&lt;?&gt; request) {
        getAndInvokeMethod(preDestroyMap, request.getBean());
        return super.preDelete(request);
    }

    @Override
    public boolean preUpdate(BeanPersistRequest&lt;?&gt; request) {
        getAndInvokeMethod(preUpdateMap, request.getBean());
        return super.preUpdate(request);
    }

    @Override
    public void postDelete(BeanPersistRequest&lt;?&gt; request) {
        // there is no @PostDestroy annotation in JPA 2
        super.postDelete(request);
    }

    @Override
    public void postInsert(BeanPersistRequest&lt;?&gt; request) {
        getAndInvokeMethod(postPersistMap, request.getBean());
        super.postInsert(request);
    }

    @Override
    public void postUpdate(BeanPersistRequest&lt;?&gt; request) {
        getAndInvokeMethod(postUpdateMap, request.getBean());
        super.postUpdate(request);
    }

    @Override
    public void postLoad(Object bean, Set&lt;String&gt; includedProperties) {
        getAndInvokeMethod(postLoadMap, bean);
        super.postLoad(bean, includedProperties);
    }
}
</code></pre></noscript> </p>
<p>Questions? Comments? Forks? </p>
<p>I appreciate any kind of feedback! <img src='http://dominikdorn.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2012/01/jpa-entity-listener-annotations-avaje-ebean/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Presenting: Avaje Ebean  &#8211; An alternative approach to Java Persistence</title>
		<link>http://dominikdorn.com/2011/12/avaje-ebean-alternative-approach-to-java-persistence-jpa/</link>
		<comments>http://dominikdorn.com/2011/12/avaje-ebean-alternative-approach-to-java-persistence-jpa/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 19:46:42 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[persistence]]></category>
		<category><![CDATA[ebean]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[jsug]]></category>
		<category><![CDATA[Persistence]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=371</guid>
		<description><![CDATA[Recently I hold a presentation about Avaje Ebean on my local Java User Group &#8211; The Java Student User Group. Ebean is a alternative to the established Java Persistence API (JPA) implementations like Hibernate, EclipseLink etc. It uses the JPA Annotations like @Table @Entity @OneToOne @OneToMany @ManyToOne @ManyToMany @Column @Enumerated @Temporal etc. to map your [...]]]></description>
				<content:encoded><![CDATA[<p>Recently I hold a presentation about <a href="http://www.avaje.org/" title="Avaje Ebean Homepage" target="_blank"><strong>Avaje Ebean</strong></a> on my local Java User Group &#8211; The <a href="http://jsug.at" title="Java Student User Group Homepage" target="_blank">Java Student User Group</a>.</p>
<p>Ebean is a <strong>alternative to the</strong> established <strong>Java Persistence API</strong> (JPA) implementations like Hibernate, EclipseLink etc.<br />
It uses the JPA Annotations like<br />
<code>@Table<br />
@Entity<br />
@OneToOne<br />
@OneToMany<br />
@ManyToOne<br />
@ManyToMany<br />
@Column<br />
@Enumerated<br />
@Temporal<br />
</code><br />
etc.<br />
to map your Java Objects to your database tables, but thats all that it has in common with JPA and its implementations. </p>
<p>You can download the <a href='http://dominikdorn.com/wp-content/uploads/2012/01/ebean-presentation.pdf'>ebean presentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2011/12/avaje-ebean-alternative-approach-to-java-persistence-jpa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSF Components &#8211; The entity &#8220;uuml&#8221; was referenced, but not declared</title>
		<link>http://dominikdorn.com/2010/10/jsf-components-entity-referenced-not-declared/</link>
		<comments>http://dominikdorn.com/2010/10/jsf-components-entity-referenced-not-declared/#comments</comments>
		<pubDate>Mon, 25 Oct 2010 15:21:54 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Facelets]]></category>
		<category><![CDATA[javaee6]]></category>
		<category><![CDATA[JSF2]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=348</guid>
		<description><![CDATA[Shows how to use html entities in a JSF 2 Facelets Composite Component]]></description>
				<content:encoded><![CDATA[<p>Today I created a custom <strong>JSF 2 Composite Component</strong>, but <strong>Mojarra</strong> threw an error on me, when I tried to use a German <strong>Umlaut</strong> like <strong>&amp;uuml;</strong> in my markup, like this</p>
<h1>HTTP Status 500 -</h1>
<hr /><strong>type</strong> Exception report</p>
<p><strong>message</strong></p>
<p><strong>description</strong>The server encountered an internal error () that prevented it from fulfilling this request.</p>
<p><strong>exception</strong></p>
<pre>javax.servlet.ServletException: javax.servlet.ServletException: javax.faces.view.facelets.FaceletException: Error Parsing /resources/sg/uploadedDoc.xhtml: Error Traced[line: 45] The entity "uuml" was referenced, but not declared.</pre>
<p><strong>root cause</strong></p>
<pre>javax.servlet.ServletException: javax.faces.view.facelets.FaceletException: Error Parsing /resources/sg/uploadedDoc.xhtml: Error Traced[line: 45] The entity "uuml" was referenced, but not declared.</pre>
<p><strong>root cause</strong></p>
<pre>java.util.concurrent.ExecutionException: javax.faces.view.facelets.FaceletException: Error Parsing /resources/sg/uploadedDoc.xhtml: Error Traced[line: 45] The entity "uuml" was referenced, but not declared.</pre>
<p><strong>root cause</strong></p>
<pre>javax.faces.view.facelets.FaceletException: Error Parsing /resources/sg/uploadedDoc.xhtml: Error Traced[line: 45] The entity "uuml" was referenced, but not declared.</pre>
<p><strong>note</strong> <span style="text-decoration: underline;">The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.0.1 logs.</span></p>
<hr />
<h3>GlassFish Server Open Source Edition 3.0.1</h3>
<hr />The reason this happened is, that I referenced an entity &#8220;uuml;&#8221; in a xml document where it is not defined.<br />
XML basically just supports a few build-in entities, like &#8220;amp;&#8221; &#8220;quot;&#8221;, &#8220;apos;&#8221;, &#8220;lt;&#8221; and &#8220;gt;&#8221;.</p>
<p>To let the SAX-Parser know which additional entities I wanted to use, I simply added the XHTML 1.1 DOCTYPE to the head of the document.<br />
My Component now looks like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
<span style="color: #00bbdd;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:h</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/html&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:f</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/core&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:ui</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/facelets&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:composite</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsf/composite&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;composite:interface<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
....
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/composite:interface<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;composite:implementation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
.... text <span style="color: #ddbb00;">&amp;Uuml;</span>bermorgen ist auch noch ein Tag ... text
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/composite:implementation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Hope this helps some of you out there. </p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/10/jsf-components-entity-referenced-not-declared/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spring Security Facelets/JSF2 Tag-Library in new Web-Development Book</title>
		<link>http://dominikdorn.com/2010/10/spring-security-facelets-jsf2-tag-library-web-development-book/</link>
		<comments>http://dominikdorn.com/2010/10/spring-security-facelets-jsf2-tag-library-web-development-book/#comments</comments>
		<pubDate>Thu, 07 Oct 2010 15:33:13 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=341</guid>
		<description><![CDATA[My Spring Security Facelets/JSF2 Tag library has its own chapter in the new book of Décio Heinzelmann Luckow in his new Book Programação Java para a Web]]></description>
				<content:encoded><![CDATA[<p>My <a href="http://dominikdorn.com/facelets/">Spring Security Facelets/JSF2 Tag library</a> has its own chapter in the new book of <a href="http://www.decioluckow.com.br/">Décio Heinzelmann Luckow</a> in his new Book <a href="http://www.javaparaweb.com.br">Programação Java para a Web</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/10/spring-security-facelets-jsf2-tag-library-web-development-book/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Taming the beast: Binding imqbrokerd / OpenMQ to fixed ports</title>
		<link>http://dominikdorn.com/2010/09/binding-imqbrokerd-openmq-to-fixed-ports-ssh-firewal/</link>
		<comments>http://dominikdorn.com/2010/09/binding-imqbrokerd-openmq-to-fixed-ports-ssh-firewal/#comments</comments>
		<pubDate>Mon, 20 Sep 2010 22:17:57 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaEE6]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=334</guid>
		<description><![CDATA[Shows how to bind imqbrokerd  / OpenMQ to fixed ports. ]]></description>
				<content:encoded><![CDATA[<p>If you want to tunnel OpenMQ through SSH or any firewall, you&#8217;ll have to make sure to have fixed ports you can open.<br />
This took me ages, so maybe it helps someone:</p>
<p>I&#8217;m starting my imqbrokerd with this command</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>domdorn<span style="color: #000000; font-weight: bold;">/</span>gf<span style="color: #000000; font-weight: bold;">/</span>glassfishv3<span style="color: #000000; font-weight: bold;">/</span>mq<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>imqbrokerd \
<span style="color: #660033;">-javahome</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>domdorn<span style="color: #000000; font-weight: bold;">/</span>jdk1.6.0_21<span style="color: #000000; font-weight: bold;">/</span> \
<span style="color: #660033;">-port</span> <span style="color: #000000;">7676</span> \
<span style="color: #660033;">-startRmiRegistry</span> \
<span style="color: #660033;">-rmiRegistryPort</span> <span style="color: #000000;">34000</span> \
-Dimq.jmx.connector.jmxrmi.port=<span style="color: #000000;">31000</span> \
-Dimq.jmx.connector.ssljmxrmi.port=<span style="color: #000000;">32000</span> \
-Dimq.jmx.rmiregistry.port=<span style="color: #000000;">34000</span> \
-Dimq.portmapper.port=<span style="color: #000000;">7676</span> \
-Dimq.admin.tcp.port=<span style="color: #000000;">36000</span> \
-Dimq.cluster.port=<span style="color: #000000;">37000</span> \
-Dimq.cluster_discovery.port=<span style="color: #000000;">38000</span> \
-Dimq.cluster.heartbeat.port=<span style="color: #000000;">39000</span> \
-Dimq.httpjms.http.servletPort=<span style="color: #000000;">40000</span> \
-Dimq.httpsjms.https.servletPort=<span style="color: #000000;">41000</span> \
-Dimq.jms.tcp.port=<span style="color: #000000;">43000</span></pre></td></tr></table></div>

<p>which effectively binds imqbrokerd to these ports:</p>
<pre>
tcp6       0      0 :::34000                :::*                    LISTEN      22171/java      
tcp6       0      0 :::43000                :::*                    LISTEN      22171/java      
tcp6       0      0 :::31000                :::*                    LISTEN      22171/java      
tcp6       0      0 :::7676                 :::*                    LISTEN      22171/java      
tcp6       0      0 :::36000                :::*                    LISTEN      22171/java
</pre>
<p>Now simply connect to the remote host with</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh</span> REMOTEHOST -L7676:127.0.0.1:<span style="color: #000000;">7676</span> -L31000:127.0.0.1:<span style="color: #000000;">31000</span> -L34000:127.0.0.1:<span style="color: #000000;">34000</span> -L36000:127.0.0.1:<span style="color: #000000;">36000</span> -L43000:127.0.0.1:<span style="color: #000000;">43000</span></pre></td></tr></table></div>

<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/09/binding-imqbrokerd-openmq-to-fixed-ports-ssh-firewal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CDI/Weld beans.xml XSD/DTD</title>
		<link>http://dominikdorn.com/2010/08/cdi-weld-beans-xml-xsddtd/</link>
		<comments>http://dominikdorn.com/2010/08/cdi-weld-beans-xml-xsddtd/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 14:21:09 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaEE6]]></category>
		<category><![CDATA[CDI]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Weld]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=327</guid>
		<description><![CDATA[Shows how to proper use xml namespaces with CDI / Weld]]></description>
				<content:encoded><![CDATA[<p>As I&#8217;m often looking for the correct header of the <strong>beans.xml</strong> file required for <strong>Web Beans</strong> / <strong>Context and Dependency Injection</strong> (<strong>CDI</strong>) to work, I decided to share this simple header here with you. </p>
<p>If you don&#8217;t have anything to declare, create an empty beans.xml like this one</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> </span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
If you have to declare alternatives or interceptors, do it like this
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pre</span> <span style="color: #000066;">lang</span>=<span style="color: #ff0000;">'xml'</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> </span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;alternatives<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;stereotype<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            com.dominikdorn.dc.passwordReset.PasswordResetService
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/stereotype<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.dominikdorn.dc.passwordReset.StudyGuruPasswordReset<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/alternatives<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>A <a href="http://www.jetbrains.com/idea/">good IDE</a> will help you with creating a proper beans.xml as soon as you specify the xml namespace.</p>
<p>Popular implementations of <strong>CDI</strong> are</p>
<ol>
<li><a href="http://www.caucho.com/projects/candi/">CanDI</a></li>
<li><a href="http://seamframework.org/Weld/Development">Weld</a></li>
<li><a href="http://openwebbeans.apache.org">OpenWebBeans</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/08/cdi-weld-beans-xml-xsddtd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>StarCraft 2 (DVD) with Linux + Wine</title>
		<link>http://dominikdorn.com/2010/07/starcraft-2-dvd-with-linux-wine/</link>
		<comments>http://dominikdorn.com/2010/07/starcraft-2-dvd-with-linux-wine/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 13:13:25 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=318</guid>
		<description><![CDATA[Describes installing of StarCraft 2 from DVD on a Linux System (Ubuntu) with Wine and how to cope with the problems involved. ]]></description>
				<content:encoded><![CDATA[<p>I just got my StarCraft 2 Collectors Edition. </p>
<p>Right after unpacking it, I inserted the DVD and was wondering &#8220;They ship a whole DVD with just 2 Files, not more than 3 MB of size?&#8221; </p>
<p>The thing is: The DVD has a file system called UDF which supports hidden files and directories. Unlike with normal filesystems in linux, even a ls -lah does not show these files. </p>
<p><strong>1. Unmount the DVD</strong><br/><br />
Make sure to unmount the DVD first.<br />
Close every filemanager and console that has the DVD folder open.<br />
In my Ubuntu installation, the DVD is mounted to /media/cdrom0</p>
<p>Do the following in a console</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">umount</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>cdrom0</pre></td></tr></table></div>

<p>If it does not work, look which processes still have a lock on the directory using</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> lsof <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>cdrom0</pre></td></tr></table></div>

<p>and kill those.</p>
<p><strong>2. Mount the DVD correctly</strong><br />
First, get your own user Id. Most of the times its just 1000.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">id</span></pre></td></tr></table></div>

<p>should return something like this</p>
<pre>
uid=1000(domdorn) gid=<strong>1000</strong>(domdorn) groups=4(adm),20(dialout),24(cdrom),46(plugdev),103(fuse),104(lpadmin),114(admin),118(sambashare),1000(domdorn)
</pre>
<p>Note the values of uid=&#8230; and gid=&#8230;. (here both are 1000)</p>
<p>Next, mount the DVD the following way:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mount</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>cdrom <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>cdrom0 <span style="color: #660033;">-o</span> <span style="color: #007800;">uid</span>=<span style="color: #000000;">1000</span>,<span style="color: #007800;">gid</span>=<span style="color: #000000;">1000</span>,unhide,<span style="color: #007800;">umask</span>=0000</pre></td></tr></table></div>

<p>unhide makes linux show the hidden files on the dvd, uid/gid makes sure you&#8217;re allowed to read the files.</p>
<p><strong>3. Start the Installer</strong><br />
Now try to start the installer: Open a console, change to /media/cdrom0 and start it.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>cdrom0
<span style="color: #c20cb9; font-weight: bold;">wine</span> Installer.exe</pre></td></tr></table></div>

<p><strong>If you&#8217;re lucky, it now works out of the box and you are finished. </strong></p>
<p>If not (like me), it simply does nothing and we have to do the following. </p>
<p><strong>4. Copy the DVD</strong><br />
If the Installer does not work out of the box, create a folder on your filesystem, e.g.<br />
~/.wine/drive_c/sc2install<br />
and copy the whole DVDs contents to this directory. After this is finished, try to start the Installer from there. </p>
<p><del><br />
Log into your Battle.net Account.</p>
<p>http://www.battle.net</p>
<p>and Add your CD-Key to your Account. If you don&#8217;t have an Battle.net Account yet, create one, you&#8217;ll need it anyway.</p>
<p>After you&#8217;ve added the Game to your account, download the Windows Installer.<br />
Start up the downloaded installer and select a folder in your wines Drive C. Let it  download a few bytes and then quit the installer.<br />
Now copy the files</p>
<pre>
Installer Tome 1.MPQE.part
Installer UI 1.MPQ.part
Installer UI 2.MPQE.part
</pre>
<p>from /media/cdrom0 to the created folder. In my case its ~/.wine/drive_c/sc2download/SC2-WingsOfLiberty-enGB-Installer</p>
<p>Now startup the downloaded installer again.<br />
It should start checking the downloaded files (you might not see a difference in the progress bar, but the CPU goes up, watch with &#8220;top&#8221;).<br />
After the file check is finished, the installer should start.<br />
</del></p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/07/starcraft-2-dvd-with-linux-wine/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>JPA2 Abstract DAO,  Criteria Query &amp; the &#8220;like&#8221; Operator</title>
		<link>http://dominikdorn.com/2010/06/jpa2-abstract-dao-criteria-query-like-operator/</link>
		<comments>http://dominikdorn.com/2010/06/jpa2-abstract-dao-criteria-query-like-operator/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 13:48:17 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaEE6]]></category>
		<category><![CDATA[Abstract DAO]]></category>
		<category><![CDATA[DAO]]></category>
		<category><![CDATA[EclipseLink]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[JPA2]]></category>
		<category><![CDATA[Persistence]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=312</guid>
		<description><![CDATA[Shows an Abstract DAO realized with JPA2. Also includes a method findByAttributes which leverages JPA2s CriteriaQuerys and the like Operator. ]]></description>
				<content:encoded><![CDATA[<p>For a project at the university, I had to implement an abstract search in an abstract JPA Dao. </p>
<p>Maybe this class comes handy for some of you</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.dominikdorn.rest.dao</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.EntityManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.PersistenceContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.PersistenceException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.TypedQuery</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.criteria.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.ParameterizedType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author Dominik Dorn
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AbstractJpaDao<span style="color: #339933;">&lt;</span>TYPE<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    @PersistenceContext
    <span style="color: #000000; font-weight: bold;">protected</span> EntityManager em<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">Class</span> entityClass<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">Class</span> getEntityClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> entityClass<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setEntityClass<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> entityClass<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">entityClass</span> <span style="color: #339933;">=</span> entityClass<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> AbstractJpaDao<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ParameterizedType genericSuperclass <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ParameterizedType<span style="color: #009900;">&#41;</span> getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getGenericSuperclass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">entityClass</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>TYPE<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> genericSuperclass.<span style="color: #006633;">getActualTypeArguments</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> AbstractJpaDao<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> clazz<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">entityClass</span> <span style="color: #339933;">=</span> clazz<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> EntityManager getEm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> em<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> AbstractJpaDao setEm<span style="color: #009900;">&#40;</span>EntityManager em<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">em</span> <span style="color: #339933;">=</span> em<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> TYPE persist<span style="color: #009900;">&#40;</span>TYPE item<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>item <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> PersistenceException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Item may not be null&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        em.<span style="color: #006633;">persist</span><span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> item<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> TYPE update<span style="color: #009900;">&#40;</span>TYPE item<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>item <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> PersistenceException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Item may not be null&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        em.<span style="color: #006633;">merge</span><span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> item<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>TYPE<span style="color: #339933;">&gt;</span> getAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        CriteriaQuery cq <span style="color: #339933;">=</span> em.<span style="color: #006633;">getCriteriaBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cq.<span style="color: #006633;">select</span><span style="color: #009900;">&#40;</span>cq.<span style="color: #006633;">from</span><span style="color: #009900;">&#40;</span>entityClass<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> em.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>cq<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> TYPE getById<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>id <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> id <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> PersistenceException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Id may not be null or negative&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>TYPE<span style="color: #009900;">&#41;</span> em.<span style="color: #006633;">find</span><span style="color: #009900;">&#40;</span>entityClass, id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> delete<span style="color: #009900;">&#40;</span>TYPE item<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>item <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> PersistenceException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Item may not be null&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        em.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>em.<span style="color: #006633;">merge</span><span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>TYPE<span style="color: #339933;">&gt;</span> findByAttributes<span style="color: #009900;">&#40;</span>Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, String<span style="color: #339933;">&gt;</span> attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        List<span style="color: #339933;">&lt;</span>TYPE<span style="color: #339933;">&gt;</span> results<span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//set up the Criteria query</span>
        CriteriaBuilder cb <span style="color: #339933;">=</span> em.<span style="color: #006633;">getCriteriaBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        CriteriaQuery<span style="color: #339933;">&lt;</span>TYPE<span style="color: #339933;">&gt;</span> cq <span style="color: #339933;">=</span> cb.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>getEntityClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Root<span style="color: #339933;">&lt;</span>TYPE<span style="color: #339933;">&gt;</span> foo <span style="color: #339933;">=</span> cq.<span style="color: #006633;">from</span><span style="color: #009900;">&#40;</span>getEntityClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        List<span style="color: #339933;">&lt;</span>Predicate<span style="color: #339933;">&gt;</span> predicates <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Predicate<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s <span style="color: #339933;">:</span> attributes.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                predicates.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>cb.<span style="color: #006633;">like</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Expression<span style="color: #009900;">&#41;</span> foo.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;%&quot;</span> <span style="color: #339933;">+</span> attributes.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;%&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        cq.<span style="color: #006633;">where</span><span style="color: #009900;">&#40;</span>predicates.<span style="color: #006633;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Predicate<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        TypedQuery<span style="color: #339933;">&lt;</span>TYPE<span style="color: #339933;">&gt;</span> q <span style="color: #339933;">=</span> em.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>cq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        results <span style="color: #339933;">=</span> q.<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> results<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To instantiate this for an Entity, e.g. &#8220;Item&#8221;, simply do this</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">// get an entityManager somewhere here </span>
  AbstractJpaDao<span style="color: #339933;">&lt;</span>Item<span style="color: #339933;">&gt;</span> dao <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AbstractJpaDao<span style="color: #339933;">&lt;</span>Item<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dao.<span style="color: #006633;">setEm</span><span style="color: #009900;">&#40;</span>em<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>now you can use this Data Access Object for your persistence stuff. </p>
<p>To now <strong>search for entities</strong> in your DB, you can use the method <strong>findByAttributes</strong> which takes a map&lt;String,String&gt; and searches for appropriate items.<br />
If your Entity looks like this</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Item <span style="color: #009900;">&#123;</span>
&nbsp;
    @Id
    @GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">AUTO</span>, generator <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ITEM_GEN&quot;</span><span style="color: #009900;">&#41;</span>
    @SequenceGenerator<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ITEM_GEN&quot;</span>, allocationSize<span style="color: #339933;">=</span><span style="color: #cc66cc;">25</span>, sequenceName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;item_seq&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #339933;">;</span>
&nbsp;
    @Basic
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
    @Basic
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> description<span style="color: #339933;">;</span>
&nbsp;
    @Basic
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> size<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// constructors, getters, setters</span></pre></td></tr></table></div>

<p>you could search for an item which names contain &#8220;test&#8221; like this</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">&nbsp;
Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>,String<span style="color: #339933;">&gt;</span> attr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Hashmap<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>,String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
attr.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span>, <span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
List<span style="color: #339933;">&lt;</span>Item<span style="color: #339933;">&gt;</span> results <span style="color: #339933;">=</span> dao.<span style="color: #006633;">findByAttributes</span><span style="color: #009900;">&#40;</span>attr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>which comes quite handy in my opinion. Also note, that <strong>you don&#8217;t have to pre-generate your JPA2 Model classes</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/06/jpa2-abstract-dao-criteria-query-like-operator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
