EclipseLink 使ってみる
ちょっとお仕事の関係で EclipseLink を調べる必要が出てきたので、ひとまず簡単なサンプルを書いてみました。その時のメモです。EclipseLink は JPA、MOXy、DBWS と3つのコンポーネントがあるみたいですが、今回は JPA に関する内容です。
・EclipseLink
http://www.eclipse.org/eclipselink/
■ pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>EclipseLinkTest</groupId> <artifactId>EclipseLinkTest</artifactId> <version>0.0.1</version> <dependencies> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>eclipselink</artifactId> <version>2.5.2</version> <exclusions> <exclusion> <groupId>org.eclipse.persistence</groupId> <artifactId>commonj.sdo</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.0-801.jdbc4</version> </dependency> </dependencies> </project>
・EclipseLink/Maven - Eclipsepedia
https://wiki.eclipse.org/EclipseLink/Maven
■ persistence.xml
<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 persistence_1_0.xsd" version="1.0"> <persistence-unit name="eclipselinktest" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://127.0.0.1/testdb"/> <property name="javax.persistence.jdbc.user" value="postgres"/> <property name="javax.persistence.jdbc.password" value="postgres"/> </properties> </persistence-unit> </persistence>
persistence.xml は META-INF 配下におく。
■ t1.java
エンティティクラスです。
package test.entity; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class T1 { @Id private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
■ EMFactory.java
EntityManagerFactory の生成クラスです。
package test.factory; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class EMFactory { public static EntityManagerFactory getEMFactory() { return Persistence.createEntityManagerFactory("eclipselinktest"); } }
■ T1Access.java
EntityManager API を使ったクラスです。今回のサンプルでは find / persist / remove / CriteriaQuery で全件検索をやってます。
package test.access; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import test.entity.T1; public class T1Access { public T1 find(EntityManager em, int id) { return (T1) em.find(T1.class, id); } public void persist(EntityManager em, T1 t) { em.persist(t); } public void remove(EntityManager em, T1 t) { em.remove(t); } public List<T1> findAll(EntityManager em) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T1> cq = cb.createQuery(T1.class); CriteriaQuery<T1> select = cq.select(cq.from(T1.class)); return em.createQuery(select).getResultList(); } }
■ Main.java
package test.access; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import test.entity.T1; import test.factory.EMFactory; public class Main { public static void main(String[] args) { EntityManagerFactory emf = EMFactory.getEMFactory(); EntityManager em = emf.createEntityManager(); EntityTransaction et = em.getTransaction(); T1Access instance = new T1Access(); et.begin(); T1 t1 = new T1(); t1.setId(1); t1.setName("hoge"); T1 t2 = new T1(); t2.setId(2); t2.setName("uga"); T1 t3 = new T1(); t3.setId(3); t3.setName("oro"); instance.persist(em, t1); instance.persist(em, t2); instance.persist(em, t3); et.commit(); System.out.println("----- find -----"); T1 res = instance.find(em, 1); System.out.println(res.getId() + ":" + res.getName()); System.out.println("----- findAll -----"); for(T1 t : instance.findAll(em)) { System.out.println(t.getId() + ":" + t.getName()); } et.begin(); instance.remove(em, t2); et.commit(); System.out.println("----- findAll(after remove) -----"); for(T1 t : instance.findAll(em)) { System.out.println(t.getId() + ":" + t.getName()); } } }
※ 実行結果
----- find ----- 1:hoge ----- findAll ----- 3:oro 2:uga 1:hoge ----- findAll(after remove) ----- 3:oro 1:hoge
駆け足でしたが以上です。
・EclipseLink/Examples/JPA - Eclipsepedia
http://wiki.eclipse.org/EclipseLink/Examples/JPA
[ 環境情報 ]
Windows 7 SP1
Java SE 8 Update 74
EclipseLink 2.5.2
PostgreSQL 9.0.4