Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8348843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:48:40+00:00 2026-06-09T07:48:40+00:00

I’m trying some code. It is an architecture Hibernate – JPA – Spring. For

  • 0

I’m trying some code.
It is an architecture Hibernate – JPA – Spring. For now, I will wish to run it in a JUnit test.

For moment, i have some exception :

GRAVE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6295eb] to prepare test instance [test.service.UserAccountServiceTest@609959]
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userAccountService': Injection of autowired dependencies failed;
...
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private test.persistence.dao.UserAccountDao test.service.impl.UserAccountServiceImpl.userAccountDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [test.persistence.dao.UserAccountDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [test.persistence.dao.UserAccountDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.    
... 43 more
20 juil. 2012 10:56:19 test.service.UserAccountServiceTest tearDownOnce
INFO: tearDownOnce()

Here the JUnit : UserServiceTest

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;

import test.jndi.ContextDatasourceCreator;
import test.persistence.entity.UserAccount;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:/spring/applicationContext.xml"})
public class UserAccountServiceTest extends Assert {

private static final Log LOG = LogFactory.getFactory().getInstance(UserAccountServiceTest.class);

@Autowired
@Qualifier("userAccountService")
private UserAccountService userAccountService;

@BeforeClass
public static void setUpOnce() {
    LOG.info("setUpOnce()");
    ContextDatasourceCreator.init();
}
@AfterClass
public static void tearDownOnce() {
    LOG.info("tearDownOnce()");
}

@Before
public void onSetUp() {
    LOG.info("onSetUp()");
}
@After
public void OnTearDown() {
    LOG.info("OnTearDown()");
}

@Test
public void testListAll() {
    List<UserAccount> allUserAccounts = userAccountService.getAllAccounts();
    for (UserAccount userAccount : allUserAccounts) {
        LOG.info(userAccount);
    }
}

}

/ Here my applicationContext /

<!-- Annotations Scan -->
<context:annotation-config/>
<context:component-scan base-package="test.service, test.persistence" />

<!-- Entity Manager Factory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="dbrefPU" />
</bean>

<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<!-- Transaction Annotations -->
<tx:annotation-driven proxy-target-class="true" />
<tx:annotation-driven transaction-manager="transactionManager" />

/ Here my source code /

GenericDao Interface :

import java.util.List;

public interface GenericDao<T extends Object> {

T save(T pojo);
void remove(Class<T> classe, int id);
void delete(T pojo);
T findById(Class<T> classe, int id);    
List<T> findAll(Class<T> classe);
List<T> findByQuery(String jpql);
}

Dao Interface :

import test.persistence.entity.UserAccount;

public interface UserAccountDao extends GenericDao<UserAccount> {

UserAccount findAccount(String matricule);
}

GenericDao Impl :

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import test.persistence.dao.GenericDao;

public abstract class GenericDaoImpl<T extends Object> implements GenericDao<T> {

@PersistenceContext
protected EntityManager em;

public T save(T pojo) {
    return em.merge(pojo);
}

public void remove(Class<T> classe, int id) {
    T pojo = findById(classe, id);
    if (pojo != null) {
        em.remove(pojo);
    }
}

public void delete(T pojo) {
    em.remove(pojo);
}

public T findById(Class<T> classe, int id) {
    return (T) em.find(classe, id);
}

public List<T> findAll(Class<T> classe) {
    StringBuffer jpql = new StringBuffer(20);
    jpql.append("from ").append(classe.getName());
    List<T> result = em.createQuery(jpql.toString()).getResultList();
    return result;
}

public List<T> findByQuery(String jpql) {
    List<T> result = em.createQuery(jpql).getResultList();
    return result;
}

}

Dao Impl :

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import test.persistence.dao.UserAccountDao;
import test.persistence.entity.UserAccount;

@Repository("userAccountDao")
public class UserAccountDaoImpl extends GenericDaoImpl<UserAccount> implements     UserAccountDao {

public UserAccount findAccount(String matricule) {

    Query query = em.createNamedQuery("UserAccount.login");
    query.setParameter("matricule", matricule);

    UserAccount account = null;
    try {
        account = (UserAccount) query.getSingleResult();
    } catch (NoResultException nre) {

    }
    return account;
}

}

Service interface :

import java.util.List;

import test.persistence.entity.UserAccount;

public interface UserAccountService {

public abstract UserAccount login(String matricule);

public abstract UserAccount register(String matricule);

public abstract UserAccount getAccountWithId(Integer id);

public abstract List<UserAccount> getAllAccounts();

}

Service Impl :

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import test.persistence.dao.UserAccountDao;
import test.persistence.entity.UserAccount;
import test.service.UserAccountService;


@Service("userAccountService") 
@Transactional
public class UserAccountServiceImpl implements UserAccountService {

@Autowired
@Qualifier("userAccountDao")
private UserAccountDao userAccountDao;

public UserAccount getAccountWithId(Integer id) {
    return userAccountDao.findById(UserAccount.class, id);
}

public UserAccount login(String matricule) {
    return userAccountDao.findAccount(matricule);
}

public UserAccount register(String matricule) {
    UserAccount account = new UserAccount();
    account.setMatricule(matricule);

    try {
        account = userAccountDao.save(account);
    } catch (Exception e) {
    }
    return account;
}

public List<UserAccount> getAllAccounts() {
    return userAccountDao.findAll(UserAccount.class);
}

}

Any idea ?
Thanks a lot !!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:48:41+00:00Added an answer on June 9, 2026 at 7:48 am

    I didn’t find the solution.
    In Maven, it is not working.

    In dynamic web project, i succeed to execute my test if i change the @PersistenceContext to an EXTENDED.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.