使用泛型和jpa EntityManager方法 [英] Using generics and jpa EntityManager methods
问题描述
我可以同时使用泛型和JPA吗?
Can I use generics and JPA together?
我试图将四个类的对象持久化到我的数据库中.这是我的PersistService类别:
I am trying to persist objects of four classes to my db. Here's my PersistService class:
public class PersistService<T> {
private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("fileUploadProject");
public static EntityManager getEntityManager() {
return emf.createEntityManager();
}
// Write Client to Database
public static <T> void persist(T obj) {
EntityManager em = getEntityManager();
EntityTransaction et = em.getTransaction();
et.begin();
em.persist(obj);
et.commit();
em.close();
}
}
但是随后我遇到了删除对象的问题.除上述内容外,我在PersistService类中还有以下方法:
But then I get into a problem with removing the object. I have the following method in the PersistService class in addition to the above:
// Remove an object from the Database if they exist
public static <T> void remove(Long id) {
EntityManager em = getEntityManager();
EntityTransaction et = em.getTransaction();
<T> obj = em.find(<T>.class, id);
}
最后一行给我一个编译时错误.我也尝试过<T>.class
T
Class<T>
和T.class
,但是它仍然给我一个编译时错误.刚刚学习类型擦除,是否是因为这个错误?我该如何解决这个问题?
The final line is giving me a compile time error. I've tried <T>.class
T
Class<T>
and T.class
as well, but it still gives me a compile time error. Just learning about Type Erasure, is this error because of that? How do I resolve this issue?
推荐答案
您已经开始使用良好的模式.下一步是为每种实体类型创建PersistService的子类.我还将提到,从长远来看,您可能希望为每个实体都有一个通用的基类或接口.例如,我将其称为Entity
.该基类(如果是类而不是接口)可以是抽象的,并且可以为所有实体定义通用方法.
You have started using a good pattern. The next step is to create a subclass of PersistService for each of your entity types. I will also mention that in the long run you probably want to have a common base class or interface for each of your entities. For example, I will call it Entity
. This base class (if it is a class rather than interface) can be abstract and can define common methods for all of your entities.
public interface Entity {
long getId();
}
您可以在实现PersistService
的过程中使用Entity
定义的方法(当您在此基础服务或代码的其他位置添加更多与通用实体相关的业务逻辑时,会发现这很方便).
You can use the methods defined by Entity
in your implementation of PersistService
(which you may find handy as you add more generic entity-related business logic in this base service or elsewhere in your code).
您的实体A
看起来像
public class A extends Entity {
}
您的PersistService
成为
public abstract class PersistService<T extends Entity> {
// Your common methods (persist, remove, etc.).
public abstract Class<T> getEntityClass();
}
您的特定于实体的服务如下
Your entity-specific services look like this
public class APersistService extends PersistService<A> {
public Class<A> getEntityClass() {
return A.class;
}
}
然后在实现PersistService.remove()
时使用getEntityClass()
方法.
You then use the getEntityClass()
method when you implement PersistService.remove()
.
当特定于实体的子类解决了面对类型擦除的特定类对象的问题时,您会发现自己最终希望该子类也支持特定于实体的查询.
While the entity-specific subclasses solve the problem of getting the specific class object in the face of type erasure, you will find that you end up wanting the subclass to support entity-specific queries as well.
这篇关于使用泛型和jpa EntityManager方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!