<?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"
	>

<channel>
	<title>Jair Rillo Junior's blog</title>
	<atom:link href="http://www.jairrillo.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jairrillo.com/blog</link>
	<description>Articles and tutorials about technology</description>
	<pubDate>Sat, 13 Mar 2010 23:21:07 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>JPA 2 and Netbeans Guide - Part 3 - Querying</title>
		<link>http://www.jairrillo.com/blog/2010/03/13/jpa-2-and-netbeans-guide-part-3-querying/</link>
		<comments>http://www.jairrillo.com/blog/2010/03/13/jpa-2-and-netbeans-guide-part-3-querying/#comments</comments>
		<pubDate>Sat, 13 Mar 2010 23:21:07 +0000</pubDate>
		<dc:creator>Jair Rillo Junior</dc:creator>
		
		<category><![CDATA[JPA]]></category>

		<category><![CDATA[Netbeans]]></category>

		<category><![CDATA[Criteria]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[JEE]]></category>

		<category><![CDATA[JPA 2]]></category>

		<category><![CDATA[JPAQL]]></category>

		<guid isPermaLink="false">http://www.jairrillo.com/blog/?p=750</guid>
		<description><![CDATA[This is the third part of a series of posts about JPA 2 and Netbeans 6. This series has the goal to show up how to get started with JPA 2, as well as creating complex model using relationship and inheritance.
Part three is gonna use the same example than Part 1, but now we’re going [...]]]></description>
			<content:encoded><![CDATA[<p>This is the third part of a series of posts about JPA 2 and Netbeans 6. This series has the goal to show up how to get started with JPA 2, as well as creating complex model using relationship and inheritance.<br />
Part three is gonna use the same example than <a href="http://www.jairrillo.com/blog/2010/02/23/getting-started-with-jpa-2-and-netbeans-part-1/" target="_blank">Part 1</a>, but now we’re going to use the query mechanism to retrieve values from database.</p>
<h2>Requirement</h2>
<ul>
<li>Understand JPA concepts - If you have no idea what JPA is, please google it first</li>
<li>Netbeans 6.8 and glassfish v3 installed</li>
<li>MySQL database installed - If you have another one, make sure you have the JDBC driver</li>
<li>Read <a href="http://www.jairrillo.com/blog/2010/02/23/getting-started-with-jpa-2-and-netbeans-part-1/" target="_blank">Part 1</a> and have the database and POJO already created.</li>
</ul>
<h2>Querying strategy</h2>
<p>The most common way to retrieve values from relational databases is through an SQL Select Query, probably you have already wrote a lot of them. JPA has this ability and much more.<br />
Since version one, JPA allows you to retrieve values from native query, JPA-QL and NamedQuery, which uses JPA-QL. In version 2, JPA brings us another great API, Criteria API.</p>
<h2>Native Query</h2>
<p>Let&#8217;s get started with the most popular, but less used in JPA: Native Query.<br />
This kind of query must be used <strong>only</strong> when you need a specific command/resource from the database. Creating many specific queries, your application is going to be dependent from the database, it can be a disadvantage.<br />
To use the native query, simply use the <strong>createNativeQuery</strong> method from EntityManager. Let&#8217;s see the same example from part 1, but using the Native Query.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        EntityManagerFactory emFactory <span style="color: #339933;">=</span> Persistence.<span style="color: #006633;">createEntityManagerFactory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JPAExample01PU&quot;</span><span style="color: #009900;">&#41;</span>;
        EntityManager em <span style="color: #339933;">=</span> emFactory.<span style="color: #006633;">createEntityManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        List<span style="color: #339933;">&lt;</span>Products<span style="color: #339933;">&gt;</span> listProducts <span style="color: #339933;">=</span> em.<span style="color: #006633;">createNativeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select * from products&quot;</span>, Products.<span style="color: #000000; font-weight: bold;">class</span><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: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Products product <span style="color: #339933;">:</span> listProducts<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Product: &quot;</span> <span style="color: #339933;">+</span> product.<span style="color: #006633;">getDescription</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Price: &quot;</span> <span style="color: #339933;">+</span> product.<span style="color: #006633;">getPrice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;---------------------------------------&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The change happened on line 4. We have added the <strong>createNativeQuery</strong> method. The first parameter is the Select Query itself and the second one is the class where the result must be mapped.</p>
<h2>JPA-QL</h2>
<p>This is the query language for JPA. It is similar than SQL, but it uses object approach. There is a method called <strong>createQuery</strong> that returns a list of objects. See the example</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>Products<span style="color: #339933;">&gt;</span> listProducts <span style="color: #339933;">=</span> em.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select p from Products p&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The example above seems similar than the NativeQuery, but the biggest difference appear when you use JOIN. Supposing our Products table has a relationship with another table called Categories. We could create a JPA Query like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>Products<span style="color: #339933;">&gt;</span> listProducts <span style="color: #339933;">=</span> em.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select p from Products p inner join p.categories&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>We&#8217;re going to talk about relationship in Part 5.</p>
<h2>NamedQuery</h2>
<p>NamedQuery is the kind of querying that we used in part 1 of this guide. The NamedQuery is written using JPA-HQ and we use the <strong>createNamedQuery</strong> method to call it. The NamedQuery lives in the Entity class and has the annotation @NamedQuery.<br />
If you understand JPA-QL, certainly you&#8217;ll not find any problem to use NamedQuery.</p>
<h2>Criteria</h2>
<p>Criteria API is the newest API in JPA 2. If you&#8217;re familiar with Hibernate, like me, certainly you liked this API in JPA 2.<br />
As Criteria API is new, it deserves an unique post for it. Part 4 of this guide is exclusive for Criteria.</p>
<h2>Best Practices</h2>
<p>One of the most common question regarding querying in JPA is where to use them. The simple answer could be:</p>
<ul>
<li>Native Qurery: When you need a specific command/resource from the database that a JPA-QL doesn&#8217;t offer</li>
<li>JPA-QL: When you have a static query</li>
<li>NamedQuery: When you have a static query that doesn&#8217;t change, use it instead Criteria</li>
<li>Criteria: When you have a dynamic query</li>
</ul>
<p>This post ends here, but in the Part 4 we&#8217;re going to keep talking about querying, but we&#8217;re only talk about Criteria API.<br />
If you have any question or comment, fell free to leave your message below. I hope this topic be useful for anyone else.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jairrillo.com/blog/2010/03/13/jpa-2-and-netbeans-guide-part-3-querying/feed/</wfw:commentRss>
		</item>
		<item>
		<title>JPA 2 and Netbeans Guide - Part 2 - JPA in a web environment</title>
		<link>http://www.jairrillo.com/blog/2010/02/28/jpa-2-and-netbeans-guide-part-2-jpa-in-a-web-environment/</link>
		<comments>http://www.jairrillo.com/blog/2010/02/28/jpa-2-and-netbeans-guide-part-2-jpa-in-a-web-environment/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 23:40:47 +0000</pubDate>
		<dc:creator>Jair Rillo Junior</dc:creator>
		
		<category><![CDATA[Glassfish]]></category>

		<category><![CDATA[JPA]]></category>

		<category><![CDATA[JSF]]></category>

		<category><![CDATA[Netbeans]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[JPA 2]]></category>

		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.jairrillo.com/blog/?p=736</guid>
		<description><![CDATA[This is the second part of a series of posts about JPA 2 and Netbeans 6. This series has the goal to show up how to get started with JPA 2, as well as creating complex model using relationship and inheritance.
Part two is gonna use the same example than Part 1, but now we&#8217;re going [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second part of a series of posts about JPA 2 and Netbeans 6. This series has the goal to show up how to get started with JPA 2, as well as creating complex model using relationship and inheritance.<br />
Part two is gonna use the same example than <a href="http://www.jairrillo.com/blog/2010/02/23/getting-started-with-jpa-2-and-netbeans-part-1/" target="_blank">Part 1</a>, but now we&#8217;re going to work on a web environment. </p>
<h3>Requirement</h3>
<ul>
<li>Understand JPA concepts - If you have no idea what JPA is, please google it first</li>
<li>Netbeans 6.8 and glassfish v3 installed</li>
<li>MySQL database installed - If you have another one, make sure you have the JDBC driver</li>
<li>Read <a href="http://www.jairrillo.com/blog/2010/02/23/getting-started-with-jpa-2-and-netbeans-part-1/" target="_blank">Part 1</a> and have the database and POJO already created.</li>
</ul>
<h3>Creating the project within Netbeans</h3>
<p>Let&#8217;s getting started through the project creation. Within Netbeans, go to <em>File -> New Project -> Java Web -> Web Application</em>. Choose a name, like <em>JPAWebExample01</em> and Glassfish v3 as Server. On third screen, select JavaServer Faces and accept the default.<br />
<a href="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/jpa02_01.png" target="_blank"><img src="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/jpa02_01-300x175.png" alt="" title="jpa02_01" width="300" height="175" class="alignnone size-medium wp-image-738" /></a><br />
Note: don&#8217;t worry if you aren&#8217;t familiar with JSF. For this example we&#8217;ll focus on JPA only.</p>
<h3>Creating the DataSource</h3>
<p>Different than Stand-Alone application, in web application we could create a datasource to obtain and manage the database connection. This datasource lives in the Server, in our case, Glassfish.<br />
Fortunately we can create it directly inside Netbeans in an easy way. Let&#8217;s do that now.<br />
Similar than the post 1, let&#8217;s open the screen to create a entity class from the database. To do that, right mouse click on the project name and go to New -> Other -> Persistence -> Entity Classes from Database. On the new screen, you&#8217;re gonna see the <em>Data Source</em> field. Click on it and select <em>New Data Source</em>.<br />
<a href="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/jpa02_02.png" target="_blank"><img src="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/jpa02_02-300x72.png" alt="" title="jpa02_02" width="300" height="72" class="alignnone size-medium wp-image-741" /></a><br />
On the JNDI Name, fill up the field with the value <em>jdbc/MySQL_jpatutorial</em>.  Database Connection select the previous connection you&#8217;ve done in Part 1. Click OK and the DataSource will be created.</p>
<h2>Creating the Model and persistence.xml</h2>
<p>After you have created the datasource, you should see the table <em>products</em> listed. Now, you can follow exactly the same step from Part 1 to create the persistence.xml and the model.</p>
<h2>Creating a Manged-Bean</h2>
<p>Our application uses JSF 2.0 and because of this we can create a Managed Bean to handle the requests coming from view. If you don&#8217;t know what Managed Bean is, think he is a Java class that handles requests from view. It is similar a Controller from MVC, but it is directly binding with the view.<br />
In our example, the ManagedBean will obtain the EntityManager and call the NamedQuery.<br />
To create a ManagedBean with Netbeans, follow the following steps:<br />
Right mouse click on the project&#8217;s name <em>New -> Other -> JavaServer Faces -> JSF Managed Bean</em>. In the <em>Class Name</em> put <em>JPATutorialBean</em> and the <em>Package</em> put <em>mbeans</em>.<br />
<a href="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/jpa02_3.png" target="_blank"><img src="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/jpa02_3-300x238.png" alt="" title="jpa02_3" width="300" height="238" class="alignnone size-medium wp-image-744" /></a><br />
In th JPATutorialBean.java, let&#8217;s add a method called <em>listAllProducts</em>. The content looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">mbeans</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">entities.Products</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.bean.ManagedBean</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.bean.RequestScoped</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.EntityManager</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.PersistenceContext</span>;
&nbsp;
@ManagedBean<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;jpaTutorialBean&quot;</span><span style="color: #009900;">&#41;</span>
@RequestScoped
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JPATutorialBean <span style="color: #009900;">&#123;</span>
&nbsp;
    @PersistenceContext
    <span style="color: #000000; font-weight: bold;">private</span> EntityManager em;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> listAllProducts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       List<span style="color: #339933;">&lt;</span>Products<span style="color: #339933;">&gt;</span> listProducts <span style="color: #339933;">=</span> em.<span style="color: #006633;">createNamedQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Products.findAll&quot;</span><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: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Products product <span style="color: #339933;">:</span> listProducts<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Product: &quot;</span> <span style="color: #339933;">+</span> product.<span style="color: #006633;">getDescription</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Price: &quot;</span> <span style="color: #339933;">+</span> product.<span style="color: #006633;">getPrice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;---------------------------------------&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The method listAllProducts has no mistery. It is similar than the first post. Simply create a named query and prints out the result. However, <strong>pay attention</strong> on the lines:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">    @PersistenceContext
    <span style="color: #000000; font-weight: bold;">private</span> EntityManager em;</pre></div></div>

<p>We&#8217;ve used an annotation called @PersistenceContext. Different than StandAlone application, where we had to obtain a EntityManagerFactory and after that create the EntityManager, in a <strong>web based application, the container is responsible to inject the EntityManager for us</strong>. Really simple, doesn&#8217;t it?</p>
<h3>Editing the index.xhtml</h3>
<p>How can we call the method listAllProducts from ManagedBean? We can do that editing the index.xhtml and creating a link to access this method. Edit the current index.xhtml and add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
      xmlns:h=&quot;http://java.sun.com/jsf/html&quot;&gt;
    &lt;h:head&gt;
        &lt;title&gt;Facelet Title&lt;/title&gt;
    &lt;/h:head&gt;
    &lt;h:body&gt;
        Hello from Facelets
        &lt;h:form&gt;
            &lt;h:commandLink action=&quot;#{jpaTutorialBean.listAllProducts}&quot;&gt;
                &lt;h:outputText value=&quot;List All Products&quot; /&gt;
            &lt;/h:commandLink&gt;
        &lt;/h:form&gt;
    &lt;/h:body&gt;
&lt;/html&gt;</pre></div></div>

<p>The code above is HTML and JSF stuff. Look at the h:commandLink and you&#8217;re gonna see where we bind the link with the method. JSF also makes our life easier.</p>
<h3>Running and Testing the code</h3>
<p>Now all changes are done, right mouse click on the project and select the <em>Run</em> option. A new page is gonna be opened in your browser and then click on the link.<br />
Look at Netbeans console and you should see an output like below:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">INFO: Product: product <span style="color: #000000;">1</span>
INFO: Price: <span style="color: #000000;">100.0</span>
INFO: <span style="color: #660033;">---------------------------------------</span>
INFO: Product: product <span style="color: #000000;">2</span>
INFO: Price: <span style="color: #000000;">200.0</span>
INFO: <span style="color: #660033;">---------------------------------------</span>
INFO: Product: product <span style="color: #000000;">3</span>
INFO: Price: <span style="color: #000000;">200.5</span>
INFO: <span style="color: #660033;">---------------------------------------</span></pre></div></div>

<h3>Conclusion</h3>
<p>Doesn&#8217;t matter if you&#8217;re in a standalone or web application, the model (entity) is gonna be the same and the code to handle the JPA also, the biggest diffrence is the way to obtain the EntityManager. In a web application, the container is responsible to inject the EntityManager for us.</p>
<p>In the next topic we&#8217;re going talk about the strategies to retrieve values from JPA, such as: NamedQuery (already used), JPAQL and the newest feature, Criteria.</p>
<p>If you have any comment or question about this post, fell free to leave your message below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jairrillo.com/blog/2010/02/28/jpa-2-and-netbeans-guide-part-2-jpa-in-a-web-environment/feed/</wfw:commentRss>
		</item>
		<item>
		<title>JPA 2 and Netbeans Guide - Part 1 - Getting Started</title>
		<link>http://www.jairrillo.com/blog/2010/02/23/getting-started-with-jpa-2-and-netbeans-part-1/</link>
		<comments>http://www.jairrillo.com/blog/2010/02/23/getting-started-with-jpa-2-and-netbeans-part-1/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 23:06:52 +0000</pubDate>
		<dc:creator>Jair Rillo Junior</dc:creator>
		
		<category><![CDATA[Glassfish]]></category>

		<category><![CDATA[JPA]]></category>

		<category><![CDATA[Netbeans]]></category>

		<category><![CDATA[Java]]></category>

		<category><![CDATA[JPA 2]]></category>

		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.jairrillo.com/blog/?p=719</guid>
		<description><![CDATA[This is the first part of a series of posts about JPA 2 and Netbeans 6. This series has the goal to show up how to get started with JPA 2, as well as creating complex model using relationship and inheritance.
This part one is going to show up the steps to create your first application [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first part of a series of posts about JPA 2 and Netbeans 6. This series has the goal to show up how to get started with JPA 2, as well as creating complex model using relationship and inheritance.<br />
This part one is going to show up the steps to create your first application that access the database using JPA 2 (Stand alone application). Using Netbeans 6, this process is easy and takes less than five minutes.</p>
<h3>Requirement</h3>
<ul>
<li>Understand JPA concepts - If you have no idea what JPA is, please google it first</li>
<li>Netbeans 6.8 and glassfish v3 installed</li>
<li>MySQL database installed - If you have another one, make sure you have the JDBC driver</li>
</ul>
<h3>Preparing the database</h3>
<p>Before we get started with Netbeans, let&#8217;s prepare our database. The instructions below are regarding MySQL, but if you&#8217;re using another database, you may need to change the scripts to work out.<br />
Firstly, connect to your database:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">mysql <span style="color: #660033;">-u</span> root <span style="color: #660033;">-p</span></pre></div></div>

<p>Then, create the database and select it. Let&#8217;s call it as &#8220;jpatutorial&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">create database jpatutorial;
use jpatutorial;</pre></div></div>

<p>Now, run the script below. Basically it creates a table called &#8220;products&#8221; and insert three products by default.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">create table products <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">id</span> int not null auto_increment, description varchar<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">40</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>, price double, primary key<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">id</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
insert into products <span style="color: #7a0874; font-weight: bold;">&#40;</span>description, price<span style="color: #7a0874; font-weight: bold;">&#41;</span> values <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'product 1'</span>,<span style="color: #000000;">100</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; 
insert into products <span style="color: #7a0874; font-weight: bold;">&#40;</span>description, price<span style="color: #7a0874; font-weight: bold;">&#41;</span> values <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'product 2'</span>,<span style="color: #000000;">200</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; 
insert into products <span style="color: #7a0874; font-weight: bold;">&#40;</span>description, price<span style="color: #7a0874; font-weight: bold;">&#41;</span> values <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'product 3'</span>,<span style="color: #000000;">200.50</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;</pre></div></div>

<h3>Creating the project within Netbeans</h3>
<p>Now the &#8220;magic&#8221; starts. Open up Netbeans and create a simple Java program called &#8220;JPAExample01&#8243;. Netbeans also creates a &#8220;Main&#8221; class for you.<br />
<a href="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/jpa1_01.png" target="_blank"><img src="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/jpa1_01-300x185.png" alt="" title="jpa1_01" width="300" height="185" class="alignnone size-medium wp-image-723" /></a></p>
<h3>Creating the Model and persistence.xml</h3>
<p>After the project is created, right mouse click on it and go to <em>New -> Other -> Persistence -> Entity Classes from Database</em>. On next screen, select <em>Database Connection -> New Database Connection</em> and type the information from your database. After that, the table &#8220;products&#8221; should appear on the screen.<br />
<a href="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/jpa2_01.png" target="_blank"><img src="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/jpa2_01-300x179.png" alt="" title="jpa2_01" width="300" height="179" class="alignnone size-medium wp-image-724" /></a><br />
Select &#8220;products&#8221; and click on &#8220;Add&#8221; and then &#8220;Next&#8221;.<br />
On third screen, you must create our &#8220;Persistence Unit&#8221;. Click on <em>Create Persistence Unit&#8230;</em> button. Now, we must select the provider we want. By default, Netbeans brings the EclipseLink, but you&#8217;re free to change it. Also, you can change the Unit name and table generation strategy, but let&#8217;s keep the default so far.<br />
<a href="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/jpa3_01.png" target="_blank"><img src="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/jpa3_01-300x131.png" alt="" title="jpa3_01" width="300" height="131" class="alignnone size-medium wp-image-726" /></a><br />
The persistence.xml file has created, now click next until the end and finish the process (you can accept all the default options).<br />
Take a look at the Project and you&#8217;re gonna see a new folder called &#8220;META-INF&#8221; with the file persistence.xml and the file &#8220;Products.java&#8221; created.</p>
<h3>Examining the Products.java</h3>
<p>Usually a model is a simple POJO. With JPA, the POJO has annotations that define the primary key, columns, relationship and so on. Netbeans has already done these annotations for us. Open the Products.java and have a look.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
@Table<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;products&quot;</span><span style="color: #009900;">&#41;</span>
@NamedQueries<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    @NamedQuery<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Products.findAll&quot;</span>, query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT p FROM Products p&quot;</span><span style="color: #009900;">&#41;</span>,
    @NamedQuery<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Products.findById&quot;</span>, query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT p FROM Products p WHERE p.id = :id&quot;</span><span style="color: #009900;">&#41;</span>,
    @NamedQuery<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Products.findByDescription&quot;</span>, query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT p FROM Products p WHERE p.description = :description&quot;</span><span style="color: #009900;">&#41;</span>,
    @NamedQuery<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Products.findByPrice&quot;</span>, query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT p FROM Products p WHERE p.price = :price&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Products <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L;
    @Id
    @GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">IDENTITY</span><span style="color: #009900;">&#41;</span>
    @Basic<span style="color: #009900;">&#40;</span>optional <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> id;
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> description;
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;price&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Double</span> price;</pre></div></div>

<p>Attention: The netbeans has already generated some NamedQueries for us too, Thanks Netbeans team <img src='http://www.jairrillo.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Running and testing the JPA</h3>
<p>To test JPA, we must a EntityManager. In a stand-alone application, to get it, we must a EntityManagerFactory. Let&#8217;s change our Main class and initialize a EntityManager and call a simple NamedQuery. The Main class looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">jpaexample01</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">entities.Products</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.EntityManager</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.EntityManagerFactory</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Persistence</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        EntityManagerFactory emFactory <span style="color: #339933;">=</span> Persistence.<span style="color: #006633;">createEntityManagerFactory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JPAExample01PU&quot;</span><span style="color: #009900;">&#41;</span>;
        EntityManager em <span style="color: #339933;">=</span> emFactory.<span style="color: #006633;">createEntityManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        List<span style="color: #339933;">&lt;</span>Products<span style="color: #339933;">&gt;</span> listProducts <span style="color: #339933;">=</span> em.<span style="color: #006633;">createNamedQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Products.findAll&quot;</span><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: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Products product <span style="color: #339933;">:</span> listProducts<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Product: &quot;</span> <span style="color: #339933;">+</span> product.<span style="color: #006633;">getDescription</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Price: &quot;</span> <span style="color: #339933;">+</span> product.<span style="color: #006633;">getPrice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;---------------------------------------&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Pay attention on main method. We get a EntityManagerFactory, then an EntityManager, we call a NamedQuery already done and finally we iterate over the list and print out the description and price of the product.<br />
<strong>Before</strong> run the code, you must add the JDBC driver into the project&#8217;s classpath. To do that right mouse click on the project <em>Properties -> Libraries -> Add Library -> MySQL JDBC Driver</em><br />
Now you can run the application and if everything is fine, you are gonna see in the console:</p>
<pre>
Product: product 1
Price: 100.0
---------------------------------------
Product: product 2
Price: 200.0
---------------------------------------
Product: product 3
Price: 200.5
---------------------------------------
</pre>
<h3>Conclusion</h3>
<p>As you can see, Netbeans has a great support for JPA 2. You were able to create your first application in few minutes.</p>
<p>This post finishes here, in the next one we are going to talk about <a href="http://www.jairrillo.com/blog/2010/02/28/jpa-2-and-netbeans-guide-part-2-jpa-in-a-web-environment/">JPA 2 in a web environment</a>. You&#8217;re gonna see the code is similar, but the way to obtain the EntityManager is different.<br />
If you have any comment or question about this post, fell free to leave your message below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jairrillo.com/blog/2010/02/23/getting-started-with-jpa-2-and-netbeans-part-1/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Enabling USB on Sun VirtualBox v3 on Ubuntu step-by-step</title>
		<link>http://www.jairrillo.com/blog/2010/02/21/enabling-usb-on-sun-virtualbox-v3-on-ubuntu-step-by-step/</link>
		<comments>http://www.jairrillo.com/blog/2010/02/21/enabling-usb-on-sun-virtualbox-v3-on-ubuntu-step-by-step/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 22:45:15 +0000</pubDate>
		<dc:creator>Jair Rillo Junior</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[Ubuntu]]></category>

		<category><![CDATA[USB]]></category>

		<category><![CDATA[VirtualBox]]></category>

		<category><![CDATA[VM]]></category>

		<guid isPermaLink="false">http://www.jairrillo.com/blog/?p=709</guid>
		<description><![CDATA[VirtualMachine is a reality nowadays. If you have a base operating system (called host) and want to use another one on the base (called guest), you can use a VM to do that.
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.virtualbox.org/graphics/vbox_logo2_gradient.png" alt="Sun VirtualBox" align="left" />VirtualMachine is a reality nowadays. If you have a base operating system (called host) and want to use another one on the base (called guest), you can use a VM to do that.<br />
<a href="http://www.virtualbox.org/" target="_blank'>Sun VirtualBox</a> is a great free tool to do that in an easy and fast way. However, by default on Ubuntu, the USB doesn&#8217;t work properly. Inside the guest operating system, the USB is not accessible at all.<br />
In this topic, I&#8217;ll show up how to setup Ubuntu to enable the USB on guest operating system. Fortunately it is straightforward and doesn&#8217;t hurt <img src='http://www.jairrillo.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Downloading and installing Sun VirtualBox</h2>
<p><a href="http://www.virtualbox.org/wiki/Downloads">Downloading the Sun VirtualBox directly from its website.</a>For Ubuntu, there&#8217;s a .deb file. Simply download it and double click on it install it. After that the VirtualBox is installed. Now, you can install the Operating system that you want.</p>
<h2>Setup Ubuntu</h2>
<p>If you have already run Sun VirtualBox, probably you notified that USB didn&#8217;t appear. It happens because you need to make some changes on Ubuntu. Let&#8217;s do that now.<br />
First, open the terminal and type the following:</p>
<pre class="bash">
grep vboxusers /etc/group
</pre>
<p>Memorize the number that appear for you. In my example, the number is <strong>121</strong>.<br />
<a href="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/imagem1.png" target="_blank"><img src="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/imagem1-300x44.png" alt="" title="imagem1" width="300" height="44" class="alignnone size-medium wp-image-712" /></a><br />
Now, go to <em>System -> Administration -> Users and Groups</em>. After that, click on &#8220;Change button&#8221; and enter your password.<br />
<a href="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/imagem2.png" target="_blank"><img src="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/imagem2-300x145.png" alt="" title="imagem2" width="300" height="145" class="alignnone size-medium wp-image-710" /></a><br />
After that, click on <em>Manage Groups</em>. Find and select the <strong>vboxusers</strong> and click on <em>Properties</em> button. On the screen, select your user and type the Group ID that you picked early.<br />
<a href="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/imagem3.png" target="_blank"><img src="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/imagem3-300x214.png" alt="" title="imagem3" width="300" height="214" class="alignnone size-medium wp-image-714" /></a><br />
The changes are done. Now restart the Ubuntu and in the next time you run VM, the USB are gonna appear for you.<br />
<a href="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/imagem4.png" target="_blank"><img src="http://www.jairrillo.com/blog/wp-content/uploads/2010/02/imagem4-300x61.png" alt="" title="imagem4" width="300" height="61" class="alignnone size-medium wp-image-716" /></a><br />
<strong>PS</strong>: When you start the USB on guest machine, the USB port on host machine is closed and vice-versa.</p>
<p>This is a simple post but I hope it be helpful for anyone else. If you have any question or comment, fell free to leave your comment below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jairrillo.com/blog/2010/02/21/enabling-usb-on-sun-virtualbox-v3-on-ubuntu-step-by-step/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Long time&#8230; 2010 has began</title>
		<link>http://www.jairrillo.com/blog/2010/02/12/long-time-2010-has-began/</link>
		<comments>http://www.jairrillo.com/blog/2010/02/12/long-time-2010-has-began/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 13:51:39 +0000</pubDate>
		<dc:creator>Jair Rillo Junior</dc:creator>
		
		<category><![CDATA[Pessoal]]></category>

		<guid isPermaLink="false">http://www.jairrillo.com/blog/?p=707</guid>
		<description><![CDATA[O meu último post no blog foi no dia 12 de Novembro de 2009, ou seja, exatos 3 meses sem escrever aqui.
Certamente os meses de Novembro, Dezembro e início de Janeiro foram os mais desafiadores dentro da IBM para mim. Tinhamos algumas atividades monstruosas para desenvolvermos em tempo recorde. Eu fiz o papel de liderar [...]]]></description>
			<content:encoded><![CDATA[<p>O meu último post no blog foi no dia 12 de Novembro de 2009, ou seja, exatos 3 meses sem escrever aqui.<br />
Certamente os meses de Novembro, Dezembro e início de Janeiro foram os mais desafiadores dentro da IBM para mim. Tinhamos algumas atividades monstruosas para desenvolvermos em tempo recorde. Eu fiz o papel de liderar o time técnico e felizmente, graças ao grande trabalho do meu time (Gustavo Oliva, Victor, Gisele, Renato, Gustavo Castellano e Marcela), conseguimos atingir o objetivo com excelente qualidade.<br />
Esse foi um dos motivos que não tive tempo para postar novidades no blog, mas isso não significa que nesse tempo eu não tenha acompanhado as novidades no mundo de TI.<br />
Cheguei a fazer alguns exemplos do JSF 2.0 e JPA 2.0, a princípio gostei bastante de ambos. Dentro do projeto fiz algumas coisas com o Hibernate que nunca tinha feito antes, utilizei alguns design patterns que ajudaram muito no projeto, implantamos testes unitários úteis utilizando JUnit e EasyMock, enfim foram meses de pura tecnologia.<br />
2010 começa e eu vou tentar ser mais ativo no blog. Alguns posts já estão na cabeça, mas ainda falta pique para iniciá-los. No próximo dia 22 eu tiro férias de 13 dias e isso certamente vai servir para carregar as pilhas e ter mais um ano cheio de desafios.<br />
Uma coisa que já voltei a fazer com mais frequência é acessar o <a href="http://www.guj.com.br" target="_blank">GUJ.com.br</a> diariamente.<br />
Esse ano vou voltar de fato estudar Ruby e Scala, duas grandes linguagens que eu abandonei um tempo atrás. Internamente na empresa será o ano de entregar o segmento 1 do projeto e também pretendo tirar a certificação interna de IT Specialist.<br />
Enfim, 2010 será cheio de desafios, mas nós da área de TI sempre temos que arrumar tempo para estudar tecnologias novas e também compartilhar nosso conhecimento, seja através de blog, fórum, palestras, eventos, etc etc etc<br />
Bom 2010 para todos.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jairrillo.com/blog/2010/02/12/long-time-2010-has-began/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Ayrton Senna Racing Day 2009 - Eu fui e quase morri.</title>
		<link>http://www.jairrillo.com/blog/2009/11/12/ayrton-senna-racing-day-2009-eu-fui-e-quase-morri/</link>
		<comments>http://www.jairrillo.com/blog/2009/11/12/ayrton-senna-racing-day-2009-eu-fui-e-quase-morri/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 14:41:41 +0000</pubDate>
		<dc:creator>Jair Rillo Junior</dc:creator>
		
		<category><![CDATA[Pessoal]]></category>

		<guid isPermaLink="false">http://www.jairrillo.com/blog/?p=705</guid>
		<description><![CDATA[No dia 08/11/2009 eu participei, oficialmente, da minha primeira maratona por equipes. A maratona em questão foi simplesmente a Ayrton Senna Racing Day. A nossa equipe era de 8 corredores e cada um era responsável por completar uma volta no circuito de interlagos. Embora a quilometragem não aparenta ser alta (5.3km), a dificuldade é imensa, [...]]]></description>
			<content:encoded><![CDATA[<p>No dia 08/11/2009 eu participei, oficialmente, da minha primeira maratona por equipes. A maratona em questão foi simplesmente a Ayrton Senna Racing Day. A nossa equipe era de 8 corredores e cada um era responsável por completar uma volta no circuito de interlagos. Embora a quilometragem não aparenta ser alta (5.3km), a dificuldade é imensa, ainda mais para uma pessoa que mal treinou. Abaixo segue um relato da corrida.<br />
Eu gostei bastante da corrida, embora eu quase tenha morrido no final. São Pedro ajudou e não mandou aquele sol infernal. O tempo estava fechado porém quente.<br />
Eu fui o último da equipe a correr, entrei na pista próximo da 12:00. Fiz todo o percurso (5.3 km) em 35 minutos, bem próximo da média que eu calculei (7 minutos por km). É claro que esse tempo é MUITO melhor que o do Rubinho né!!<br />
O Evento é show de bola, porque você tem acesso a interlagos inteira (padock, boxes, arquibancadas, etc etc etc), porém a corrida é BEMMMM puxada.<br />
No início é só empolgação. A saída é dos boxes e o S do Senna é uma descida animal, depois entra na reta oposta que embora seja grande, é bem plana. No final da reta oposta tinha a marcação do KM 1, nesse ponto eu vi que tinha feito em 5 minutos o quilômetro. No caso eu parei com a empolgação e me concentrei em fazer a minha meta. Fim da reta oposta tem mais um descidão (e várias marcas de pneu, inclusive uma derrapagem saindo da pista que só pode ser do Rubinho, hehehe). Começa o laranjinha, primeira subida do circuito. Nessa hora comecei a me esforçar um pouco mais. É uma subida bem grande em curva, no final da subida tem a placa de 2KM e você pensa, pqp, ainda tem 3 KMs pela frente). A parte mista do circuito é tranquila, com subidas e descidas. Nesse ponto é interessante conhecer coisa do circuito que não se vê facilmente na televisão, como por exemplo, uma pista de terra no estilo off-road, com rampas, barro, etc, e também um lago bem no meio do circuito (agora eu entendi porque tem uma curva que chama curva do lago).<br />
Fim da junção e ai começa o pesadelo. Eu me lembro até agora que eu olhei para a direita e ví um morro do meu lado, era o início da subida!!! Nesse ponto você ainda está no KM3.<br />
O interessante que você vê bastante gente andando e lá em cima tinha uma ambulância de prontidão, eu pensei: pelo menos até a ambulância eu chego. Começa a subida e meu ritmo vai diminuindo, diminuindo, diminuindo e eu penso: meu preparo físico está uma mer**, correr 6km na lagoa do taquaral é MUITO mais fácil. Eu estava prester a andar quando um senhor da terceira idade me ultrapassou, bom, isso feriu meu orgulho e fez eu continuar<br />
A subida é insana e quando você acha que está terminando, você olha uma placa de 4km. Isso é o início da &#8220;reta&#8221;. Reta entre aspas, pois depois de 4km e uma subida monstro, aquilo está longe de ser uma simples reta. Nesse ponto você já está de frente com os boxes, paddock e arquibancada. Dá para ver o grid de largada e as marcas dos pneus também, além das luzes de largada. Embora você esteja cansado, anima um pouco porque você passa na frente da galera.<br />
No traçado da maratona, você tem que ir até o final da reta e voltar até a entrada dos boxes, esse &#8220;pequeno&#8221; trajeto tem 1KM.<br />
Após isso são os 300 metros finais. Para ficar mais divertido, esses 300 metros são em subida (já que a entrada dos boxes é uma subida). Nesse ponto eu usei as últimas forças para um sprint final. Não pensando em melhorar meu tempo, pelo contrário, para parar de correr o mais rápido possível.<br />
Algumas coisas engraçadas aconteceram no meio da prova, como por exemplo, o asfalto cheio de gatorade que o pessoal derrubava no KM 3. Isso fazia com que o tenis ficava colado no chão. Outro detalhe foi o meu número da camisa que caiu no meio da prova, ai eu fiquei correndo segurando o número.</p>
<p>Resumindo: foi divertido porém bastante cansativo. Sinceramente não garanto que eu vá fazer uma insanidade dessa novamente, ja que correr não é algo divertido (diferente de futebol e tennis).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jairrillo.com/blog/2009/11/12/ayrton-senna-racing-day-2009-eu-fui-e-quase-morri/feed/</wfw:commentRss>
		</item>
		<item>
		<title>JSF 2.0 and AJAX example</title>
		<link>http://www.jairrillo.com/blog/2009/10/10/jsf-20-and-ajax-example/</link>
		<comments>http://www.jairrillo.com/blog/2009/10/10/jsf-20-and-ajax-example/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 14:20:24 +0000</pubDate>
		<dc:creator>Jair Rillo Junior</dc:creator>
		
		<category><![CDATA[JSF]]></category>

		<category><![CDATA[AJAX]]></category>

		<category><![CDATA[JSF 2]]></category>

		<guid isPermaLink="false">http://www.jairrillo.com/blog/?p=699</guid>
		<description><![CDATA[As you should know, in JSF 1 the AJAX wasn&#8217;t default. To use AJAX we had to use an external component, such as Richfaces (ajax4jsf), IceFaces, etc. This raises some drawback, because you cannot use both components in the same project.
However, in version 2, the AJAX API comes by default. If you&#8217;re familiar with ajax4jsf, [...]]]></description>
			<content:encoded><![CDATA[<p>As you should know, in JSF 1 the AJAX wasn&#8217;t default. To use AJAX we had to use an external component, such as <a href="http://www.jboss.org/richfaces" target="_blank">Richfaces</a> (ajax4jsf), <a href="http://www.icefaces.org/main/home/" target="_blank">IceFaces</a>, etc. This raises some drawback, because you cannot use both components in the same project.<br />
However, in version 2, the AJAX API comes by default. If you&#8217;re familiar with ajax4jsf, you&#8217;ll fee comfortable to use the AJAX API in JSF 2.0.Also, the JSF team promisses that you&#8217;ll be able to use external AJAX API, such as YUI, JQuery, etc, in JSF smoothly. Check it out this topic: <a href="http://weblogs.java.net/blog/driscoll/archive/2009/08/using_the_yui_c_1.html" target="_blank">http://weblogs.java.net/blog/driscoll/archive/2009/08/using_the_yui_c_1.html</a><br />
Let&#8217;s create a simple example using our previous example that has been written <a href="http://www.jairrillo.com/blog/2009/10/02/getting-starting-with-jsf-20-a-first-example/" target="_blank">here</a>. Please, go to the previous post and download the example.</p>
<h2>How the example works</h2>
<p>In the previous example we had 2 JSPs and 1 ManagedBean. The principal.xhtml had a form with one field (name). A submit button calls the managed bean and this name attribute was converted to upper case. Finally, the success.xhtml showed the result.<br />
Now, let&#8217;s use only the principal.xhtml file. It will have the form and then will show up the result.</p>
<h2>Changing the principal.xhtml page</h2>
<p>The first thing to do is setup the javascript library into page. To do that, simple add the following in the head content.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;h:outputScript name=&quot;jsf.js&quot; library=&quot;javax.faces&quot; target=&quot;head&quot; /&gt;</pre></div></div>

<p>Another change is in the h:form tag. Add the <em>prependId=&#8221;false&#8221;</em> attribute.<br />
Note: All fields must have the id attribute setup. As you should know, AJAX looks for those IDs to make the request. </p>
<h2>Adding AJAX call</h2>
<p>So far we changed the principal.xhtml file to be able to make AJAX call, but where is the call itself?<br />
It is done through a tag called f:ajax. This tag can be inserted into components, such as h:commandButton to make an ajax call.<br />
So, let&#8217;s create a commandButton and add the AJAX functionality for it.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">        	&lt;h:commandButton value=&quot;Ajax Button&quot; action=&quot;#{helloWorld.ajaxCall}&quot;&gt;
        		&lt;f:ajax event=&quot;action&quot; execute=&quot;@form&quot; render=&quot;out&quot;/&gt;        		
        	&lt;/h:commandButton&gt;</pre></div></div>

<p>Some important notes: The h:commandButton is common. It has a value and an action call. It could have other events, such as: onclick, onblur, etc.<br />
Inside the button we have the f:ajax tag. Its event attribute tells to JSF with event will be called, in our example the <em>action</em> event. Other events, such as onclick can be used.<br />
The execute attribute tells with data should be sent do the server. The @form value tells that all the form should be sent.<br />
Finally, we have the render attribute. In this render we tell to JSF with component will be updated after the call.<br />
Now, we just need to add our h:outputText component.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;h:outputText value=&quot;#{helloWorld.name}&quot; id=&quot;out&quot; /&gt;</pre></div></div>

<p>After those changes, our entire principal.xhtml looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
      &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
      xmlns:h=&quot;http://java.sun.com/jsf/html&quot;
      xmlns:f=&quot;http://java.sun.com/jsf/core&quot;
      xmlns:ui=&quot;http://java.sun.com/jsf/facelets&quot;&gt;
&lt;h:head&gt;
	&lt;h:outputScript name=&quot;jsf.js&quot; library=&quot;javax.faces&quot; target=&quot;head&quot; /&gt;
&lt;/h:head&gt;      
&lt;h:body&gt;
    &lt;f:view&gt;
        &lt;h2&gt;&lt;h:outputText value=&quot;This is our first JSF 2.0 example&quot; /&gt;&lt;/h2&gt;
        &lt;h:form prependId=&quot;false&quot; id=&quot;form&quot;&gt;
        	Name: &lt;h:inputText value=&quot;#{helloWorld.name}&quot; id=&quot;name&quot; /&gt;&lt;br /&gt;
        	&lt;h:commandButton action=&quot;#{helloWorld.doSomething}&quot; value=&quot;Something is gonna happen&quot; /&gt;&lt;br /&gt;
        	&lt;h:commandButton value=&quot;Ajax Button&quot; action=&quot;#{helloWorld.ajaxCall}&quot;&gt;
        		&lt;f:ajax event=&quot;action&quot; execute=&quot;@form&quot; render=&quot;out&quot;/&gt;        		
        	&lt;/h:commandButton&gt; 
        	&lt;h:outputText value=&quot;#{helloWorld.name}&quot; id=&quot;out&quot; /&gt;        	
        &lt;/h:form&gt;        
    &lt;/f:view&gt;
&lt;/h:body&gt;
&lt;/html&gt;</pre></div></div>

<p>Note: We kept the old button there, thus you can have both example in the same project.<br />
</h2>
<p>Chaning the ManagedBean</h2>
<p>If you look at the AJAX Button, you&#8217;ll see that its call an <em>ajaxCall</em> method. So, let&#8217;s implement it into ManagedBean.<br />
Open the HelloWorldMBean.java class and add the following method:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> ajaxCall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		name <span style="color: #339933;">=</span> name.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note: the method doesn&#8217;t have a return. That&#8217;s simple, huh?</p>
<h2>Running the code</h2>
<p>After that, you can run the project, type something and click on AJAX Button. You&#8217;ll realize that a AJAX call was performed because only the result is in upper case, not the value in the field.<br />
<a href="http://www.jairrillo.com/blog/wp-content/uploads/2009/10/ajax_jsf.png"><img src="http://www.jairrillo.com/blog/wp-content/uploads/2009/10/ajax_jsf.png" alt="" title="ajax_jsf" width="480" height="171" class="alignnone size-full wp-image-702" /></a></p>
<h2>Conclusion</h2>
<p>Certainly the JSF 2.0 brings great news and native AJAX support is one of them. Who knows in the future, we can use multiples AJAX components in our single project. Now you already know how to use AJAX support, try out combine it with other AJAX frameworks, such as YUI or JQuery.</p>
<p>If you have any comment, question or feedback, please, let your message below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jairrillo.com/blog/2009/10/10/jsf-20-and-ajax-example/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Getting Starting with JSF 2.0 - A First Example</title>
		<link>http://www.jairrillo.com/blog/2009/10/02/getting-starting-with-jsf-20-a-first-example/</link>
		<comments>http://www.jairrillo.com/blog/2009/10/02/getting-starting-with-jsf-20-a-first-example/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 02:29:28 +0000</pubDate>
		<dc:creator>Jair Rillo Junior</dc:creator>
		
		<category><![CDATA[JSF]]></category>

		<category><![CDATA[JSF 2.0]]></category>

		<guid isPermaLink="false">http://www.jairrillo.com/blog/?p=685</guid>
		<description><![CDATA[JSF 2.0 specification has already been approved, however its official version is gonna be released with JEE 6.
Although its stable version has not been released, we can get started with some examples and get familiar with the new changes, by the way, a lot of changes.
If you&#8217;re interesting about JSF 2.0 and would like to [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="" src="https://glassfish-theme.dev.java.net/logo.gif" align="left" width="107" height="86" />JSF 2.0 specification has already been approved, however its official version is gonna be released with JEE 6.<br />
Although its stable version has not been released, we can get started with some examples and get familiar with the new changes, by the way, a lot of changes.<br />
If you&#8217;re interesting about JSF 2.0 and would like to test an example, this post may be useful for you.</p>
<h2>Creating the web project</h2>
<p>The first thing to do is creating a web project. As usual, we&#8217;re going to use Maven to do that. Type the following onto console.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">mvn archetype:create \
 -<span style="color: #007800;">DgroupId</span>=com.jairrillo \
 -<span style="color: #007800;">DartifactId</span>=JSF2FirstExample \
 -<span style="color: #007800;">DarchetypeArtifactId</span>=maven-archetype-webapp</pre></div></div>

<p>After that, edit the pom.xml file and add the content below:</p>

<div class="wp_syntax"><div class="code"><pre class="xml xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <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://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_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;modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.jairrillo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>JSF2FirstExample<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jar<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>JSF2FirstExample<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://maven.apache.org<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-compiler-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.0.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>junit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Note: We didn&#8217;t setup the JSF&#8217;s libraries. Unfortunately the JSF 2 JAR files aren&#8217;t within the maven repository yet. We&#8217;ll copy them yourself.</p>
<h2>Downloading JSF 2.0</h2>
<p>Go to the official java server faces page and there download the lastest version. <a href="http://java.sun.com/javaee/javaserverfaces/" target="_blank">http://java.sun.com/javaee/javaserverfaces/</a>. When I wrote this topic, the latest version was the mojarra-2.0.0-SNAPSHOT-binary version.<br />
Probably you&#8217;re going to download a .zip file. After extract it, copy the <em>jsf-api.jar</em> and <em>jsf-impl.jar</em> to the project/src/main/webapp/WEB-INF/lib.</p>
<h2>Modifying the XML</h2>
<p>We must change the web.xml file and tell to the project that we&#8217;re going to use JSF 2.0. To do that, simply edit the web.xml file and add the following entries:</p>

<div class="wp_syntax"><div class="code"><pre class="xml xml" style="font-family:monospace;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Faces Servlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.faces.webapp.FacesServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Faces Servlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>*.jsf<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>That&#8217;s all about setting up, but what about the faces-config.xml? That&#8217;s a good news, no longer the faces-config.xml is needed!!!<br />
Now we can register a managed bean through annotation and use convention over configuration for navigation. JSF 2.0 is much easier than its old version.</p>
<h2>Coding the project</h2>
<p>It&#8217;s time to have fun, let&#8217;s coding. But before get started, let&#8217;s understand our application. It is a simple application, it has 2 pages (principal and success) and one managed bean. The principal page has a form with one field. It calls the managed bean and then the success page is opened. Success page can open the principal page directly, no navigation rule is needed.</p>
<h3>Changing the current index.jsp file</h3>
<p>When we had used the webapp archetype to create the project for us, a index.jsp file had also been created. Edit it and put the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
  &lt;body&gt;
    &lt;jsp:forward page=&quot;principal.jsf&quot; /&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Now, let&#8217;s create a new file. Its name should be <u>principal.xhtml</u>. Here we&#8217;ve got a new feature. By default, JSF 2 has facelets as engine, therefore we&#8217;re using the xhtml extension. The content of the principal.xhtml is:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
      &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
      xmlns:h=&quot;http://java.sun.com/jsf/html&quot;
      xmlns:f=&quot;http://java.sun.com/jsf/core&quot;
      xmlns:ui=&quot;http://java.sun.com/jsf/facelets&quot;&gt;
&lt;h:body&gt;
    &lt;f:view&gt;
        &lt;h2&gt;&lt;h:outputText value=&quot;This is our first JSF 2.0 example&quot; /&gt;&lt;/h2&gt;
        &lt;h:form&gt;
        	Name: &lt;h:inputText value=&quot;#{helloWorld.name}&quot; /&gt;
        	&lt;h:commandButton action=&quot;#{helloWorld.doSomething}&quot; value=&quot;Something is gonna happen&quot; /&gt;
        &lt;/h:form&gt;        
    &lt;/f:view&gt;
&lt;/h:body&gt;
&lt;/html&gt;</pre></div></div>

<p>Note: We&#8217;ve got a new tag there!! <em>h:body</em>. JSF 2.0 brings new tags, such as: h:body, h:head, h:link, etc.<br />
Nothing special in the file above. We&#8217;ve got a simple form with one field. This field uses the managed bean helloWorld and it is binding with the name attribute. Now, let&#8217;s create/implement this ManagedBean.</p>
<h3>Creating the ManagedBean</h3>
<p>By default, the folder src/main/java is not created by this maven archetype (honestly, I have no idea why), therefore, let&#8217;s create it ourself.<br />
After that, create a HelloWorldMBean.java class with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.jairrillo.mbeans</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.bean.ManagedBean</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.bean.RequestScoped</span>;
&nbsp;
@ManagedBean<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;helloWorld&quot;</span><span style="color: #009900;">&#41;</span>
@RequestScoped
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorldMBean <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		name <span style="color: #339933;">=</span> name.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;success&quot;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<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> name;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<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;">name</span> <span style="color: #339933;">=</span> name;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Stop now, take a breath and let&#8217;s analyse three lines of the code above.<br />
First of all, look at the @ManagedBean annotation. This is the annotation responsible to register the MBean. If you have already used JSF 1.2 + Spring, this annotation is not new. Also, we have a @RequestScoped annotation. You can guess there are other annotation for the other different scopes as well.<br />
Check the return of the doSomthing method. It returns a String with the name success, which means the JSF 2.0, through convention over configuration, looks for a success.xhtml page and try to open it. That&#8217;s awesome, huh?</p>
<h3>Creating the success.xhtml</h3>
<p>Finally, create a success.xhtml file and put the content:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
      &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
      xmlns:h=&quot;http://java.sun.com/jsf/html&quot;
      xmlns:f=&quot;http://java.sun.com/jsf/core&quot;
      xmlns:ui=&quot;http://java.sun.com/jsf/facelets&quot;&gt;
&lt;h:body&gt;
    &lt;f:view&gt;
    	&lt;h2&gt;&lt;h:outputText value=&quot;Success page&quot;/&gt;&lt;/h2&gt;
    	&lt;h:outputText value=&quot;Your name in uppercase is: &quot;/&gt;
    	&lt;h:outputText value=&quot;#{helloWorld.name}&quot; /&gt;&lt;br /&gt;
    	&lt;h:link value=&quot;Back do Principal&quot; outcome=&quot;principal&quot; /&gt;
    &lt;/f:view&gt;
&lt;/h:body&gt;
&lt;/html&gt;</pre></div></div>

<p>Stop again. What does h:link do? It&#8217;s another new feature from JSF 2.0. Different than its version 1, that all requests were POST, with JSF 2 we can use GET instead. h:link creates a simple a href on the page.</p>
<h2>Running the application</h2>
<p>If you reach this point, you will be able to run the application. I have tested in Tomcat 6 and Glassfish v2 and worked in both.</p>
<h2>Conclusion</h2>
<p>Certainly JSF 2 is easier than version 1. The JCP learned with the mistakes from version 1 and created a new version better. No doubt that this new version is gonna be more productive and funny. </p>
<p>This topic ends up here. You can download the First Example project <a href="http://www.jairrillo.com/downloads/JSF2FirstExample.zip">HERE</a>.<br />
If you have any comment/question or feedback, leave your message below.</p>
<p>Thanks guys.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jairrillo.com/blog/2009/10/02/getting-starting-with-jsf-20-a-first-example/feed/</wfw:commentRss>
		</item>
		<item>
		<title>(pt-BR) Ubuntu - Configuração do GRUB</title>
		<link>http://www.jairrillo.com/blog/2009/08/21/pt-br-ubuntu-configuracao-do-grub/</link>
		<comments>http://www.jairrillo.com/blog/2009/08/21/pt-br-ubuntu-configuracao-do-grub/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 17:36:31 +0000</pubDate>
		<dc:creator>Jair Rillo Junior</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[GRUB]]></category>

		<guid isPermaLink="false">http://www.jairrillo.com/blog/?p=675</guid>
		<description><![CDATA[Se você, assim como eu, utiliza dual-boot em sua máquina (provavelmente Linux e Windows) certamente deve usar o GRUB para o gerenciamento do boot.
O GRUB funciona muito bem, porém na sua configuração default, a cada atualização do Kernel do Linux uma nova opção de boot aparece na inicialização do computador. Isso acaba sendo inviável quando [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="" src="http://www.gnu.org/graphics/gnu-head-sm.jpg" align="left" class="alignnone" width="129" height="122" />Se você, assim como eu, utiliza dual-boot em sua máquina (provavelmente Linux e Windows) certamente deve usar o <a href="http://www.gnu.org/software/grub/" target="_blank">GRUB</a> para o gerenciamento do boot.<br />
O GRUB funciona muito bem, porém na sua configuração default, a cada atualização do Kernel do Linux uma nova opção de boot aparece na inicialização do computador. Isso acaba sendo inviável quando você tem várias atualizações.<br />
Uma forma de &#8220;remover&#8221; as atualizações antigas é através da configuração do GRUB. A maneira mais fácil é editar diretamente o arquivo <strong>menu.lst</strong> que fica no diretório <u>boot/grub</u>. A dica foi retirada do seguinte post: <a href="http://brazovsky.blogspot.com/2007/04/configurar-o-grub-no-ubuntu.html" target="_blank">http://brazovsky.blogspot.com/2007/04/configurar-o-grub-no-ubuntu.html</a></p>
<h2>Fazendo o backup</h2>
<p>A primeira coisa que devemos fazer é um backup do menu.lst original. Para isso, digite na shell o seguinte comando:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #000000; font-weight: bold;">/</span>boot<span style="color: #000000; font-weight: bold;">/</span>grub<span style="color: #000000; font-weight: bold;">/</span>menu.lst <span style="color: #000000; font-weight: bold;">/</span>boot<span style="color: #000000; font-weight: bold;">/</span>grub<span style="color: #000000; font-weight: bold;">/</span>menu.lst_backup</pre></div></div>

<h2>Alterando as opções do GRUB</h2>
<p>Após o backup, vamos editar o arquivo através do comando:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> gedit <span style="color: #000000; font-weight: bold;">/</span>boot<span style="color: #000000; font-weight: bold;">/</span>grub<span style="color: #000000; font-weight: bold;">/</span>menu.lst</pre></div></div>

<p>Com o arquivo aberto, vá até o fim do mesmo. Lá iremos encontrar, após a sessão &#8220;End Default Options&#8221;, as opções do GRUB. Podemos apagar todas opções e deixarmos apenas a última, conforme exemplo abaixo:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">## ## End Default Options ##</span>
&nbsp;
title		Ubuntu <span style="color: #000000;">9.04</span>, kernel 2.6.28-<span style="color: #000000;">15</span>-generic
uuid		6e8438cb-be6c-42bb-<span style="color: #000000;">8898</span>-157d69148f51
kernel		<span style="color: #000000; font-weight: bold;">/</span>boot<span style="color: #000000; font-weight: bold;">/</span>vmlinuz-2.6.28-<span style="color: #000000;">15</span>-generic <span style="color: #007800;">root</span>=<span style="color: #007800;">UUID</span>=6e8438cb-be6c-42bb-<span style="color: #000000;">8898</span>-157d69148f51 ro quiet splash 
initrd		<span style="color: #000000; font-weight: bold;">/</span>boot<span style="color: #000000; font-weight: bold;">/</span>initrd.img-2.6.28-<span style="color: #000000;">15</span>-generic
quiet
&nbsp;
title		Ubuntu <span style="color: #000000;">9.04</span>, kernel 2.6.28-<span style="color: #000000;">15</span>-generic <span style="color: #7a0874; font-weight: bold;">&#40;</span>recovery mode<span style="color: #7a0874; font-weight: bold;">&#41;</span>
uuid		6e8438cb-be6c-42bb-<span style="color: #000000;">8898</span>-157d69148f51
kernel		<span style="color: #000000; font-weight: bold;">/</span>boot<span style="color: #000000; font-weight: bold;">/</span>vmlinuz-2.6.28-<span style="color: #000000;">15</span>-generic <span style="color: #007800;">root</span>=<span style="color: #007800;">UUID</span>=6e8438cb-be6c-42bb-<span style="color: #000000;">8898</span>-157d69148f51 ro  single
initrd		<span style="color: #000000; font-weight: bold;">/</span>boot<span style="color: #000000; font-weight: bold;">/</span>initrd.img-2.6.28-<span style="color: #000000;">15</span>-generic
&nbsp;
title		Ubuntu <span style="color: #000000;">9.04</span>, memtest86+
uuid		6e8438cb-be6c-42bb-<span style="color: #000000;">8898</span>-157d69148f51
kernel		<span style="color: #000000; font-weight: bold;">/</span>boot<span style="color: #000000; font-weight: bold;">/</span>memtest86+.bin
quiet
&nbsp;
<span style="color: #666666; font-style: italic;">### END DEBIAN AUTOMAGIC KERNELS LIST</span></pre></div></div>

<p>Pode salvar o arquivo e pronto, na próxima inicialização apenas 1 opção do Linux será visível. </p>
<h2>Outras opções importantes</h2>
<p>Como você pode ver nesse arquivo, existem várias opções que podemos configurar. Dentre elas vou destacar três que eu julgo importantes.<br />
<strong>howmany</strong>: Por padrão ela vem comentada, porém você pode tirar o comentário e configurar quantas versão visíveis vão aparecer no GRUB. Eu sempre deixo 2, pois após a atualização do Kernel, se der algum problema no Kernel novo, eu tenho ainda a opção de voltar para o Kernel antigo.<br />
<strong>default</strong>: Padrão é 0. Isso significa que o Linux será o padrão na tela do GRUB. Você pode mudar esse valor. Por exemplo: você deseja que a partição default seja o Windows, portanto, altere esse valor para 1.<br />
<strong>timeout</strong>: Padrão é 10, ou seja, 10 segundos que o GRUB vai esperar até ele executar a opção padrão.</p>
<p>Bom pessoal, essa é uma dica simples porém útil para quem trabalha com dual-boot na máquina. Qualquer dúvida ou comentário, podem deixar sua mensagem abaixo. Até mais</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jairrillo.com/blog/2009/08/21/pt-br-ubuntu-configuracao-do-grub/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Sun VirtualBox: Problema após mudança de Kernel</title>
		<link>http://www.jairrillo.com/blog/2009/08/19/sun-virtualbox-problema-apos-mudanca-de-kernel/</link>
		<comments>http://www.jairrillo.com/blog/2009/08/19/sun-virtualbox-problema-apos-mudanca-de-kernel/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 18:12:34 +0000</pubDate>
		<dc:creator>Jair Rillo Junior</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[Ubuntu]]></category>

		<category><![CDATA[VirtualBox]]></category>

		<guid isPermaLink="false">http://www.jairrillo.com/blog/?p=669</guid>
		<description><![CDATA[Assim como a VMWare, o Sun VirtualBox também gera problemas após mudança de Kernel do Linux. Na verdade, após a mudança do Kernel, suas máquinas virtuais não irão mais funcionar.
Felizmente o processo para arrumar isso é simples. Basicamente você precisa reinstalar o VirtualBox. Nesse caso existem duas opções:

Fazer novamente o download do VirtualBox, clicar duas [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone" src="http://www.virtualbox.org/graphics/vbox_logo2_gradient.png" align="left" />Assim como a VMWare, o <a href="http://www.virtualbox.org/" target="_blank">Sun VirtualBox</a> também gera problemas após mudança de Kernel do Linux. Na verdade, após a mudança do Kernel, suas máquinas virtuais não irão mais funcionar.<br />
Felizmente o processo para arrumar isso é simples. Basicamente você precisa reinstalar o VirtualBox. Nesse caso existem duas opções:</p>
<ol>
<li>Fazer novamente o download do VirtualBox, clicar duas vezes no arquivo .deb (se você estiver usando uma versão debian-like) e clicar no botão <strong>Reinstalar Pacote</strong>.<br />
<a href="http://www.jairrillo.com/blog/wp-content/uploads/2009/08/virtualbox.png" target="_blank"><img src="http://www.jairrillo.com/blog/wp-content/uploads/2009/08/virtualbox-300x218.png" alt="" title="virtualbox" width="300" height="218" class="alignnone size-medium wp-image-670" /></a></li>
<li>Usar o comando abaixo para reconfigurar o pacote.

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> dpkg-reconfigure virtualbox-<span style="color: #000000;">3.0</span></pre></div></div>

</li>
</ol>
<p>Após isso, pode usufruir novamente das suas máquinas virtuals.</p>
<p>Espero que esse post curto seja útil.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jairrillo.com/blog/2009/08/19/sun-virtualbox-problema-apos-mudanca-de-kernel/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
