1. Micha Kops
  2. jpa2_tutorial

Commits

hascode  committed 34e38a6 Draft

Type safe queries added.

  • Participants
  • Parent commits 1599c48
  • Branches default

Comments (0)

Files changed (10)

File .hgignore

View file
+.project
+.classpath
+.settings
+settings
+.cache
+target

File README.md

View file
+# Java Persistence API 2 Tutorial
+
+A short introduction to the Java Persistence API / JPA 2. 
+
+For more detailed information please feel free to take a look at the full article at [www.hascode.com].
+
+----
+
+**2013 Micha Kops /hasCode.com**
+
+   [www.hascode.com]:http://www.hascode.com/
+    

File pom.xml

File contents unchanged.

File src/main/java/com/hascode/tutorial/jpa2/Address.java

File contents unchanged.

File src/main/java/com/hascode/tutorial/jpa2/Bookmark.java

File contents unchanged.

File src/main/java/com/hascode/tutorial/jpa2/Person.java

File contents unchanged.

File src/main/java/com/hascode/tutorial/jpa2/Pet.java

File contents unchanged.

File src/main/java/com/hascode/tutorial/jpa2/Sex.java

File contents unchanged.

File src/test/java/com/hascode/tutorial/jpa2/PersonTest.java

View file
 import org.junit.Test;
 
 public class PersonTest {
-	private static EntityManagerFactory	emf;
-	private static EntityManager		em;
-	private static EntityTransaction	tx;
+	private static EntityManagerFactory emf;
+	private static EntityManager em;
+	private static EntityTransaction tx;
 
 	@Before
 	public void initEntityManager() throws Exception {
 		tx.commit();
 
 		// query with named parameters (i prefer this one)
-		Query query = em
-				.createQuery("SELECT p FROM Person p WHERE p.nickname=:name");
+		TypedQuery<Person> query = em.createQuery(
+				"SELECT p FROM Person p WHERE p.nickname=:name", Person.class);
 		query.setParameter("name", "HAL9000");
-		Person p1 = (Person) query.getResultList().get(0);
+		Person p1 = query.getResultList().get(0);
 		assertEquals(hal.getId(), p1.getId());
 
 		// same with positional parameters
-		Query query2 = em
-				.createQuery("SELECT p FROM Person p WHERE p.nickname=?1");
+		TypedQuery<Person> query2 = em.createQuery(
+				"SELECT p FROM Person p WHERE p.nickname=?1", Person.class);
 		query2.setParameter(1, "HAL9000");
-		Person p2 = (Person) query2.getResultList().get(0);
+		Person p2 = query2.getResultList().get(0);
 		assertEquals(hal.getId(), p2.getId());
 
 		// an example using joins
-		Query query3 = em
-				.createQuery("SELECT p FROM Person p LEFT JOIN FETCH p.bookmarks b WHERE b.title=:title");
+		TypedQuery<Person> query3 = em
+				.createQuery(
+						"SELECT p FROM Person p LEFT JOIN FETCH p.bookmarks b WHERE b.title=:title",
+						Person.class);
 		query3.setParameter("title", "Snoring for experts");
-		Person p3 = (Person) query3.getResultList().get(0);
+		Person p3 = query3.getResultList().get(0);
 		assertEquals(hal.getId(), p3.getId());
 	}
 
 		em.persist(ronald);
 		tx.commit();
 
-		List<Person> persons = em.createNamedQuery("findAll").getResultList();
+		List<Person> persons = em.createNamedQuery("findAll", Person.class)
+				.getResultList();
 		assertEquals(2, persons.size());
 
 		Query q = em.createNamedQuery("findByNickname");

File src/test/resources/META-INF/persistence.xml

File contents unchanged.