Seguinte pessoal,
Iniciei um projeto pra testar o VR3, seguindo os tutoriais disponíveis e consegui sem problemas fazer
funcionar, acesso ao mysql utlizando o hibernate e o spring, já que trabalham em conjunto o VR.
Mass, aí quis utilizar um GenericDao com o HibernateTemplate, e aí comecei a patinar buunito…
Abaixo segue a estrutura de classes que estou seguindo, e o erro no final.
e alguém puder me dar uma luz eu agradeço
OBS.: Agora o erro que está ocorrendo é em relação a Referência Circular, porém comecei a mexer
que acabei me perdendo mesmo, mas o primeiro erro era que ao tentar dar um getHibernateTemplate().xxxx
ocorria o seguinte erro:
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'produtoDaoImpl' defined in file
[D:\VRaptor\Projects\.metadata\.plugins\org.eclipse.wst.server.core
\tmp0\wtpwebapps\accessus\WEB-INF\classes\br\com\tecnopolis\accessus
\dao\impl\ProdutoDaoImpl.class]: Invocation of init method failed; nested
exception is java.lang.IllegalArgumentException:
[color=red]'sessionFactory' or 'hibernateTemplate' is required [/color]
Enfim, repassando o código:
Vou partir do princípio que apenas quero listar uma tabela.
@Resource
public class ProdutosController<T> {
private Result result;
private IProdutoDao<Produto> dao;
public ProdutosController(Result result, IProdutoDao<Produto> dao) {
super();
this.result = result;
this.dao = dao;
}
public void lista() {
Integer begin = 0;
Integer maxResult = 3;
result.use(Results.json()).from(dao.findAll(begin, maxResult)).serialize();
}
}
public interface IGenericDao<T> {
/**
* Return all persistent instances of the given entity class.
* Note: Use queries or criteria for retrieving a specific subset.
* @return a List containing 0 or more persistent instances
*/
public List<T> findAll();
/**
* Execute a query based on the given Hibernate criteria object.
* @param begin the index of the first result object to be retrieved (numbered from 0)
* @param maxResult the maximum number of result objects to retrieve (or <=0 for no limit)
* @return a List containing 0 or more persistent instances
*/
public List<T> findAll(Integer begin, Integer maxResult);
}
@Component
public class GenericDaoImpl<T> extends HibernateDaoSupport implements
IGenericDao<T> {
private static final Log log = LogFactory.getLog(GenericDaoImpl.class);
private T typeClass;
private HibernateTemplate hibernateTemplate;
protected Session session;
public GenericDaoImpl(T clazz, Session session) {
this.typeClass = clazz;
this.session = session;
this.hibernateTemplate = createHibernateTemplate(this.session.getSessionFactory());
}
/* (non-Javadoc)
* @see br.com.tecnopolis.accessus.dao.GenericDao#findAll()
*/
@SuppressWarnings("unchecked")
@Override
public List<T> findAll() {
log.debug("finding all " + typeClass.getClass().getName() + " instances");
try {
List<T> results = (List<T>) this.hibernateTemplate.loadAll(
typeClass.getClass());
log.debug("findAll successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("findAll failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see br.com.tecnopolis.accessus.dao.GenericDao#findAll(java.lang.Integer, java.lang.Integer)
*/
@SuppressWarnings("unchecked")
@Override
public List<T> findAll(Integer begin, Integer maxResult) {
log.debug("finding all " + typeClass.getClass().getName() + " instances from: "
+ begin + " to: " + maxResult);
try {
DetachedCriteria criteria = DetachedCriteria.forClass(typeClass.getClass());
List<T> results = (List<T>) this.hibernateTemplate.findByCriteria(
criteria, begin, maxResult);
log.debug("findAll successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("findAll failed", re);
throw re;
}
}
public interface IProdutoDao<T> extends IGenericDao<T> {
}
@Component
public class ProdutoDaoImpl<T> extends GenericDaoImpl<T> implements IProdutoDao<T> {
public ProdutoDaoImpl(T clazz, Session session) {
super(clazz, session);
}
}
Bom e o erro que agora ocorre é:
[color=red]org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘produtosController’ defined in file [D:\VRaptor\Projects.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\accessus\WEB-INF\classes\br\com\tecnopolis\accessus\controller\ProdutosController.class]: Unsatisfied dependency expressed through constructor argument with index 1 of type [br.com.tecnopolis.accessus.dao.IProdutoDao]: : Error creating bean with name ‘produtoDaoImpl’ defined in file [D:\VRaptor\Projects.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\accessus\WEB-INF\classes\br\com\tecnopolis\accessus\dao\impl\ProdutoDaoImpl.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.Object]: : Error creating bean with name ‘produtosController’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘produtosController’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘produtoDaoImpl’ defined in file [D:\VRaptor\Projects.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\accessus\WEB-INF\classes\br\com\tecnopolis\accessus\dao\impl\ProdutoDaoImpl.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.Object]: : Error creating bean with name ‘produtosController’: Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘produtosController’: Requested bean is currently in creation: Is there an unresolvable circular reference?
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:591)
org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:193)[/color]
Desculpem por nao ter tirado os comentários, já que fiz apenas um copy/paste, e tem mais a questão de resolver isso rapidin.
Se quiser xingar pode xingar, mas não xinga a mãe, blz !!
Abraço e desculpe aí qualquer coisa…
Marcelo