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

发布于 2024-11-25 20:19:55 字数 599 浏览 1 评论 0原文

我正在尝试编写简单的 DAO,它将根据存储在 String 字段中的类型创建实体对象。如何返回动态改变的类型? UserDAO 类的 findById() 方法应返回 User 类对象。 ProductDAO 的相同方法应返回 Product。 我不想在每个扩展 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;
}

I'm trying to write simple DAO that will create entity objects based on their type stored in String field. How to return type that is changed dynamicly?
Method findById() of UserDAO class should return User class object. Same method for ProductDAO should return Product.
I don't want to implement findById in every class that extends DAO, it should be done automatically.

Example code:

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞陪衬 2024-12-02 20:19:55

修改为

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;
}

Modify it to

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;
}
拿命拼未来 2024-12-02 20:19:55

使用 java 中的泛型。在这里找到一个例子。

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...
}

Use Generics in java. Find an example here.

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...
}
两人的回忆 2024-12-02 20:19:55

首先,不要使用String,而是使用类。接下来,使用 entityManager (请参阅 docs

class DAO<T> {
   private Class<T> entityClass;

   // How you get one of these depends on the framework.
   private EntityManager entityManager;

   public T findById(int id) {
       return em.find(entityClass, id);
   }
}

现在您可以根据类型使用不同的DAO,例如

DAO<User> userDAO = new DAO<User>();
DAO<Product> userDAO = new DAO<Product>();

Firstly, instead of using the String, use the class. Next up, use an entityManager (see docs)

class DAO<T> {
   private Class<T> entityClass;

   // How you get one of these depends on the framework.
   private EntityManager entityManager;

   public T findById(int id) {
       return em.find(entityClass, id);
   }
}

Now you can use a different DAO dependent on the type e.g.

DAO<User> userDAO = new DAO<User>();
DAO<Product> userDAO = new DAO<Product>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文