类型化通用 DAO 和实体

发布于 2024-12-08 14:00:54 字数 1631 浏览 0 评论 0原文

目前我正在尝试实现类型化通用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 技术交流群。

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

发布评论

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

评论(1

画尸师 2024-12-15 14:00:54

错误来自这里:

public class UserDAOHibernate implements GenericHibernateDAO<User, LongEntity> {
}

您已指定 GenericHibernateDAOID 参数化类型受 限制。

LongEntity 扩展了 GenericEntity,因此,这就是类型不匹配的原因。

另外,GenericHibernateDAO 是一个抽象类(而不是接口),因此您需要扩展而不是实现。

正确的解决方案应该是:

public class UserDAOHibernate extends GenericHibernateDAO<User, Long> {
}

The error comes from here:

public class UserDAOHibernate implements GenericHibernateDAO<User, LongEntity> {
}

You've specified that GenericHibernateDAO's ID parameterized type is bounded by <ID extends Serializable>.

LongEntity extends GenericEntity, and hence, why you have a type mismatch.

Also, GenericHibernateDAO is an abstract class (and not an interface), so you'll need to extends instead of implements.

The correct solution should be:

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