1
I’m trying to implement a generic CDI DAO. Apparently I’m doing it right, but I’m getting the following error:
cannot be cast to java.lang.reflect.Parameterizedtype
This error is pointing me to this line of my Repository class:
Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
Irepository interface code
package br.com.amarildo.repository;
import java.util.List;
public interface IRepository<T> {
public T porId(Long id);
public T guardar(T entity);
public void alterar(T entity);
public void remover(T entity);
public List<T> todos();
}
Repository class code
package br.com.amarildo.repository;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
public class Repository<T> implements IRepository<T> {
protected final EntityManager manager;
private final Class<T> clazz;
@SuppressWarnings("unchecked")
@Inject
public Repository(EntityManager manager) {
this.manager = manager;
this.clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public T porId(Long id) {
return manager.find(clazz, id);
}
public T guardar(T entity) {
return this.manager.merge(entity);
}
public void alterar(T entity) {
this.manager.merge(entity);
}
public void remover(T entity) {
this.manager.remove(entity);
}
public List<T> todos() {
TypedQuery<T> query = manager.createQuery("from " + clazz.getName(), clazz);
List<T> resultList = query.getResultList();
return resultList;
}
}
class Testerepository code
package br.com.amarildo.repository;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import br.com.amarildo.model.FuncionarioModel;
public class TesteRepository extends Repository<FuncionarioModel> implements Serializable {
private static final long serialVersionUID = 1L;
public TesteRepository(EntityManager manager) {
super(manager);
}
public List<String> cargosQueContem(String cargo) {
TypedQuery<String> query = super.manager.createQuery("select distinct cargo from FuncionarioModel " + "where upper(cargo) like upper(:cargo)", String.class);
query.setParameter("cargo", "%" + cargo + "%");
return query.getResultList();
}
}
class code Functioncontroller where I run Testerepository
package br.com.amarildo.controller;
import java.io.Serializable;
import javax.inject.Inject;
import br.com.amarildo.model.FuncionarioModel;
import br.com.amarildo.repository.TesteRepository;
import br.com.amarildo.util.NegocioException;
import br.com.amarildo.util.Transactional;
public class FuncionarioController implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private TesteRepository funcionarioRepository;
@Transactional
public FuncionarioModel salvar(FuncionarioModel funcionarioModel) throws NegocioException {
if (funcionarioModel.getCargo().isEmpty() ) {
throw new NegocioException("Não é Possivel Salvar Funcionario sem Cargo");
}
return this.funcionarioRepository.guardar(funcionarioModel);
}
@Transactional
public void atualizar(FuncionarioModel funcionarioModel) throws NegocioException {
if (funcionarioModel.getCargo().isEmpty()) {
throw new NegocioException("Não é possível fazer a Alteração campo cargo está vazio !");
}
this.funcionarioRepository.alterar(funcionarioModel);
}
@Transactional
public void excluir(FuncionarioModel funcionarioModel) throws NegocioException {
funcionarioModel = this.funcionarioRepository.porId(funcionarioModel.getCodigo());
if (funcionarioModel.getCargo()== null) {
throw new NegocioException("Não é possível excluir um Funcionario Demitido!");
}
this.funcionarioRepository.remover(funcionarioModel);
}
}
How to solve this?
Follow the error I’m getting:
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type TesteRepository with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private br.com.amarildo.controller.FuncionarioController.funcionarioRepository
at br.com.amarildo.controller.FuncionarioController.funcionarioRepository(FuncionarioController.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518)
at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:63)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
... 4 more
jun 29, 2017 10:26:45 AM org.apache.catalina.core.ContainerBase startInternal
GRAVE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
at java.util.concurrent.FutureTask.report(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:441)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:787)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.startup.Catalina.start(Catalina.java:629)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:351)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:485)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:924)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
Thanks to all the error It was solved as below: I had to do the injection in my Testerepository Builder of the Testerepository class was just that.
@Inject
public TesteRepository(EntityManager manager) {
super(manager);
}
Thank you very much Victor. I’ll check every step you’ve given me .
– JavaX_javaX
I posted the whole sequence of my code above I still could not identify the error if someone can tell me ta complicated. vlw
– JavaX_javaX
am receiving the following error now = WELD-001408: Unsatisfied dependencies for type Testerepository with Qualifiers @Default
– JavaX_javaX