Created
May 22, 2011 09:38
-
-
Save koduki/985306 to your computer and use it in GitHub Desktop.
JPA 2.0(JavaEE 6 by GlassFish 3.1) Example with CriteriaQuery.
This file contains hidden or 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
import java.util.List; | |
import javax.ejb.embeddable.EJBContainer; | |
import javax.naming.NamingException; | |
import javax.persistence.EntityManager; | |
import javax.persistence.Persistence; | |
import javax.persistence.Query; | |
import javax.persistence.criteria.CriteriaBuilder; | |
import javax.persistence.criteria.CriteriaQuery; | |
import javax.persistence.criteria.Predicate; | |
import javax.persistence.criteria.Root; | |
import model.Customer; | |
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
/** | |
* | |
* @author hiro | |
*/ | |
public class JPAExample { | |
public static void main(String[] args) throws NamingException { | |
EJBContainer container = EJBContainer.createEJBContainer(); | |
// persistance.xmlの読み込み. | |
EntityManager em = Persistence.createEntityManagerFactory("CustomerDBPU").createEntityManager(); | |
// CriteriaQuery | |
System.out.println("CriteriaQueryによる検索."); | |
CriteriaBuilder builder = em.getCriteriaBuilder(); | |
CriteriaBuilder cb = em.getCriteriaBuilder(); | |
CriteriaQuery<Customer> cq = cb.createQuery(Customer.class); | |
Root<Customer> customer = cq.from(Customer.class); | |
Predicate predicate = cb.equal(customer.get("customerId"), 409); | |
for (Customer c : em.createQuery(cq.select(customer).where(predicate)).getResultList()) { | |
System.out.println("customer is " + c.getName()); | |
} | |
// Count | |
System.out.println("CriteriaQueryによるcount."); | |
CriteriaQuery<Long> cq2 = builder.createQuery(Long.class); | |
cq2.select(builder.count(customer)); | |
System.out.println(em.createQuery(cq2.where(predicate)).getSingleResult()); | |
// JPQL | |
System.out.println("JPQLによる検索."); | |
Query query = em.createNamedQuery("Customer.findByCustomerId"); | |
query.setParameter("customerId", 410); | |
List<Customer> list = query.getResultList(); | |
for(Customer c : list){ | |
System.out.println("customer is " + c.getName()); | |
} | |
container.close(); | |
} | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | |
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> | |
<persistence-unit name="CustomerDBPU" transaction-type="JTA"> | |
<jta-data-source>jdbc/sample</jta-data-source> | |
<exclude-unlisted-classes>false</exclude-unlisted-classes> | |
<properties> | |
<property name="eclipselink.logging.level" value="FINE" /> | |
<property name="eclipselink.logging.timestamp" value="true" /> | |
<property name="eclipselink.logging.session" value="true" /> | |
<property name="eclipselink.logging.thread" value="true" /> | |
</properties> | |
</persistence-unit> | |
</persistence> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment