从父级 DAO 扩展的子级
我有一个问题。我有一个父 DAO:
public abstract class ParentDAO<T> {
@PersistenceContext
private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
private EntityManager em() {
if (entityManager == null)
throw new IllegalStateException("The entity manager is not set");
return entityManager;
}
}
从中扩展另一个子 DAO。 当我想对子DAO中的子实体进行某些操作时,我必须从父类获取EntityManager对象,或者将entityManager对象声明更改为受保护,这是糟糕的OOP设计。还有其他方法可以做到这一点吗?因为当我有 100 个 DAO 子级时,我必须为每个新子级从父级 DAO 获取实体管理器。
I have a problem. I have a parent DAO:
public abstract class ParentDAO<T> {
@PersistenceContext
private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
private EntityManager em() {
if (entityManager == null)
throw new IllegalStateException("The entity manager is not set");
return entityManager;
}
}
from which extends another children DAOs.
When I want to do some operation with children entity in children DAO, I must get EntityManager object from parent class or change the entityManager object declaration to protected which is bad OOP design. Is there another way to do this? Because when I have 100 DAO children's then I must get the entityManager from parent DAO for every new children.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,如果你使用的是spring,你可以只使用“parent”属性来给出基础bean id....所以在这种情况下创建一个基础bean对象实体管理器并将其添加为你的子类bean声明中的父对象....在 Spring bean 声明中查找 extends +parent。
yes incase you are using spring, you can just use "parent" attribute to give the base bean id.... so create one base bean object in this case entity manager and add it as parent in ur subclassed bean declarations.... look for extends + parent in spring bean declaration.