我正在实现GenericDao。我有两个方法的问题- getAll()和getById(长id),实体类有空值。看来这个类没有设置好。我该如何解决这个问题?
@Repository
public class GenericDaoImpl<T> implements GenericDao<T> {
private Class<T> clazz;
@Autowired
SessionFactory sessionFactory;
public void setClazz(final Class<T> clazzToSet) {
this.clazz = clazzToSet;
}
public T getById(final Long id) {
return (T) this.getCurrentSession().get(this.clazz, id);
}
public List<T> getAll() {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
this.clazz);
return criteria.list();
}
protected final Session getCurrentSession() {
return this.sessionFactory.getCurrentSession();
}
}PersonDao
public interface PersonDao extends GenericDao<Person> { }PersonDaoImpl
@Repository("PersonDAO")
public class PersonDaoImpl extends GenericDaoImpl<Person> implements PersonDao {}服务:
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonDao personDao;
@Transactional
public List<Person> getAll() {
return personDao.getAll();
}
@Transactional
public Person getById(Long id) {
return personDao.getById(id);
}
}发布于 2014-06-15 11:47:20
必须设置clazz属性PersonDao。这可以通过使用后初始化回调注释声明@PostConstruct来实现。
@Repository("PersonDAO")
public class PersonDaoImpl extends GenericDaoImpl<Person> implements PersonDao {
@PostConstruct
public void init(){
super.setClazz(Person.class);
}
}https://stackoverflow.com/questions/24229225
复制相似问题