ecache configuration in spring throws: "Named query not known Error"

221 Views Asked by At

Trying to run a unit test on a Dao in my Spring app I'm getting error: Named query not known. Actually I'm trying to implement cache with spring 4.2.

Here is the code that is relevant to the integration: Have a look at my test cases.

My test class: 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");
  }
}

The error i'm getting

Stack Trace:

  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)

My dao-context-config.xml defines:

<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"/> 

and my 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);
}

My Pojo:QuestionType

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

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

I have a valid ehcache.xml as i think... My ehcache.xml Defines:

 <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"/>

And my junit console have following:

[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

Does anyone have an idea of what I'm doing wrong? It seems like a pretty straight forward implementation that is working on all of the tutorials I've seen yet for some reason I cannot get the app to start while test is performed. Any help will be appreciated. Thank you.

0

There are 0 best solutions below