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

<channel>
	<title>Pakzilla &#187; JPA</title>
	<atom:link href="http://www.pakzilla.com/tag/jpa/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pakzilla.com</link>
	<description>A blog on Programming, Web and Technology</description>
	<lastBuildDate>Sat, 04 Feb 2012 22:25:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Spoken on Google App Engine at a conference in UET</title>
		<link>http://www.pakzilla.com/2009/12/19/spoken-on-google-app-engine-at-a-conference-in-uet/</link>
		<comments>http://www.pakzilla.com/2009/12/19/spoken-on-google-app-engine-at-a-conference-in-uet/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 13:31:34 +0000</pubDate>
		<dc:creator>Tahir Akram</dc:creator>
				<category><![CDATA[Pakistan]]></category>
		<category><![CDATA[Porgramming]]></category>
		<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[UET]]></category>

		<guid isPermaLink="false">http://www.pakzilla.com/?p=728</guid>
		<description><![CDATA[Today I had delivered a workshop on Developing Java Web Applications In Google App Engine. This workshop was the part of events of 3rd International Conference on Open Source Systems and Technologies. It was organized by Al-Khawarizmi Institute of Computer Science , University of Engineering and Technology, Lahore. I have presented following slides there. Also [...]
Related posts:<ol>
<li><a href='http://www.pakzilla.com/2009/04/09/java-in-google-app-engine-whats-new-for-developers/' rel='bookmark' title='Java in Google App Engine, what&#8217;s new for developers'>Java in Google App Engine, what&#8217;s new for developers</a></li>
<li><a href='http://www.pakzilla.com/2009/10/03/how-to-avoid-deadlineexceededexception-in-google-app-engine/' rel='bookmark' title='How to avoid DeadlineExceededException in Google App Engine'>How to avoid DeadlineExceededException in Google App Engine</a></li>
<li><a href='http://www.pakzilla.com/2010/01/12/useful-cheat-sheets-on-cloud-computing-and-google-app-engine-for-java/' rel='bookmark' title='Useful cheat sheets on Cloud Computing and Google App Engine for Java'>Useful cheat sheets on Cloud Computing and Google App Engine for Java</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Today I had delivered a workshop on <a title="Developing Java Web Applications In Google App Engine" href="http://www.slideshare.net/tahirakram/developing-java-web-applications-in-google-app-engine-2749092">Developing Java Web Applications In Google App Engine</a>. This workshop was the part of events of <a href="http://www.uet.edu.pk/Conferences/icosst2009/_workshop.html">3rd International Conference on Open Source Systems and Technologies</a>. It was organized by <a href="http://www.kics.edu.pk/">Al-Khawarizmi Institute of Computer Science</a> , <a href="http://www.uet.edu.pk">University of Engineering and Technology</a>, Lahore.</p>
<p>I have presented following slides there. Also coded a basic sample application to show how to create and deploy new Java project on GAE. Which can send emails, how to use authentication by Google, and how to do database interaction. It was a very interesting topic for me to present. Participants also liked it.</p>
<div style="width:425px" id="__ss_2749092"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/tahirakram/developing-java-web-applications-in-google-app-engine-2749092" title="Developing Java Web Applications In Google App Engine">Developing Java Web Applications In Google App Engine</a></strong><object id="__sse2749092" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=developingjavabasedwebapplicationsingoogleapp-091219065103-phpapp01&#038;stripped_title=developing-java-web-applications-in-google-app-engine-2749092" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse2749092" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=developingjavabasedwebapplicationsingoogleapp-091219065103-phpapp01&#038;stripped_title=developing-java-web-applications-in-google-app-engine-2749092" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="padding:5px 0 12px">View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/tahirakram">Tahir Akram</a>.</div>
</div>
<p>Comments and feedback is welcomed on the presentation material from you guys.</p>
<p>Related posts:<ol>
<li><a href='http://www.pakzilla.com/2009/04/09/java-in-google-app-engine-whats-new-for-developers/' rel='bookmark' title='Java in Google App Engine, what&#8217;s new for developers'>Java in Google App Engine, what&#8217;s new for developers</a></li>
<li><a href='http://www.pakzilla.com/2009/10/03/how-to-avoid-deadlineexceededexception-in-google-app-engine/' rel='bookmark' title='How to avoid DeadlineExceededException in Google App Engine'>How to avoid DeadlineExceededException in Google App Engine</a></li>
<li><a href='http://www.pakzilla.com/2010/01/12/useful-cheat-sheets-on-cloud-computing-and-google-app-engine-for-java/' rel='bookmark' title='Useful cheat sheets on Cloud Computing and Google App Engine for Java'>Useful cheat sheets on Cloud Computing and Google App Engine for Java</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.pakzilla.com/2009/12/19/spoken-on-google-app-engine-at-a-conference-in-uet/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Tutorial: JPA on Google App Engine</title>
		<link>http://www.pakzilla.com/2009/12/13/tutorial-jpa-on-google-app-engine/</link>
		<comments>http://www.pakzilla.com/2009/12/13/tutorial-jpa-on-google-app-engine/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 15:05:58 +0000</pubDate>
		<dc:creator>Tahir Akram</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JPA]]></category>

		<guid isPermaLink="false">http://www.pakzilla.com/?p=723</guid>
		<description><![CDATA[This project will create and delete an entity record. I have chosen JPA for datastore in app engine. You can also look JDO for this purpose. But my choice to go for JPA is due to Sun standardization on it. I am using App Engine SDK version 1.2.18 Project name: engineplay URL: http://engineplay.appspot.com [ad#co-2] Project [...]
Related posts:<ol>
<li><a href='http://www.pakzilla.com/2009/12/07/upgrading-to-google-app-engine-java-sdk-1-2-8/' rel='bookmark' title='Upgrading to Google App Engine Java SDK 1.2.8'>Upgrading to Google App Engine Java SDK 1.2.8</a></li>
<li><a href='http://www.pakzilla.com/2009/12/01/determine-whether-application-is-running-on-local-server-or-app-engine/' rel='bookmark' title='Determine whether application is running on local server or App Engine'>Determine whether application is running on local server or App Engine</a></li>
<li><a href='http://www.pakzilla.com/2009/10/03/tutorial-java-based-twitter-app-on-google-app-engine/' rel='bookmark' title='Tutorial: Java based Twitter App on Google App Engine'>Tutorial: Java based Twitter App on Google App Engine</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>This project will create and delete an entity record. I have chosen JPA for datastore in app engine. You can also look JDO for this purpose. But my choice to go for JPA is due to Sun standardization on it.</p>
<p>I am using App Engine SDK version 1.2.18</p>
<p>Project name: engineplay<br />
URL: <a href="http://engineplay.appspot.com">http://engineplay.appspot.com</a><br />
[ad#co-2]</p>
<p><strong>Project structure:</strong></p>
<p><img class="aligncenter size-full wp-image-717" title="jpa_gae_project_structure" src="http://www.pakzilla.com/wp-content/upload/jpa_gae_project_structure.png" alt="jpa_gae_project_structure" width="279" height="530" /><strong>UserPrefs.java:</strong> The entity class</p>
<pre class="brush: java; title: ; notranslate">
package com.engineplay.datastore.pojos;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import com.engineplay.datastore.EMFService;
import com.google.appengine.api.users.User;

@Entity(name = &quot;UserPrefs&quot;)
public class UserPrefs {
 @Id
 private String userId;
 private int donuts;
 @Basic
 private User user;

 public UserPrefs(String userId) {
 this.userId = userId;
 }

 /* TODO create getter and setters for above properties yourself */

 public static UserPrefs getPrefsForUser(User user) {
 UserPrefs userPrefs = null;
 EntityManager em = EMFService.get().createEntityManager();
 try {
 if (em.find(UserPrefs.class, user.getEmail()) == null) {

 userPrefs = new UserPrefs(user.getEmail());
 userPrefs.setUser(user);

 } else {
 userPrefs = em.find(UserPrefs.class, user.getEmail());
 }
 } finally {
 em.close();
 }
 return userPrefs;
 }

 public void save() {
 EntityManager em = EMFService.get().createEntityManager();
 try {
 em.persist(this);
 } finally {
 em.close();
 }
 }

 public void remove() {
 EntityManager em = EMFService.get().createEntityManager();
 try {
 UserPrefs userPrefs = em.find(UserPrefs.class, user.getEmail());
 em.remove(userPrefs);
 } finally {
 em.close();
 }
 }
}
</pre>
<p><strong>EMFService.java: </strong>The class to create EntityManagerFactory instance.</p>
<pre class="brush: java; title: ; notranslate">
package com.engineplay.datastore;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class EMFService {
 private static final EntityManagerFactory emfInstance = Persistence
 .createEntityManagerFactory(&quot;transactions-optional&quot;);

 private EMFService() {
 }

 public static EntityManagerFactory get() {
 return emfInstance;
 }
}
</pre>
<p><strong>LoginServlet.java</strong></p>
<pre class="brush: java; title: ; notranslate">
package com.engineplay.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.engineplay.datastore.pojos.UserPrefs;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class LoginServlet extends HttpServlet {

 public void doGet(HttpServletRequest req, HttpServletResponse resp)
 throws IOException {

 UserService userService = UserServiceFactory.getUserService();
 User user = userService.getCurrentUser();
 String navBar, form = null;
 int donuts = 0;
 if (user == null) {
 navBar = &quot;&lt;p&gt;Welcome! &lt;a href=\&quot;&quot; + userService.createLoginURL(&quot;/&quot;) +
 &quot;\&quot;&gt;Sign in or register&lt;/a&gt; to customize.&lt;/p&gt;&quot;;
 form = &quot;&quot;;
 } else {
 UserPrefs userPrefs = UserPrefs.getPrefsForUser(user);

 if (userPrefs != null) {
 donuts = userPrefs.getDonuts();
 }
 navBar = &quot;&lt;p&gt;Welcome, &quot; + user.getEmail() + &quot;! You can &lt;a href=\&quot;&quot; +
 userService.createLogoutURL(&quot;/&quot;) +
 &quot;\&quot;&gt;sign out&lt;/a&gt;.&lt;/p&gt;&quot;;
 form = &quot;&lt;form action=\&quot;/donuts\&quot; method=\&quot;post\&quot;&gt;&quot; +
 &quot;&lt;label for=\&quot;donuts\&quot;&gt;&quot; +
 &quot;Need more donuts?:&quot; +
 &quot;&lt;/label&gt;&quot; +
 &quot;&lt;input name=\&quot;donuts\&quot; id=\&quot;donuts\&quot; type=\&quot;text\&quot; size=\&quot;4\&quot; /&gt;&quot; +
 &quot; &lt;input name=\&quot;olddonuts\&quot;  type=\&quot;hidden\&quot; value=\&quot;&quot; + donuts + &quot;\&quot; /&gt; &quot; +
 &quot; &lt;input name=\&quot;userId\&quot;  type=\&quot;hidden\&quot; value=\&quot;&quot; +user.getEmail()+ &quot;\&quot; /&gt; &quot; +
 &quot;   &lt;input type=\&quot;submit\&quot; value=\&quot;  ADD  \&quot; /&gt;&lt;br&gt;&lt;input type=\&quot;submit\&quot;  name =\&quot;deleteBtn\&quot; value=\&quot;  DELETE ME!  \&quot; /&gt;&quot; +
 &quot;&lt;/form&gt;&quot;;

 }
 resp.setContentType(&quot;text/html&quot;);
 PrintWriter out = resp.getWriter();
 out.print(&quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;Engine Play - Donuts&lt;/title&lt;/head&gt;&lt;body&gt;&quot;);
 out.println(navBar);
 if (donuts != 0)
 out.println(&quot;&lt;p&gt;Donuts you have: &quot; + donuts + &quot;&lt;/p&gt;&quot;);
 else
 out.println(&quot;&lt;p&gt; No donuts you have <img src='http://www.pakzilla.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  &lt;/p&gt;&quot;);
 out.println(form);
 out.print(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);
 }
}
</pre>
<p><strong>DonutsServlet.java</strong></p>
<pre class="brush: java; title: ; notranslate">
package com.engineplay.servlet;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.engineplay.datastore.pojos.UserPrefs;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class DonutsServlet extends HttpServlet {

 public void doPost(HttpServletRequest req, HttpServletResponse resp)
 throws IOException {
 UserService userService = UserServiceFactory.getUserService();
 User user = userService.getCurrentUser();
 UserPrefs userPrefs = UserPrefs.getPrefsForUser(user);
 try {

 String deleteOpt = req.getParameter(&quot;deleteBtn&quot;);
 if (deleteOpt == null){
 int donuts = new Integer(req.getParameter(&quot;donuts&quot;)).intValue();
 int oldDonuts = new Integer(req.getParameter(&quot;olddonuts&quot;)).intValue();

 userPrefs.setDonuts(donuts + oldDonuts);
 userPrefs.save();
 }else{
 String userId = req.getParameter(&quot;userId&quot;);
 userPrefs.setUserId(userId);
 userPrefs.remove();
 }
 } catch (Exception e) {
 e.printStackTrace();
 }
 resp.sendRedirect(&quot;/Login&quot;);
 }
}
</pre>
<p><strong>persistence.xml:</strong> Configuration file required for JPA</p>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;persistence xmlns=&quot;http://java.sun.com/xml/ns/persistence&quot;
 xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
 xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/persistence

http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd&quot;

 version=&quot;1.0&quot;&gt;
 &lt;persistence-unit name=&quot;transactions-optional&quot;&gt;
 &lt;provider&gt;
 org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider
 &lt;/provider&gt;
 &lt;properties&gt;
 &lt;property name=&quot;datanucleus.NontransactionalRead&quot;
 value=&quot;true&quot; /&gt;
 &lt;property name=&quot;datanucleus.NontransactionalWrite&quot;
 value=&quot;true&quot; /&gt;
 &lt;property name=&quot;datanucleus.ConnectionURL&quot;
 value=&quot;appengine&quot; /&gt;
 &lt;/properties&gt;
 &lt;/persistence-unit&gt;
&lt;/persistence&gt;
</pre>
<p><strong>web.xml</strong></p>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;!DOCTYPE web-app PUBLIC
 &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quot;
 &quot;http://java.sun.com/dtd/web-app_2_3.dtd&quot;&gt;

&lt;web-app xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot; version=&quot;2.5&quot;&gt;

 &lt;servlet&gt;
 &lt;servlet-name&gt;LoginServlet&lt;/servlet-name&gt;
 &lt;servlet-class&gt;com.engineplay.servlet.LoginServlet&lt;/servlet-class&gt;
 &lt;/servlet&gt;
 &lt;servlet&gt;
 &lt;servlet-name&gt;DonutsServlet&lt;/servlet-name&gt;
 &lt;servlet-class&gt;com.engineplay.servlet.DonutsServlet&lt;/servlet-class&gt;
 &lt;/servlet&gt;

 &lt;servlet-mapping&gt;
 &lt;servlet-name&gt;LoginServlet&lt;/servlet-name&gt;
 &lt;url-pattern&gt;/Login&lt;/url-pattern&gt;
 &lt;/servlet-mapping&gt;
 &lt;servlet-mapping&gt;
 &lt;servlet-name&gt;DonutsServlet&lt;/servlet-name&gt;
 &lt;url-pattern&gt;/donuts&lt;/url-pattern&gt;
 &lt;/servlet-mapping&gt;

 &lt;welcome-file-list&gt;
 &lt;welcome-file&gt;Login&lt;/welcome-file&gt;
 &lt;/welcome-file-list&gt;
&lt;/web-app&gt;
</pre>
<p>Access the project here: <a href="http://engineplay.appspot.com">http://engineplay.appspot.com</a></p>
<p><a href="http://engineplay.appspot.com"><img class="aligncenter size-full wp-image-718" title="run_gae_project" src="http://www.pakzilla.com/wp-content/upload/run_gae_project.png" alt="run_gae_project" width="562" height="281" /></a></p>
<p>Related posts:<ol>
<li><a href='http://www.pakzilla.com/2009/12/07/upgrading-to-google-app-engine-java-sdk-1-2-8/' rel='bookmark' title='Upgrading to Google App Engine Java SDK 1.2.8'>Upgrading to Google App Engine Java SDK 1.2.8</a></li>
<li><a href='http://www.pakzilla.com/2009/12/01/determine-whether-application-is-running-on-local-server-or-app-engine/' rel='bookmark' title='Determine whether application is running on local server or App Engine'>Determine whether application is running on local server or App Engine</a></li>
<li><a href='http://www.pakzilla.com/2009/10/03/tutorial-java-based-twitter-app-on-google-app-engine/' rel='bookmark' title='Tutorial: Java based Twitter App on Google App Engine'>Tutorial: Java based Twitter App on Google App Engine</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.pakzilla.com/2009/12/13/tutorial-jpa-on-google-app-engine/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

