gpt4 book ai didi

java - Dao 类的通用类

转载 作者:行者123 更新时间:2023-12-01 13:08:42 26 4
gpt4 key购买 nike

我正在尝试设置一个通用类,它将作为我的应用程序中所有 Dao 类的基类,但我现在遇到一个错误。我的泛型类是这样定义的;

public class Dao<E> {

private final E entity;

@Autowired
SessionFactory sessionFactory;

protected Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}

public Dao(E entity) {
this.entity = entity;
}

public E getEntity() {
return this.entity;
}

@Transactional
public boolean persist(E transientInstance) {
try {
sessionFactory.getCurrentSession().persist(transientInstance);
return true;
} catch (RuntimeException re) {
return false;
}
}

@Transactional
public boolean remove(E transientInstance) {
try {
sessionFactory.getCurrentSession().delete(transientInstance);
return true;
} catch (RuntimeException re) {
return false;
}
}

@SuppressWarnings("unchecked")
@Transactional
public E merge(E detachedInstance) {
try {
E result = (E) sessionFactory.getCurrentSession().merge(detachedInstance);
return result;
} catch (RuntimeException re) {
return null;
}
}

@Transactional
public E findById(int id) {
try {
E instance = (E) sessionFactory.getCurrentSession().get(E, id);
return instance;
} catch (RuntimeException re) {
return null;
}
}

@SuppressWarnings("unchecked")
@Transactional
public E findByUsername(String username) {
try {
E instance = (E) sessionFactory.getCurrentSession().createCriteria(E, username).add(Restrictions.like("login", username)).list().get(0);
return instance;
} catch (RuntimeException re) {
return null;
}
}

@SuppressWarnings("unchecked")
@Transactional
public List<E> findAll() {
try {
List<E> instance = sessionFactory.getCurrentSession().createCriteria(E).list();
return instance;
} catch (RuntimeException re) {
return null;
}
}

}

出现错误的方法是最后三个,与它们的实现中get()和createCriteria()对E的引用有关(错误是E不能被解析为变量)。通常,当我不使用这种基于通用的方法时,我会使用“Usuario.class”之类的东西。

任何人都知道如何在我的通用类中修复此错误(如果这种方法可行的话)。

最佳答案

首先,类型参数E不等于 Class 的实例。其次,由于 Java 中的类型删除,有关 E 的所有内容都会在运行时丢失。但这似乎entity作为参数传递给构造函数的是尝试请求类时要使用的类型标记。因此更改方法如下:

@Transactional
public E findById(int id) {
try {
E instance = (E) sessionFactory.getCurrentSession().get(entity.getClass(), id);
return instance;
} catch (RuntimeException re) {
return null;
}
}

@SuppressWarnings("unchecked")
@Transactional
public E findByUsername(String username) {
try {
E instance = (E) sessionFactory.getCurrentSession().createCriteria(entity.getClass(), username).add(Restrictions.like("login", username)).list().get(0);
return instance;
} catch (RuntimeException re) {
return null;
}
}

@SuppressWarnings("unchecked")
@Transactional
public List<E> findAll() {
try {
List<E> instance = sessionFactory.getCurrentSession().createCriteria(entity.getClass()).list();
return instance;
} catch (RuntimeException re) {
return null;
}
}

或者如果entity不是您的类型标记,请添加参数 Class<E> clazz像这样的构造函数:

private final E entity;
private final Class<E> clazz;

public Dao(E entity, Class<E> clazz) {
this.entity = entity;
this.clazz = clazz;
}

并使用clazz而不是entity.getClass()按照我建议的方法。

关于java - Dao 类的通用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23061984/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com