为常见数据库功能构建 Dao
我正在尝试为常见数据库函数(如 findAll、findById、insert、update、deleteById 等)编写 BaseDao 类。
我采用了接口 BaseDao.java 和另一个类 BaseDaoAdapter.java 实现 BaseDao.java。
这个Dao被注入到服务中。因此,需要通用 CRUD 功能的 Service 类将使用这个 Dao,而那些需要特定功能的 Service 类将拥有自己的 Dao。
我们这样做是为了减少 Dao 层的代码重复。
我编写的该类的代码如下。
@Repository
public class BaseDaoAdapter {
private EntityManagerFactory emf;
@PersistenceUnit
public void setEntityManagerFactory(EntityManagerFactory emf) {
this.emf = emf;
}
/**
*
* @param tableName Name of table from which data has to be retrieved
* @return A list of all the records in the specified table
*/
@SuppressWarnings("unchecked")
public List<? extends BaseEntity> list(String tableName){
EntityManager em = emf.createEntityManager();
Query q =em.createQuery("SELECT A FROM "+tableName+" A");
return q.getResultList();
}
/**
*
* @param obj An object of the entity that is to be inserted into the database.
*/
public void add(BaseEntity obj){
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(obj);
em.getTransaction().commit();
em.close();
}
/**
*
* @param obj Entity object to be deleted from the database
*/
public void remove(BaseEntity obj){
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.remove(obj);
em.getTransaction().commit();
em.close();
}
}
这里的 BaseEntity 是所有域类都实现的接口。
当我尝试将查询返回的列表转换为任何特定于域的列表(例如 List)时, (? extends BaseEntity) 语法有时会出错。
我们这样做是因为我们只想返回特定于域的列表。
我想在这里问两个问题。
- 1)我们遵循的方法是否正确?
- 2)有没有更好的方法来实现这个BaseDaoAdapter,这样我们就不会遇到类型转换问题?
I am trying to code a BaseDao class for common Database functions like findAll, findById, insert, update, deleteById, etc.
I have taken an interface BaseDao.java and another class BaseDaoAdapter.java implements BaseDao.java.
This Dao is injected into service. So Service classes which require common CRUD functionality will use this Dao, and those that require specific functionality will have their own Dao.
We have done this to reduce code duplication across the Dao layer.
The code for the class, which I wrote is as follows.
@Repository
public class BaseDaoAdapter {
private EntityManagerFactory emf;
@PersistenceUnit
public void setEntityManagerFactory(EntityManagerFactory emf) {
this.emf = emf;
}
/**
*
* @param tableName Name of table from which data has to be retrieved
* @return A list of all the records in the specified table
*/
@SuppressWarnings("unchecked")
public List<? extends BaseEntity> list(String tableName){
EntityManager em = emf.createEntityManager();
Query q =em.createQuery("SELECT A FROM "+tableName+" A");
return q.getResultList();
}
/**
*
* @param obj An object of the entity that is to be inserted into the database.
*/
public void add(BaseEntity obj){
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(obj);
em.getTransaction().commit();
em.close();
}
/**
*
* @param obj Entity object to be deleted from the database
*/
public void remove(BaseEntity obj){
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.remove(obj);
em.getTransaction().commit();
em.close();
}
}
Here the BaseEntity is an interface which all domain classes implement.
The (? extends BaseEntity) syntax sometimes gives errors when I try to cast the list returned by query to any domain specific list for example List.
We have done this because we want to return only a domain specific list.
I would like to ask two questions here.
- 1) Whether the approach we are following is correct?
- 2) Is there any better way to implement this BaseDaoAdapter so that we do not get type casting issues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您仔细阅读 Google App Engine 关于多态性主题的文档。某些 JDO 继承关系可能尚不受支持。此外,如果您尝试此方法,存储 Key 引用也可能是个好主意。
有关多态性的 Google App Engine JDO 文档
I would suggest you read Google App Engine's documentation on the subject of Polymorphism very carefully. Some JDO inheritance relationships may not yet be supported. Additionally, it may also be a good idea to store a Key reference if you attempt this method.
Google App Engine JDO Documentation on Polymorphism