类型化通用 DAO 和实体
目前我正在尝试实现类型化通用DAO。
我什至无法编译任何内容,因为 NetBeans 抱怨 UserDAOHibernate
这里需要界面
类型参数用户不在类型变量 ENTITY 的范围内
我担心我使用继承/接口的方式存在一些明显的错误,因为我对 Java 相当陌生。
这是一些精简的代码
public interface GenericEntity<ID extends Serializable> {
public abstract ID getId();
public abstract void setId(final ID id);
}
public abstract class LongEntity implements GenericEntity<Long> {
protected Long id;
public Long getId();
public void setId(final Long id);
}
public class User extends LongEntity implements Serializable {
private String name;
private String password;
private Customer customer;
}
public interface GenericDAO<ENTITY extends GenericEntity<ID>, ID extends Serializable> {
public abstract ENTITY findById(ID id);
public abstract List<ENTITY> findAll();
public abstract ENTITY makePersistent(ENTITY entity);
public abstract void makeTransient(ENTITY entity);
}
public abstract class GenericHibernateDAO<ENTITY extends GenericEntity<ID>, ID extends Serializable>
implements GenericDAO<ENTITY, ID> {
}
public class UserDAOHibernate implements GenericHibernateDAO<User, LongEntity> {
}
LongEntity
应该扩展 GenericEntity
吗?如果是这样,我将如何使用 Java 的单级或继承来做到这一点?
这种分层方法是一个不好的榜样吗?我的所有实体都需要一个 id,并且此实现可以轻松地在以后使用不同的 id 类型重用,所以我想我可以使用它。
currently I'm trying to implement a typed generic DAO.
I do not even get to compile anything, since NetBeans complains about UserDAOHibernate
interface expected here
type argument User is not within bounds of type-variable ENTITY
I'm afraid there is some obvious mistake in how I use inheritance/interfaces, since I'm rather new to Java.
Here's some stripped down code
public interface GenericEntity<ID extends Serializable> {
public abstract ID getId();
public abstract void setId(final ID id);
}
public abstract class LongEntity implements GenericEntity<Long> {
protected Long id;
public Long getId();
public void setId(final Long id);
}
public class User extends LongEntity implements Serializable {
private String name;
private String password;
private Customer customer;
}
public interface GenericDAO<ENTITY extends GenericEntity<ID>, ID extends Serializable> {
public abstract ENTITY findById(ID id);
public abstract List<ENTITY> findAll();
public abstract ENTITY makePersistent(ENTITY entity);
public abstract void makeTransient(ENTITY entity);
}
public abstract class GenericHibernateDAO<ENTITY extends GenericEntity<ID>, ID extends Serializable>
implements GenericDAO<ENTITY, ID> {
}
public class UserDAOHibernate implements GenericHibernateDAO<User, LongEntity> {
}
Is it that LongEntity
should extend GenericEntity<Long>
? If so, how would I do this with Java's single level or inheritance?
Is this layered approach a bad example to follow? All my entities need an id and this implementation could easily be reused lateron with different id types, so I thought I might use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误来自这里:
您已指定
GenericHibernateDAO
的ID
参数化类型受
限制。LongEntity
扩展了GenericEntity
,因此,这就是类型不匹配的原因。另外,GenericHibernateDAO 是一个抽象类(而不是接口),因此您需要扩展而不是实现。
正确的解决方案应该是:
The error comes from here:
You've specified that
GenericHibernateDAO
'sID
parameterized type is bounded by<ID extends Serializable>
.LongEntity
extendsGenericEntity
, and hence, why you have a type mismatch.Also,
GenericHibernateDAO
is an abstract class (and not an interface), so you'll need toextends
instead ofimplements
.The correct solution should be: