<?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>Developers, Developers, Developers! &#187; persistence</title>
	<atom:link href="http://maksim.sorokin.dk/it/tag/persistence/feed/" rel="self" type="application/rss+xml" />
	<link>http://maksim.sorokin.dk/it</link>
	<description>Maksim Sorokin IT Blog</description>
	<lastBuildDate>Sun, 05 Feb 2012 19:37:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>JPA Persistence With GlassFish</title>
		<link>http://maksim.sorokin.dk/it/2010/02/26/jpa-persistence-with-glassfish/</link>
		<comments>http://maksim.sorokin.dk/it/2010/02/26/jpa-persistence-with-glassfish/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 22:20:32 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[GlassFish]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[persistence]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=75</guid>
		<description><![CDATA[So you want to use javax.persistence. Here is how you can configure it and run with GlassFish. In this example we will use MySQL database. First, we will need to configure GlassFish JDBC resources. In GlassFish administration portal go to "Resources-&#62;JDBC-&#62;Connection Pools". Create new Connection Pool with name examplePool. Set Resource Type to java.sql.Driver; Database [...]]]></description>
			<content:encoded><![CDATA[<p>So you want to use <code>javax.persistence</code>. Here is how you can configure it and run with GlassFish.<span id="more-75"></span></p>
<p>In this example we will use MySQL database.<br />
First, we will need to configure GlassFish JDBC resources. In GlassFish administration portal go to "Resources-&gt;JDBC-&gt;Connection Pools". Create new Connection Pool with name <code>examplePool</code>. Set Resource Type to <code>java.sql.Driver</code>; Database Vendor to <code>MySql</code>, <code>com.mysql.jdbc.Driver</code>.<br />
Then go to "Resources-&gt;JDBC-&gt;JDBC Resources". Add new resource with JNDI Name <code>jdbc/example</code>. Set Pool Name to <code>examplePool</code>.</p>
<p>In the application under <code>src/main/resource</code> <code>META-INF</code> folder should be created. And inside <code>META-INF</code> folder <code>persistence.xml</code> file added with following content:</p>
<pre class="brush: xml; title: ;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&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-unit name=&quot;default&quot; transaction-type=&quot;JTA&quot;&gt;
    &lt;jta-data-source&gt;&lt;strong&gt;${jtaDataSource}&lt;/strong&gt;&lt;/jta-data-source&gt;
  &lt;/persistence-unit&gt;
&lt;/persistence&gt;
</pre>
<p>Where <code>${jtaDataSource}</code> should be <code>jdbc/example</code>. Or you can enable filtering in Maven <code>pom.xml</code> file and specify <code>jdbc/example</code> there So afterwards it would be easier to change and maintain it:</p>
<pre class="brush: xml; title: ;">
&lt;build&gt;
  ...
  &lt;resources&gt;
    &lt;resource&gt;
      &lt;directory&gt;src/main/resources&lt;/directory&gt;
      &lt;filtering&gt;true&lt;/filtering&gt;
    &lt;/resource&gt;
  &lt;/resources&gt;
  ...
&lt;/build&gt;
...
&lt;properties&gt;
  &lt;jtaDataSource&gt;jdbc/example&lt;/jtaDataSource&gt;
&lt;/properties&gt;
</pre>
<p>You are ready to go! Create Entity:</p>
<pre class="brush: java; title: ;">@Entity
@Table(name = &quot;pencils&quot;)
public class Pencil {
  @Id
  private int id;

  @Column
  private String color;

  public int getId() {
    return id;
  }

  public String getColor() {
    return color;
  }
}
</pre>
<p>Inject <code>javax.persistence.PersistenceContext</code>:</p>
<pre class="brush: java; title: ;">@PersistenceContext
private EntityManager em;</pre>
<p>And you can create queries:</p>
<pre class="brush: java; title: ;">Query q = em.createQuery(&quot;select p from Pencil p where p.color=:color&quot;);
q.setParameter(&quot;color&quot;, &quot;blue&quot;);</pre>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2010/02/26/jpa-persistence-with-glassfish/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

