Commits

Micha Kops  committed 4a7387f

JPA Criteria API examples finished.

  • Participants
  • Parent commits 437232d

Comments (0)

Files changed (3)

 
 This should produce a similar output:
 
-    Book [id=2, title=The big book of something, author=Author [id=1, name=Some Guy], published=Fri Dec 31 22:43:42 CET 2010,tags=[Tag [name=Horror], Tag [name=Drama], Tag [name=Science]]]
-    Book [id=2, title=The big book of something, author=Author [id=1, name=Some Guy], published=Fri Dec 31 22:43:42 CET 2010, tags=[Tag [name=Horror], Tag [name=Drama], Tag [name=Science]]]
-    Book [id=2, title=The big book of something, author=Author [id=1, name=Some Guy], published=Fri Dec 31 22:43:42 CET 2010, tags=[Tag [name=Horror], Tag [name=Drama], Tag [name=Science]]]
+    Using Querydsl:			Book [id=2, title=The big book of something, author=Author [id=1, name=Some Guy], published=Fri Dec 31 16:55:00 CET 2010, tags=[Tag [name=Horror], Tag [name=Drama], Tag [name=Science]]]
+    Using JPA Criteria API:		Book [id=2, title=The big book of something, author=Author [id=1, name=Some Guy], published=Fri Dec 31 16:55:00 CET 2010, tags=[Tag [name=Horror], Tag [name=Drama], Tag [name=Science]]]
+    Using JPA Query Language:	Book [id=2, title=The big book of something, author=Author [id=1, name=Some Guy], published=Fri Dec 31 16:55:00 CET 2010, tags=[Tag [name=Horror], Tag [name=Drama], Tag [name=Science]]]
 
 
 ## MongoDB with Morphia

File jpa-example/src/main/java/com/hascode/app/JPAExamples.java

 package com.hascode.app;
 
 import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
 
 import javax.persistence.EntityManager;
 import javax.persistence.EntityTransaction;
 import javax.persistence.Persistence;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Expression;
 import javax.persistence.criteria.Root;
 
 import com.hascode.entity.Author;
 				.where(book.title.startsWith("The"),
 						book.published.after(cal1.getTime()),
 						book.author.name.eq("Some Guy"),
-						book.tags.contains(horrorTag)).uniqueResult(book);
-		System.out.println(bookFound1.toString());
+						book.tags.contains(horrorTag))
+				.orderBy(book.title.asc()).uniqueResult(book);
+		System.out.println("Using Querydsl:\t\t\t" + bookFound1.toString());
 
 		// Using JPA Criteria API
 		CriteriaBuilder cb = em.getCriteriaBuilder();
 		CriteriaQuery<Book> cq = cb.createQuery(Book.class);
 		Root<Book> bk = cq.from(Book.class);
 		cq.select(bk);
-		cq.where(cb.like(bk.get("title").as(String.class), "The%"));
+		Expression<List<Tag>> tags = bk.get("tags");
+		cq.where(cb.like(bk.get("title").as(String.class), "The%"),
+				cb.greaterThan(bk.get("published").as(Date.class),
+						cal1.getTime()), cb.equal(bk.get("author").get("name")
+						.as(String.class), "Some Guy"), cb.isMember(horrorTag,
+						tags));
+
+		cq.orderBy(cb.asc(bk.get("title")));
 		Book bookFound2 = em.createQuery(cq).getSingleResult();
-		System.out.println(bookFound2.toString());
+		System.out.println("Using JPA Criteria API:\t\t"
+				+ bookFound2.toString());
 
 		// Using JPA Query Language
 		Book bookFound3 = em
 				.createQuery(
-						"SELECT book FROM Book book WHERE book.title LIKE 'The%' AND book.published>:published AND book.author.name=:author AND :tag MEMBER OF book.tags",
+						"SELECT book FROM Book book WHERE book.title LIKE 'The%' AND book.published>:published AND book.author.name=:author AND :tag MEMBER OF book.tags ORDER BY book.title ASC",
 						Book.class).setParameter("published", cal1.getTime())
 				.setParameter("author", "Some Guy")
 				.setParameter("tag", new Tag("Horror")).getSingleResult();
-		System.out.println(bookFound3.toString());
+		System.out.println("Using JPA Query Language:\t"
+				+ bookFound3.toString());
 		tx.rollback();
 
 	}

File mongodb-example/src/main/java/com/hascode/app/MongoDbExamples.java

 import java.io.IOException;
 import java.net.UnknownHostException;
 import java.util.Calendar;
+import java.util.List;
 
 import org.mongodb.morphia.Datastore;
 import org.mongodb.morphia.Morphia;
 		b2.setTitle("Another book");
 		b2.setAuthor(author);
 
+		Book b3 = new Book();
+		b3.setTitle("The other book of something");
+		b3.setAuthor(author);
+		b3.setPublished(cal2.getTime());
+		b3.addTag(dramaTag);
+
 		ds.save(b1);
 		ds.save(b2);
 
 		QBook book = QBook.book;
 		MorphiaQuery<Book> query = new MorphiaQuery<Book>(morphia, ds, book);
-		Book bookFound = query
-				.where(book.title.startsWith("The"),
-						book.published.after(cal1.getTime()),
-						book.author.name.eq("Some guy"),
-						book.tags.contains(horrorTag)).list().get(0);
-		System.out.println(bookFound.toString());
+		List<Book> books = query.where(book.title.startsWith("The"),
+				book.published.after(cal1.getTime()),
+				book.author.name.eq("Some guy"), book.tags.contains(horrorTag))
+				.list();
+		for (Book bookFound : books) {
+			System.out.println(bookFound.toString());
+		}
 	}
 }