Created
October 15, 2012 08:49
-
-
Save asicfr/3891517 to your computer and use it in GitHub Desktop.
struts2RestJpaBootstrap - dao
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.demo.vo.dao; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.persistence.EntityManager; | |
import javax.persistence.TypedQuery; | |
import javax.persistence.criteria.CriteriaBuilder; | |
import javax.persistence.criteria.CriteriaQuery; | |
import javax.persistence.criteria.Predicate; | |
import javax.persistence.criteria.Root; | |
import org.apache.commons.lang3.StringUtils; | |
import org.demo.util.GenericJpaDao; | |
import org.demo.vo.bean.Book; | |
public class JpaBookDAO extends GenericJpaDao<Book, Integer> { | |
private final static Predicate[] ap = {}; | |
/** | |
* Constructs a DAO for Book | |
*/ | |
public JpaBookDAO() { | |
super(Book.class); | |
} | |
/** | |
* Search by book example | |
*/ | |
public List<Book> search(Book book) { | |
final EntityManager em = getEntityManager(); | |
if ( book != null ) { | |
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); | |
CriteriaQuery<Book> criteriaQuery = criteriaBuilder.createQuery(Book.class); | |
Root<Book> from = criteriaQuery.from(Book.class); | |
List<Predicate> listP = new ArrayList<Predicate>(); | |
if (StringUtils.isNotBlank(book.getIsbn())) { | |
Predicate predicate = criteriaBuilder.equal(from.get("isbn"), book.getIsbn()); | |
listP.add(predicate); | |
} | |
if (StringUtils.isNotBlank(book.getTitle())) { | |
Predicate predicate = criteriaBuilder.equal(from.get("title"), book.getTitle()); | |
listP.add(predicate); | |
} | |
if (book.getId() != null) { | |
Predicate predicate = criteriaBuilder.equal(from.get("id"), book.getId()); | |
listP.add(predicate); | |
} | |
if (book.getPublisherId() != null) { | |
Predicate predicate = criteriaBuilder.equal(from.get("publisherId"), book.getPublisherId()); | |
listP.add(predicate); | |
} | |
if (book.getAuthorId() != null) { | |
Predicate predicate = criteriaBuilder.equal(from.get("authorId"), book.getAuthorId()); | |
listP.add(predicate); | |
} | |
if (book.getPrice() != null) { | |
Predicate predicate = criteriaBuilder.equal(from.get("price"), book.getPrice()); | |
listP.add(predicate); | |
} | |
if (book.getQuantity() != null) { | |
Predicate predicate = criteriaBuilder.equal(from.get("quantity"), book.getQuantity()); | |
listP.add(predicate); | |
} | |
if (book.getDiscount() != null) { | |
Predicate predicate = criteriaBuilder.equal(from.get("discount"), book.getDiscount()); | |
listP.add(predicate); | |
} | |
if (book.getAvailability() != null) { | |
Predicate predicate = criteriaBuilder.equal(from.get("availability"), book.getAvailability()); | |
listP.add(predicate); | |
} | |
if (book.getBestSeller() != null) { | |
Predicate predicate = criteriaBuilder.equal(from.get("bestSeller"), book.getBestSeller()); | |
listP.add(predicate); | |
} | |
criteriaQuery.where(listP.toArray(ap)); | |
TypedQuery<Book> tq = em.createQuery(criteriaQuery); | |
return tq.getResultList(); | |
} | |
else | |
{ | |
return this.loadAll(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment