Author: manaRH
Date: 2012-09-06 10:56:12 -0400 (Thu, 06 Sep 2012)
New Revision: 15120
Modified:
branches/community/Seam_2_3/examples-ee6/jpa/jpa-tests/src/test/java/org/jboss/seam/example/jpa/test/BookingTest.java
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/java/org/jboss/seam/example/jpa/AuthenticatorAction.java
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/java/org/jboss/seam/example/jpa/BookingListAction.java
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/java/org/jboss/seam/example/jpa/RegisterAction.java
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/resources/META-INF/persistence.xml
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/home.xhtml
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/main.xhtml
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/register.xhtml
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/template.xhtml
Log:
JBSEAM-4876 using JPA 2 criteriaAPI and getSingleResult in JPA Booking
Modified:
branches/community/Seam_2_3/examples-ee6/jpa/jpa-tests/src/test/java/org/jboss/seam/example/jpa/test/BookingTest.java
===================================================================
---
branches/community/Seam_2_3/examples-ee6/jpa/jpa-tests/src/test/java/org/jboss/seam/example/jpa/test/BookingTest.java 2012-09-05
13:52:16 UTC (rev 15119)
+++
branches/community/Seam_2_3/examples-ee6/jpa/jpa-tests/src/test/java/org/jboss/seam/example/jpa/test/BookingTest.java 2012-09-06
14:56:12 UTC (rev 15120)
@@ -13,6 +13,7 @@
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.OverProtocol;
import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.seam.Component;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.core.Manager;
import org.jboss.seam.example.jpa.Booking;
@@ -24,6 +25,7 @@
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -70,17 +72,17 @@
@Override
protected void invokeApplication()
{
- assert invokeAction("#{hotelSearch.find}")==null;
+ Assert.assertNull(invokeAction("#{hotelSearch.find}"));
}
@Override
protected void renderResponse()
{
DataModel hotels = (DataModel)
Contexts.getSessionContext().get("hotels");
- assert hotels.getRowCount()==1;
- assert ( (Hotel) hotels.getRowData() ).getCity().equals("NY");
- assert getValue("#{hotelSearch.searchString}").equals("Union
Square");
- assert !Manager.instance().isLongRunningConversation();
+ Assert.assertEquals(1, hotels.getRowCount());
+ Assert.assertEquals("NY",( (Hotel) hotels.getRowData() ).getCity()
);
+ Assert.assertEquals("Union Square",
getValue("#{hotelSearch.searchString}"));
+ Assert.assertTrue(!Manager.instance().isLongRunningConversation());
}
}.run();
@@ -91,7 +93,7 @@
protected void invokeApplication() throws Exception {
HotelBookingAction hotelBooking = (HotelBookingAction)
getInstance("hotelBooking");
DataModel hotels = (DataModel)
Contexts.getSessionContext().get("hotels");
- assert hotels.getRowCount()==1;
+ Assert.assertEquals(1, hotels.getRowCount());
hotelBooking.selectHotel( (Hotel) hotels.getRowData() );
}
@@ -99,9 +101,9 @@
protected void renderResponse()
{
Hotel hotel = (Hotel)
Contexts.getConversationContext().get("hotel");
- assert hotel.getCity().equals("NY");
- assert hotel.getZip().equals("10011");
- assert Manager.instance().isLongRunningConversation();
+ Assert.assertEquals("NY",hotel.getCity() );
+ Assert.assertEquals("10011",hotel.getZip() );
+ Assert.assertTrue(Manager.instance().isLongRunningConversation());
}
}.run();
@@ -117,14 +119,14 @@
@Override
protected void renderResponse()
{
- assert getValue("#{booking.user}")!=null;
- assert getValue("#{booking.hotel}")!=null;
- assert getValue("#{booking.creditCard}")==null;
- assert getValue("#{booking.creditCardName}")==null;
+ Assert.assertNotNull(getValue("#{booking.user}"));
+ Assert.assertNotNull(getValue("#{booking.hotel}"));
+ Assert.assertNull(getValue("#{booking.creditCard}"));
+ Assert.assertNull(getValue("#{booking.creditCardName}"));
Booking booking = (Booking)
Contexts.getConversationContext().get("booking");
- assert
booking.getHotel()==Contexts.getConversationContext().get("hotel");
- assert
booking.getUser()==Contexts.getSessionContext().get("user");
- assert Manager.instance().isLongRunningConversation();
+
Assert.assertTrue(booking.getHotel()==Contexts.getConversationContext().get("hotel"));
+
Assert.assertTrue(booking.getUser()==Contexts.getConversationContext().get("user"));
+ Assert.assertTrue(Manager.instance().isLongRunningConversation());
}
}.run();
@@ -135,23 +137,23 @@
protected void processValidations() throws Exception
{
validateValue("#{booking.creditCard}", "123");
- assert isValidationFailure();
+ Assert.assertTrue(isValidationFailure());
}
@Override
protected void renderResponse()
{
Iterator messages = FacesContext.getCurrentInstance().getMessages();
- assert messages.hasNext();
- assert ( (FacesMessage) messages.next() ).getSummary().equals("Credit
card number must 16 digits long");
- assert !messages.hasNext();
- assert Manager.instance().isLongRunningConversation();
+ Assert.assertTrue(messages.hasNext());
+ Assert.assertEquals("Credit card number must 16 digits long", (
(FacesMessage) messages.next() ).getSummary());
+ Assert.assertFalse(messages.hasNext());
+ Assert.assertTrue(Manager.instance().isLongRunningConversation());
}
@Override
protected void afterRequest()
{
- assert !isInvokeApplicationBegun();
+ Assert.assertTrue(!isInvokeApplicationBegun());
}
}.run();
@@ -162,23 +164,23 @@
protected void processValidations() throws Exception
{
validateValue("#{booking.creditCardName}", "");
- assert isValidationFailure();
+ Assert.assertTrue(isValidationFailure());
}
@Override
protected void renderResponse()
{
Iterator messages = FacesContext.getCurrentInstance().getMessages();
- assert messages.hasNext();
- assert ( (FacesMessage) messages.next() ).getSummary().equals("Credit
card name is required");
- assert !messages.hasNext();
- assert Manager.instance().isLongRunningConversation();
+ Assert.assertTrue(messages.hasNext());
+ Assert.assertEquals("Credit card name is required", (
(FacesMessage) messages.next() ).getSummary());
+ Assert.assertFalse(messages.hasNext());
+ Assert.assertTrue(Manager.instance().isLongRunningConversation());
}
@Override
protected void afterRequest()
{
- assert !isInvokeApplicationBegun();
+ Assert.assertFalse(isInvokeApplicationBegun());
}
}.run();
@@ -206,11 +208,12 @@
protected void renderResponse()
{
Iterator messages = FacesContext.getCurrentInstance().getMessages();
- assert messages.hasNext();
+ Assert.assertTrue(Manager.instance().isLongRunningConversation());
+ Assert.assertTrue(messages.hasNext());
FacesMessage message = (FacesMessage) messages.next();
- assert message.getSummary().equals("Check out date must be later than
check in date");
- assert !messages.hasNext();
- assert Manager.instance().isLongRunningConversation();
+ Assert.assertEquals("Check out date must be later than check in
date",message.getSummary());
+ Assert.assertFalse(messages.hasNext());
+ Assert.assertTrue(Manager.instance().isLongRunningConversation());
}
@Override
@@ -240,13 +243,13 @@
@Override
protected void renderResponse()
{
- assert Manager.instance().isLongRunningConversation();
+ Assert.assertTrue(Manager.instance().isLongRunningConversation());
}
@Override
protected void afterRequest()
{
- assert isInvokeApplicationComplete();
+ Assert.assertTrue( isInvokeApplicationComplete() );
}
}.run();
@@ -262,7 +265,7 @@
@Override
protected void afterRequest()
{
- assert isInvokeApplicationComplete();
+ Assert.assertTrue( isInvokeApplicationComplete() );
}
}.run();
@@ -273,12 +276,12 @@
protected void renderResponse()
{
ListDataModel bookings = (ListDataModel) getInstance("bookings");
- assert bookings.getRowCount()==1;
+ Assert.assertEquals(1, bookings.getRowCount());
bookings.setRowIndex(0);
Booking booking = (Booking) bookings.getRowData();
- assert booking.getHotel().getCity().equals("NY");
- assert booking.getUser().getUsername().equals("gavin");
- assert !Manager.instance().isLongRunningConversation();
+ Assert.assertEquals("NY", booking.getHotel().getCity());
+ Assert.assertEquals("gavin", booking.getUser().getUsername());
+ Assert.assertFalse(Manager.instance().isLongRunningConversation());
}
}.run();
@@ -297,8 +300,8 @@
protected void renderResponse()
{
ListDataModel bookings = (ListDataModel)
Contexts.getSessionContext().get("bookings");
- assert bookings.getRowCount()==0;
- assert !Manager.instance().isLongRunningConversation();
+ Assert.assertEquals(0, bookings.getRowCount());
+ Assert.assertFalse(Manager.instance().isLongRunningConversation());
}
}.run();
Modified:
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/java/org/jboss/seam/example/jpa/AuthenticatorAction.java
===================================================================
---
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/java/org/jboss/seam/example/jpa/AuthenticatorAction.java 2012-09-05
13:52:16 UTC (rev 15119)
+++
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/java/org/jboss/seam/example/jpa/AuthenticatorAction.java 2012-09-06
14:56:12 UTC (rev 15120)
@@ -2,13 +2,13 @@
import static org.jboss.seam.ScopeType.SESSION;
-import java.util.List;
-
import javax.persistence.EntityManager;
+import javax.persistence.NoResultException;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
+import org.jboss.seam.faces.FacesMessages;
@Name("authenticator")
public class AuthenticatorAction
@@ -20,18 +20,18 @@
public boolean authenticate()
{
- List results = em.createQuery("select u from User u where
u.username=#{identity.username} and u.password=#{identity.password}")
- .getResultList();
-
- if ( results.size()==0 )
+ try
{
- return false;
+ user = (User) em.createQuery("select u from User u where
u.username=#{identity.username} and u.password=#{identity.password}")
+ .getSingleResult();
+ return true;
}
- else
+ catch (NoResultException e)
{
- user = (User) results.get(0);
- return true;
+ FacesMessages.instance().add("Authentication failed");
+ return false;
}
+
}
}
Modified:
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/java/org/jboss/seam/example/jpa/BookingListAction.java
===================================================================
---
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/java/org/jboss/seam/example/jpa/BookingListAction.java 2012-09-05
13:52:16 UTC (rev 15119)
+++
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/java/org/jboss/seam/example/jpa/BookingListAction.java 2012-09-06
14:56:12 UTC (rev 15120)
@@ -8,6 +8,9 @@
import java.util.List;
import javax.persistence.EntityManager;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Root;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
@@ -47,9 +50,20 @@
// @Transactional
public void getBookings()
{
- bookings = em.createQuery("select b from Booking b where b.user.username =
:username order by b.checkinDate")
- .setParameter("username", user.getUsername())
- .getResultList();
+// JPA 1.0 way
+// bookings = em.createQuery("select b from Booking b where b.user.username =
:username order by b.checkinDate")
+// .setParameter("username", user.getUsername())
+// .getResultList();
+
+ //JPA 2.0 way - using new CriteriaBuilder API for dynamic query execution
+ //this easily check the typos in comparison to usage of string query
+ CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
+ CriteriaQuery<Booking> query = criteriaBuilder.createQuery(Booking.class);
+ Root<Booking> hotelBooking = query.from(Booking.class);
+ query.where(criteriaBuilder.equal(hotelBooking.get("user"), user));
+ query.orderBy(criteriaBuilder.asc(hotelBooking.get("checkinDate")));
+
+ bookings = em.createQuery(query).getResultList();
}
public void cancel()
Modified:
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/java/org/jboss/seam/example/jpa/RegisterAction.java
===================================================================
---
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/java/org/jboss/seam/example/jpa/RegisterAction.java 2012-09-05
13:52:16 UTC (rev 15119)
+++
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/java/org/jboss/seam/example/jpa/RegisterAction.java 2012-09-06
14:56:12 UTC (rev 15120)
@@ -3,9 +3,8 @@
import static org.jboss.seam.ScopeType.EVENT;
-import java.util.List;
-
import javax.persistence.EntityManager;
+import javax.persistence.NoResultException;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
@@ -34,18 +33,19 @@
{
if ( user.getPassword().equals(verify) )
{
- List existing = em.createQuery("select u.username from User u where
u.username=#{user.username}")
- .getResultList();
- if (existing.size()==0)
+ // this is JPA 2.0 usage of new method getSingleResult()
+ try
{
+ em.createQuery("select u.username from User u where
u.username=#{user.username}").getSingleResult();
+ facesMessages.addToControl("username", "Username
#{user.username} already exists");
+ }
+ catch (NoResultException e)
+ {
em.persist(user);
facesMessages.add("Successfully registered as #{user.username}");
registered = true;
}
- else
- {
- facesMessages.addToControl("username", "Username
#{user.username} already exists");
- }
+
}
else
{
Modified:
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/resources/META-INF/persistence.xml
===================================================================
---
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/resources/META-INF/persistence.xml 2012-09-05
13:52:16 UTC (rev 15119)
+++
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/resources/META-INF/persistence.xml 2012-09-06
14:56:12 UTC (rev 15120)
@@ -1,15 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
- version="1.0">
+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
+ version="2.0">
<persistence-unit name="bookingDatabase"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:jboss/datasources/ExampleDS</non-jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto"
value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
-<!-- <property name="hibernate.cache.provider_class"
value="org.hibernate.cache.HashtableCacheProvider"/> -->
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<!-- Binds the EntityManagerFactory to JNDI where Seam can look it up.
This is only relevant when the container automatically loads the
persistence unit, as is the case in JBoss AS 5. -->
Modified: branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/home.xhtml
===================================================================
---
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/home.xhtml 2012-09-05
13:52:16 UTC (rev 15119)
+++
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/home.xhtml 2012-09-06
14:56:12 UTC (rev 15120)
@@ -42,7 +42,7 @@
</h:form>
</div>
</div>
- <div id="footer">Created with JBoss EJB 3.0, Seam, JSF (Mojarra), and
Facelets</div>
+ <div id="footer">Created with Seam, JSF 2 and JPA 2</div>
</div>
</f:view>
</h:body>
Modified: branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/main.xhtml
===================================================================
---
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/main.xhtml 2012-09-05
13:52:16 UTC (rev 15119)
+++
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/main.xhtml 2012-09-06
14:56:12 UTC (rev 15120)
@@ -20,7 +20,7 @@
<h1>Search Hotels</h1>
<fieldset>
<h:inputText id="searchString"
value="#{hotelSearch.searchString}" style="width: 165px;">
- <a:ajax id="onkeyup" event="keyup"
listener="#{hotelSearch.find}" render="searchResults" />
+ <a:ajax id="keyup" event="keyup"
listener="#{hotelSearch.find}" render="searchResults" />
</h:inputText>
 
<a:commandButton id="findHotels" value="Find Hotels"
action="#{hotelSearch.find}" reRender="searchResults"/>
Modified:
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/register.xhtml
===================================================================
---
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/register.xhtml 2012-09-05
13:52:16 UTC (rev 15119)
+++
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/register.xhtml 2012-09-06
14:56:12 UTC (rev 15120)
@@ -98,7 +98,7 @@
</div>
</div>
</div>
- <div id="footer">Created with JBoss EJB 3.0, Seam, JSF (Mojarra), and
Facelets</div>
+ <div id="footer">Created with Seam, JSF 2 and JPA 2</div>
</div>
</h:body>
</html>
Modified:
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/template.xhtml
===================================================================
---
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/template.xhtml 2012-09-05
13:52:16 UTC (rev 15119)
+++
branches/community/Seam_2_3/examples-ee6/jpa/jpa-web/src/main/webapp/template.xhtml 2012-09-06
14:56:12 UTC (rev 15120)
@@ -29,7 +29,7 @@
<ui:include src="conversations.xhtml" />
</div>
</div>
- <div id="footer">Created with JBoss Seam, JSF 1.2, Hibernate 3 and
Facelets</div>
+ <div id="footer">Created with JBoss Seam, JSF 2 and JPA 2</div>
</div>
</h:body>
</html>