Java DAO工厂动态返回的对象类型

Java DAO工厂动态返回的对象类型,java,generics,dao,genericdao,Java,Generics,Dao,Genericdao,我试图编写一个简单的DAO,它将根据字符串字段中存储的实体对象类型创建实体对象。如何返回动态更改的类型? UserDAO类的方法findById()应返回用户类对象。ProductDAO的相同方法应返回产品。 我不想在每个扩展DAO的类中实现findById,它应该自动完成 示例代码: class DAO { protected String entityClass = ""; public (???) findById(int id) { // some DB quer

我试图编写一个简单的DAO,它将根据字符串字段中存储的实体对象类型创建实体对象。如何返回动态更改的类型? UserDAO类的方法findById()应返回用户类对象。ProductDAO的相同方法应返回产品。 我不想在每个扩展DAO的类中实现findById,它应该自动完成

示例代码:

class DAO {
   protected String entityClass = "";
   public (???) findById(int id) {
      // some DB query
      return (???)EntityFromDatabase; // how to do this?
   }
}
class UserDAO extends DAO {
   protected String entityClass = "User";
}
class ProductDAO extends DAO {
   protected String entityClass = "Product";
}
class User extends Entity {
   public int id;
   public String name;
}

首先,不要使用
字符串
,而是使用类。接下来,使用
entityManager
(请参阅)


首先,不要使用
字符串
,而是使用类。接下来,使用
entityManager
(请参阅)

修改为

class DAO<T> {
   //   protected String entityClass = "";
   public T findById(int id) {

      return (T)EntityFromDatabase; // how to do this?
   }
}
class UserDAO extends DAO<User> {
   //protected String entityClass = "User";
}
class ProductDAO extends DAO<Product> {
   //protected String entityClass = "Product";
}
class User extends Entity {
   public int id;
   public String name;
}
类DAO{
//受保护字符串entityClass=“”;
公共T findById(int id){
return(T)EntityFromDatabase;//如何执行此操作?
}
}
类UserDAO扩展了DAO{
//受保护的字符串entityClass=“用户”;
}
类ProductDAO扩展了DAO{
//受保护字符串entityClass=“产品”;
}
类用户扩展实体{
公共int id;
公共字符串名称;
}
将其修改为

class DAO<T> {
   //   protected String entityClass = "";
   public T findById(int id) {

      return (T)EntityFromDatabase; // how to do this?
   }
}
class UserDAO extends DAO<User> {
   //protected String entityClass = "User";
}
class ProductDAO extends DAO<Product> {
   //protected String entityClass = "Product";
}
class User extends Entity {
   public int id;
   public String name;
}
类DAO{
//受保护字符串entityClass=“”;
公共T findById(int id){
return(T)EntityFromDatabase;//如何执行此操作?
}
}
类UserDAO扩展了DAO{
//受保护的字符串entityClass=“用户”;
}
类ProductDAO扩展了DAO{
//受保护字符串entityClass=“产品”;
}
类用户扩展实体{
公共int id;
公共字符串名称;
}
使用。在这里找到一个例子

public interface GenericDAO<T,PK extends Serializable> {

  PK create(T entity);
  T read(PK id);
  void update(T entity);
  void delete(T entity);
}
public class GenericDAOImpl<T,PK extends Serializable>  implements GenericDAO<T,PK>{
    private Class<T> entityType;
    public GenericDAOImpl(Class<T> entityType){
          this.entityType = entityType; 
    }
     //Other impl methods here...
}
公共接口GenericDAO{
PK创建(T实体);
T读取(pkid);
无效更新(T实体);
无效删除(T实体);
}
公共类GenericDAOImpl实现GenericDAO{
私有类实体类型;
公共GenericDAOImpl(类entityType){
this.entityType=entityType;
}
//这里的其他impl方法。。。
}
使用。在这里找到一个例子

public interface GenericDAO<T,PK extends Serializable> {

  PK create(T entity);
  T read(PK id);
  void update(T entity);
  void delete(T entity);
}
public class GenericDAOImpl<T,PK extends Serializable>  implements GenericDAO<T,PK>{
    private Class<T> entityType;
    public GenericDAOImpl(Class<T> entityType){
          this.entityType = entityType; 
    }
     //Other impl methods here...
}
公共接口GenericDAO{
PK创建(T实体);
T读取(pkid);
无效更新(T实体);
无效删除(T实体);
}
公共类GenericDAOImpl实现GenericDAO{
私有类实体类型;
公共GenericDAOImpl(类entityType){
this.entityType=entityType;
}
//这里的其他impl方法。。。
}

我强烈推荐您阅读这篇文章。我必须说,你的想法不错。

我强烈推荐你这篇文章。我必须说,您的想法不错。

如何在DAO中获取
entityClass
?此hibernate链接提供了一个如何在DAO中设置entityClass的示例如何获取
entityClass
?此hibernate链接提供了一个如何设置entityClass的示例