[Resolvido]Como acessar os métodos do HibernateTemplate em um DAO estendido do Generic

4 respostas
M

Olá, pessoal.

Preciso de um método na minha classe FuncionarioDAO(estendido de uma classe genérica) que retorne uma lista através de um consulta HQL, mas só dá “java.lang.NullPointerException”;

PS: Os métodos que são estendidos do Generic estão todos ok.

O método em questão é o “listarNomes”:

package br.com.techfor.nasa.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import br.com.techfor.nasa.model.Funcionario;

@Repository
public class FuncionarioDAOImpl extends GenericDAOImpl<Funcionario, Long>
		implements FuncionarioDAO {

	public FuncionarioDAOImpl(Class<Funcionario> classe) {
		super(classe);
		// TODO Auto-generated constructor stub
	}

	@Autowired
	public FuncionarioDAOImpl(SessionFactory sessionFactory) {
		this(Funcionario.class);
		setSessionFactory(sessionFactory);
	}

	public List<Funcionario> listarNomes(String nome) {

		String hql = "from Funcionario f where f.nome like '%" + nome + "%'";

		return getHibernateTemplate().find(hql);
	}
}

4 Respostas

renandemelo

Cara, está dando erro nessa linha?

return getHibernateTemplate().find(hql);

Pode colar o stacktrace do nullpointer?

M
É claro, mas mudou o erro, pois mudei o método de teste.
java.lang.IllegalStateException: Failed to load ApplicationContext
	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'genericDAOImpl' defined in class path resource [br/com/techfor/nasa/teste/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [br.com.techfor.nasa.dao.GenericDAOImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: br.com.techfor.nasa.dao.GenericDAOImpl.<init>()
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
	at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
	... 24 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [br.com.techfor.nasa.dao.GenericDAOImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: br.com.techfor.nasa.dao.GenericDAOImpl.<init>()
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:70)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
	... 38 more
Caused by: java.lang.NoSuchMethodException: br.com.techfor.nasa.dao.GenericDAOImpl.<init>()
	at java.lang.Class.getConstructor0(Unknown Source)
	at java.lang.Class.getDeclaredConstructor(Unknown Source)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:65)
	... 39 more

Classe do teste:

package br.com.techfor.nasa.teste;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import br.com.techfor.nasa.dao.FuncionarioDAOImpl;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "applicationContext.xml")
public class TesteDAO {

	private FuncionarioDAOImpl funcionarioDAOImpl;

	@Autowired
	public void setFuncionarioDAOImpl(FuncionarioDAOImpl funcionarioDAOImpl) {
		this.funcionarioDAOImpl = funcionarioDAOImpl;
	}

	@Test
	public void getAllTest() {
		assertNotNull(funcionarioDAOImpl);
		assertNotNull(funcionarioDAOImpl.listarNomes("SHAZAN"));

	}

}
M

Disfarcem… :oops: :oops:

O erro tava no applicationContext:

O id do bean tava pegando o Genérico. Agora, troquei para :

renandemelo

Blz cara, qualquer coisa dá um toque! :slight_smile:

Criado 21 de junho de 2011
Ultima resposta 21 de jun. de 2011
Respostas 4
Participantes 2