2017-03-20 105 views
0

我developp與Spring MVC的4 Web應用程序的DAO層預選賽豆,我添加了一個類GenericDAO:`公共類GenericDAO {錯誤:未找到相關性

@PersistenceContext 
private EntityManager entityManager; 

private Class<T> parametrizedType; 

public GenericDAO(Class<T> parametrizedType) { 
    this.parametrizedType = parametrizedType; 
} 

public EntityManager getEntityManager() { 
    return entityManager; 
} 

public List<T> executeQuery(String query){ 
    TypedQuery<T> typedQuery = entityManager.createQuery(query, parametrizedType); 
    List<T> result = typedQuery.getResultList(); 
    return result; 
} 

public List<T> listAll(){ 
    List<T> result = getEntityManager().createQuery("Select t From " + parametrizedType.getSimpleName() + " t", parametrizedType).getResultList(); 
    return result; 
} 

public T getById(Object id){ 
    return getEntityManager().find(parametrizedType, id); 
} 

@SuppressWarnings("unchecked") 
public List<T> executeNativeQuery(String query){ 
    List<T> result = entityManager.createNativeQuery(query, parametrizedType).getResultList(); 
    return result; 
} 

public List<T> executeNamedQuery(String query){ 
    List<T> result = entityManager.createNamedQuery(query, parametrizedType).getResultList(); 
    return result; 
} 

@Transactional 
public void save(T entity){ 
    entityManager.persist(entity); 
} 

@Transactional 
public void modify(T entity){ 
    entityManager.merge(entity); 
} 

@Transactional 
public T find(Object id){ 
    if(id!=null){ 
     return entityManager.find(this.parametrizedType, id); 
    } 
    return null; 
} 

@Transactional 
public void delete(Object id){ 
    T t = find(id); 
    if(t!=null){ 
     entityManager.remove(t); 
    } 
} 

}`

聲明吾道像下面產生錯誤durring開始我的應用程序:

@Autowired 
@Qualifier(value="partenaireDao") 
private GenericDAO<VscPartenaire> partenaireDao; 

錯誤是:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency ....: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=partenaireDao)} 

在org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1463) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1094) 在有機.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056) 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)

,但它那OK with:

@Repository("partenaireDao") 

公共類PartenaireDao擴展GenericDAO {

public PartenaireDao() { 
    super(VscPartenaire.class); 
} 

}

你能告訴我它的錯誤與我的第一個聲明? 我不想爲每個實體創建dao。

感謝

回答

0

你能告訴我它的錯誤與我的第一個聲明?

註釋genericDao類,使類不是春實例化。

我不想爲每個實體創建dao。

然後加@Repository("partenaireDao")GenericDao類。

+0

感謝您的回覆,即使我將@Repository添加到GenericDAO,我也有同樣的錯誤 – user19096

+0

你能用最新的代碼更新你的問題嗎? –

+0

@Repository 公共類GenericDAO { \t \t @ PersistenceContext \t私人的EntityManager EntityManager的; \t ..... – user19096