我在另一個主題(Empty List (not Table) at ManyToMany-Relation)中遇到問題並懷疑我的EntityManager用法是否正確。那麼使用EntityManager最好的方法是什麼?幾年前,我閱讀了一些關於DAO模式的內容(如http://www.oracle.com/technetwork/java/dataaccessobject-138824.html),因爲那是我用過的。但現在,當我想加盟類的WebServices我想到了一個「服務層」會比較好,所以我建立一個像實例化EntityManager的最佳實踐
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@Path("role")
public class RoleService {
@GET
@Path("ping")
@Produces(MediaType.TEXT_PLAIN)
public String helloWorld() {
return "REST-Web-Service ready for Role.class!";
}
public static void create(Role object) {
EntityManager em = PersistenceUtil.getEntityManagerFactory().createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(object);
tx.commit();
em.close();
}
public static void update(Role object) {
EntityManager em = PersistenceUtil.getEntityManagerFactory().createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.merge(object);
tx.commit();
em.close();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("id/{id}")
public static Role getRole(@PathParam("id") Integer id) {
return load(id);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("name")
public static String getName(@QueryParam("id") Integer id) {
Role role = findById(id);
if (role != null) {
return "[\n {\n \"id\":"+id+",\n \"type\":\"role\",\n \"name\": \"" + role.getName() + "\",\n \"query\":\"success\"\n }\n]";
}
return "[\n {\n \"id\":"+id+",\n \"type\":\"role\",\n \"query\":\"failed\"\n }\n]";
}
public static Role findById(Integer id) {
EntityManager em = PersistenceUtil.getEntityManagerFactory().createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Role object = em.find(Role.class, id);
tx.commit();
em.close();
return object;
}
public static Role load(Integer id) {
EntityManager em = PersistenceUtil.getEntityManagerFactory().createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Role objectResult = em.find(Role.class, id);
tx.commit();
em.close();
return objectResult;
}
public static Role load(Role object) {
EntityManager em = PersistenceUtil.getEntityManagerFactory().createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Role objectResult = em.find(Role.class, object.getId());
tx.commit();
em.close();
return objectResult;
}
public static void deleteById(Integer id) {
EntityManager em = PersistenceUtil.getEntityManagerFactory().createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.remove(em.find(Role.class, id));
tx.commit();
em.close();
}
// @DELETE
// @Path("{id}")
public static void delete(Role object) {
EntityManager em = PersistenceUtil.getEntityManagerFactory().createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.remove(em.find(Object.class, object.getId()));
tx.commit();
em.close();
}
public static List<Role> findByName(String name) {
EntityManager em = PersistenceUtil.getEntityManagerFactory().createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
List<Role> list = em.createQuery("SELECT r FROM Role r WHERE r.name LIKE :name").setParameter("name", "%" + name + "%").getResultList();
tx.commit();
em.close();
return list;
}
}
類的PersistenceUtil是
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class PersistenceUtil {
/*
* Name of persistence unit which MUST correlate to persistence-unit name in persistence.xml
*/
private static final String PERSISTENCE_UNIT_NAME = "RoleModel";
private static final EntityManagerFactory entityManagerFactory;
static {
try {
entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
} catch (Throwable ex) {
System.err.println("EntityManagerFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static EntityManagerFactory getEntityManagerFactory() {
return entityManagerFactory;
}
}
但在文章中Entity manager best practices它看起來不同。 Se應該在哪裏實例化EntityManager?我應該使用更好的註釋嗎?更好的Sigleton級?當我在每種方法中使用它時都可以嗎?
您認爲如何?
CDI(jboss或spring)是一種現代而優雅的方式。 – dit