2

尝试在我的 Spring 应用程序中对 Dao 运行单元测试时出现错误:命名查询未知。实际上我正在尝试用spring 4.2实现缓存。

这是与集成相关的代码:看看我的测试用例。

我的测试课:QuestionDaoTest

 public class QuestionDaoTest extends BaseDaoTest {

 @Autowired
 private QuestionTypeDao questionTypeDaoImpl;

 private static final Logger logger =LogManager.getLogger(QuestionDaoTest.class);

  @Test
  @Transactional
  public void test() {
  QuestionType questionType = 
    questionTypeDaoImpl.getQuestionTypeIdByName("Single Choice");
    logger.debug("Firing Query");
  }
}

我得到的错误

堆栈跟踪

  org.springframework.orm.hibernate4.HibernateSystemException: Named query not known: getQuestionTypeIdByName;
  nested exception is org.hibernate.MappingException: Named query not known: getQuestionTypeIdByName at org.springframework.orm.hibernate4
  .SessionFactoryUtils.convertHibernateAccessException
  (SessionFactoryUtils.java:218)at org.springframework.orm.hibernate4
  .HibernateExceptionTranslator.convertHibernateAccessException
  (HibernateExceptionTranslator.java:57)

我的dao-context-config.xml定义:

<context:component-scan base-package="com.oea.dao" />
<cache:annotation-driven />
<tx:annotation-driven transaction-manager="transactionManager" />

 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" 
     p:cacheManager-ref="ehcache"/>

<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
     p:configLocation="classpath:ehcache.xml"  
     p:shared="true"/> 

和我的 DaoImpl:QuestionTypeDaoImpl

 @Repository
 public class QuestionTypeDaoImpl extends AbstractDao implements 
 QuestionTypeDao {

   @Cacheable("person")
   @Override
   public QuestionType getQuestionTypeIdByName(String questionType) {
     Query query = getSession().getNamedQuery("getQuestionTypeIdByName");
     query.setString("questionType", questionType);
     List<QuestionType> questionTypesId = query.list();
        logger.debug("Question Type name  " + questionTypesId + " in database..");

    }
    return questionTypesId.get(0);
}

我的 Pojo:问题类型

 @NamedQueries({ @NamedQuery(name = "getQuestionTypeIdByName", query = "from 
 QuestionType qt where qt.questionType = :questionType ") })

 @Entity
 @Table(name = "question_type")
 public class QuestionType {}

我认为我有一个有效的 ehcache.xml... 我的ehcache.xml定义:

 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<!-- <defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />
<cache name="employee" maxElementsInMemory="10000" eternal="true" overflowToDisk="false" /> -->
<diskStore path="java.io.tmpdir"/>
<cache name="person"
       maxElementsInMemory="100"
       eternal="false"
       timeToIdleSeconds="120"
       timeToLiveSeconds="120"
       overflowToDisk="true"
       maxElementsOnDisk="10000000"
       diskPersistent="false"
       diskExpiryThreadIntervalSeconds="120"
       memoryStoreEvictionPolicy="LRU"/>

我的junit控制台有以下内容:

[main] INFO org.springframework.cache.ehcache.EhCacheManagerFactoryBean - 
Initializing EhCache CacheManager
[main] INFO org.springframework.test.context.transaction.TransactionContext 
- Began transaction (1) for test context [DefaultTestContext@4d654825 
testClass = QuestionDaoTest, testInstance = 
com.soe.dao.test.QuestionDaoTest@3bfc6a5e, testMethod = 
test@QuestionDaoTest, testException = [null], mergedContextConfiguration = 
[MergedContextConfiguration@23940f86 testClass = QuestionDaoTest, locations 
= '{classpath:/soe-dao-context-test.xml}', classes = '{}', 
contextInitializerClasses = '[]', activeProfiles = '{}', 
propertySourceLocations = '{}', propertySourceProperties = '{}', 
contextCustomizers = set[[empty]], contextLoader = 
'org.springframework.test.context.support.DelegatingSmartContextLoader', 
parent = [null]]]; transaction manager 
[org.springframework.orm.hibernate4.HibernateTransactionManager@7e94d093]; 
rollback [false]
[main] INFO org.springframework.test.context.transaction.TransactionContext 
- Committed transaction for test context [DefaultTestContext@4d654825 
testClass = QuestionDaoTest, testInstance = 
com.soe.dao.test.QuestionDaoTest@3bfc6a5e, testMethod = 
test@QuestionDaoTest, testException = 
org.springframework.orm.hibernate4.HibernateSystemException: Named query not 
known: getQuestionTypeIdByName; nested exception is 
org.hibernate.MappingException: Named query not known: 
getQuestionTypeIdByName, mergedContextConfiguration = 
[MergedContextConfiguration@23940f86 testClass = QuestionDaoTest, locations 
= '{classpath:/soe-dao-context-test.xml}', classes = '{}', 
contextInitializerClasses = '[]', activeProfiles = '{}', 
propertySourceLocations = '{}', propertySourceProperties = '{}', 
contextCustomizers = set[[empty]], contextLoader = 
'org.springframework.test.context.support.DelegatingSmartContextLoader', 
parent = [null]]].
[Thread-3] INFO 
org.springframework.context.support.GenericApplicationContext - Closing 
org.springframework.context.support.GenericApplicationContext@35841320: 
startup date [Thu Aug 03 11:37:56 IST 2017]; root of context hierarchy2017-
08-03 11:38:16,182 pool-1-thread-1 DEBUG Stopping 
LoggerContext[name=16b98e56, 
org.apache.logging.log4j.core.LoggerContext@2cb4893b]

2017-08-03 11:38:16,183 pool-1-thread-1 DEBUG Stopping 
LoggerContext[name=16b98e56, 
org.apache.logging.log4j.core.LoggerContext@2cb4893b]...
[Thread-3] INFO org.springframework.cache.ehcache.EhCacheManagerFactoryBean 
- Shutting down EhCache CacheManager
2017-08-03 11:38:16,187 pool-1-thread-1 DEBUG Shutting down 
RollingFileManager C:/src/personal/logs/monitor.log

有谁知道我做错了什么?这似乎是一个非常简单的实现,它适用于我见过的所有教程,但由于某种原因,我无法在执行测试时启动应用程序。任何帮助将不胜感激。谢谢

4

0 回答 0