import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Transient;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
/**
* Generic DAO implementation class that provides basic Data access and
* manipulation functionality
*
* @author syed.ammar
*
*/
public abstract class GenericDAOImpl<T> implements GenericDAO<T> {
private static Logger logger = Logger.getLogger(GenericDAOImpl.class);
@SuppressWarnings("unchecked")
protected Class<T> clazz = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
@PersistenceContext
protected EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public T save(T entity) throws DataAccessException {
logger.debug("Merging Object of Type: " + entity.getClass().getCanonicalName());
entity = entityManager.merge(entity);
return entity;
}
@Override
public void persist(T entity) throws DataAccessException {
logger.debug("Persisting Object of Type: " + entity.getClass().getCanonicalName());
entityManager.persist(entity);
}
/**
* This method update record in database
*
* @param updatableObj
* - the updated copy to merge
* @return the updated, registered persistent instance
* */
@Override
public T update(T entity) {
return save(entity);
}
/**
* Delete the given entity.
*
* @param entityClass
* - the entityClass to delete
* */
@Override
public void delete(T entity) {
logger.debug("Delete entity of Type: " + entity.getClass().getCanonicalName());
entityManager.remove(entity);
}
/**
* Delete all given entities.
*
* @param entities
* - the entities to delete
* */
public void deleteAll(List<T> entities) {
for (T entity : entities) {
delete(entity);
}
}
/**
* Delete Entity by Id
* @param id
* - Entity Id
*/
@Override
public int deleteById(Long id) {
Query deleteQuery = entityManager.createQuery("DELETE FROM " + clazz.getName() + " entity WHERE entity.id=:id");
deleteQuery.setParameter("id", id);
return deleteQuery.executeUpdate();
}
/**
* Delete Entity by Customer
* @param customer
* - Customer Entity
*/
/* @Override
public int deleteByCustomer(Customer customer) {
Query deleteQuery = entityManager.createQuery("DELETE FROM " + clazz.getName() + " entity WHERE entity.customer=:customer");
deleteQuery.setParameter("customer", customer);
return deleteQuery.executeUpdate();
}
*/
/**
* Delete Entity by Reseller
* @param reseller
* - Reseller Entity
*/
/* @Override
public int deleteByReseller(Reseller reseller) {
Query deleteQuery = entityManager.createQuery("DELETE FROM " + clazz.getName() + " entity WHERE entity.customer.reseller=:reseller");
deleteQuery.setParameter("reseller", reseller);
return deleteQuery.executeUpdate();
}
*/
/**
* Read the entity instance of the given class with the given id, throwing
* an exception if not found. Retrieves read-write objects from the
* hibernate UnitOfWork in case of a non-read-only transaction, and
* read-only objects else.
* @param id
* - Identity key of the desired object
* @return the entity instance
* */
@Override
public T getById(Long id) {
logger.debug("getById:" + id + ", T class: " + clazz );
return entityManager.find(clazz, id);
}
@Override
public List<T> getResultList(String jpql, Map<String, Object> queryParams) {
logger.debug("Query: " + jpql);
Query query = entityManager.createQuery(jpql.toString());
for(Map.Entry<String, Object> entry : queryParams.entrySet()){
query.setParameter(entry.getKey(), entry.getValue());
}
@SuppressWarnings("unchecked")
List<T> resultList = (List<T>) query.getResultList();
return resultList;
}
/**
* Generic search method without sort column and direction which searches
* the record depending upon property values set in the passed model bean.
* This method has been deprecated and will be removed soon. Create Query
* specific methods in related Entity DAO's instead.
*
* @param entity
* Entity with data set for criteria search
* @return List of searched model beans by applying the passed values of
* model bean as criteria
*/
@Override
@Deprecated
public List<T> search(T entity) {
return search(entity, null, null);
}
/**
* Generic search method which searches the record depending upon property
* values set in the passed model bean.
* This method has been deprecated and will be removed soon. Create Query
* specific methods in related Entity DAO's instead.
*
* @param entity
* Entity with data set for criteria search
* @param sortByColumn
* Name of column to be used for sorting result
* @param sortDirection
* 1: ASC 2:DESC
* @return List of searched model beans by applying the passed values of
* model bean as criteria
*/
@Override
@Deprecated
@SuppressWarnings("unchecked")
public List<T> search(T entity, String sortByColumn, Long sortDirection) {
List<Object> params = new ArrayList<Object>();
String query = "SELECT entity FROM " + clazz.getName() + " entity";
Field[] publicFields = entity.getClass().getDeclaredFields();
for (Field field : publicFields) {
field.setAccessible(true);
Object value = null;
try {
value = field.get(entity);
} catch (IllegalArgumentException e) {
logger.error("", e);
} catch (IllegalAccessException e) {
logger.error("", e);
}
if (value == null
|| Collection.class.isAssignableFrom(field.getType())
|| java.lang.reflect.Modifier.isStatic(field.getModifiers())
|| field.getAnnotation(Transient.class) != null) {
continue;
}
if (field.getType().equals(String.class)) {
if (query.contains("where")) {
query = query + " and " + field.getName() + " like ?"
+ params.size();
} else {
query = query + " where " + field.getName() + " like ?"
+ params.size();
}
params.add("%" + value.toString() + "%");
} else if (field.getType().equals(Date.class)) {
Date startDateTime = null;
Date endDateTime = null;
String startTime = DATE_FORMAT.format(value) + " 00:00:00";
String endTime = DATE_FORMAT.format(value) + " 23:59:59";
try {
startDateTime = DATE_TIME_FORMAT.parse(startTime);
endDateTime = DATE_TIME_FORMAT.parse(endTime);
} catch (ParseException e) {
logger.error("", e);
}
if (startDateTime == null || endDateTime == null) {
continue;
}
if (query.contains("where")) {
query = query + " and " + field.getName() + " between ?"
+ params.size() + " and ?" + (params.size() + 1);
} else {
query = query + " where " + field.getName() + " between ?"
+ params.size() + " and ?" + (params.size() + 1);
}
params.add(startDateTime);
params.add(endDateTime);
} else {
if (query.contains("where")) {
query = query + " and " + field.getName() + " = "
+ params.size();
} else {
query = query + " where " + field.getName() + " = ?"
+ params.size();
}
params.add(value);
}
}
Query queryObj = entityManager.createQuery(query);
// Check is sorting parameters are not not blank or null, apply sorting
// criteria
if (sortByColumn != null && sortDirection != null) {
if (sortDirection == 1L) {
query = query + " Order By " + sortByColumn + " ASC";
} else {
query = query + " Order By " + sortByColumn + " DESC";
}
if (params.size() > 0) {
for (int i = 0; i < params.size(); i++) {
queryObj.setParameter(i, params.get(i));
}
}
}
return queryObj.getResultList();
}
@Override
public List<T> getAll() {
return getAll(null, null);
}
@Override
public List<T> getAll(String sortByColumn, Long sortDirection) {
String query = "SELECT entity FROM " + clazz.getName() + " entity";
if (sortByColumn != null && sortDirection != null) {
if (sortDirection == 1L) {
query = query + " Order By " + sortByColumn + " ASC";
} else {
query = query + " Order By " + sortByColumn + " DESC";
}
}
@SuppressWarnings("unchecked")
List<T> resultList = (List<T>) entityManager.createQuery(query).getResultList();
return resultList;
}
/**
* Generic find method which identify the exact records depending upon
* property values set in the passed model bean else null will be returned
*
* @param entity
* Entity with data set for criteria search
* @return List of identified model beans by applying the passed values of
* model bean as criteria
*/
@Override
@SuppressWarnings("unchecked")
public List<T> find(T entity) {
List<Object> params = new ArrayList<Object>();
String query = "SELECT entity FROM " + entity.getClass().getName()
+ " entity";
Field[] publicFields = entity.getClass().getDeclaredFields();
for (Field field : publicFields) {
field.setAccessible(true);
Object value = null;
try {
value = field.get(entity);
} catch (IllegalArgumentException e) {
logger.error("", e);
} catch (IllegalAccessException e) {
logger.error("", e);
}
if (value == null
|| Collection.class.isAssignableFrom(field.getType())
|| java.lang.reflect.Modifier.isStatic(field.getModifiers())
|| field.getAnnotation(Transient.class) != null) {
continue;
}
if (field.getType().equals(Date.class)) {
Date startDateTime = null;
Date endDateTime = null;
String startTime = DATE_FORMAT.format(value) + " 00:00:00";
String endTime = DATE_FORMAT.format(value) + " 23:59:59";
try {
startDateTime = DATE_TIME_FORMAT.parse(startTime);
endDateTime = DATE_TIME_FORMAT.parse(endTime);
} catch (ParseException e) {
logger.error("", e);
}
if (startDateTime == null || endDateTime == null) {
continue;
}
if (query.contains("where")) {
query = query + " and " + field.getName() + " between ?"
+ params.size() + " and ?" + (params.size() + 1);
} else {
query = query + " where " + field.getName() + " between ?"
+ params.size() + " and ?" + (params.size() + 1);
}
params.add(startDateTime);
params.add(endDateTime);
} else {
if (query.contains("where")) {
query = query + " and " + field.getName() + " = ?"
+ params.size();
} else {
query = query + " where " + field.getName() + " = ?"
+ params.size();
}
params.add(value);
}
}
if (params.size() > 0) {
Query queryObj = entityManager.createQuery(query);
for (int i = 0; i < params.size(); i++) {
queryObj.setParameter(i, params.get(i));
}
return queryObj.getResultList();
} else {
return null;
}
}
}
///自动连线
@Controller
public class CourseController {
// @Autowired
// private SearchService searchService;
@Autowired
CourseDAOImp courseDAO;
//..............
}
SEVERE:将上下文初始化事件发送给侦听器的异常 类的实例 org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException:错误 创建名称为&#39; courseController&#39;:注入自动装配的bean 依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:不能 autowire字段: com.softech.ls360.lcms.contentbuilder.dao.impl.CourseDAOImp com.softech.ls360.lcms.contentbuilder.web.controller.CourseController.courseDAO; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 匹配类型的bean [com.softech.ls360.lcms.contentbuilder.dao.impl.CourseDAOImp]发现 对于依赖:预计至少有1个bean有资格作为autowire 这种依赖的候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)} 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 在 org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:291) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) 在 org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276) 在 org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197) 在 org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) 在 org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791) 在 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285) 在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 在 org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) 在 org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) 在 org.apache.catalina.core.StandardHost.addChild(StandardHost.java:618) 在 org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:963) 在 org.apache.catalina.startup.HostConfig $ DeployWar.run(HostConfig.java:1600) 在 java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:471) 在java.util.concurrent.FutureTask.run(FutureTask.java:262)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:744)引起: org.springframework.beans.factory.BeanCreationException:不能 autowire字段: com.softech.ls360.lcms.contentbuilder.dao.impl.CourseDAOImp com.softech.ls360.lcms.contentbuilder.web.controller.CourseController.courseDAO; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 匹配类型的bean [com.softech.ls360.lcms.contentbuilder.dao.impl.CourseDAOImp]发现 对于依赖:预计至少有1个bean有资格作为autowire 这种依赖的候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)} 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502) 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282) ... 26更多引起: org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 匹配类型的bean [com.softech.ls360.lcms.contentbuilder.dao.impl.CourseDAOImp]发现 对于依赖:预计至少有1个bean有资格作为autowire 这种依赖的候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)} 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474) ......还有28个
答案 0 :(得分:1)
你没有注册名为“courseController”的bean。首先将该类注册为bean。你注册那个没有注册courseController。通过添加@ComponenetScan注释,您可以扫描所有bean,spring将注册它们。
答案 1 :(得分:1)
您应该在xml中定义bean
或者给entry of <context:component-scan base-package=" " />
答案 2 :(得分:0)
我认为问题是DAO没有注入Controller,我在这个DAO类中添加了另一层服务和自动服务,它对我来说很好。
我不需要在我的情况下定义bean是因为我正在为此目的使用Annotation并且正在扫描整个包。
感谢,