agilob agilob - 2 months ago 20
Java Question

NoSuchBeanDefinitionException: No qualifying bean of type JpaVendorAdapter found for dependency

I'm trying to setup Java-based hibernate in memory database and start using it from a test, but when Hibernate beans are loaded Application context fail with. The point if this exercise is to setup simple service with CRUD operations using CrudRepository for a few classes and WRITE


Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.orm.jpa.JpaVendorAdapter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 72 more


Related configuration and source code:

hibernate.properties

jdbc.driverClassName = org.hsqldb.jdbcDriver
jdbc.url = jdbc:hsqldb:mem:press
jdbc.username = user
jdbc.password =
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.show_sql = false
hibernate.format_sql = false


HibernateConfig.java - which fails to load

@Configuration
@EnableTransactionManagement
@PropertySource(value = { "classpath:hibernate.properties" })
public class HibernateConfig {

@Autowired
private Environment environment;

@Bean
public EntityManagerFactory entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dataSource);
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan("net.agilob.press.entity");
return lef.getNativeEntityManagerFactory();
}

@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "net.agilob.press.entity" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}

private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("create-drop"));

return properties;
}

@Bean
@Autowired
public HibernateTemplate getHibernateTemplate(SessionFactory sessionFactory) {
HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
return hibernateTemplate;
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}


AppConfig.java - file loaded as configuration for test

@Configuration
@ComponentScan(basePackages = "net.agilob.press.entity")
@EnableJpaRepositories(basePackages = "net.agilob.press.entity.repository")
@Import(HibernateConfig.class)
public class AppConfig {

@Bean
public PostService getPostService() {
return new PostService();
}
}


PostServiceTest.java - this one should create instance(s) of Post, save it in memory database and extract later (just to see it works):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
@Transactional
public class PostDAOTest {

@Autowired
private PostService postService;

@Before
public void setUp() {
Post post = new Post();
post.setContent("body");
post.setDescription("description");
post.setIntroduction("introduction");
post.setTitle("title");
post.setTitleBrowser("title browser");
postRepository.save(post);
}

@Test
public void testList() {
// checks if spring wiring was correct
assertNotNull("Problem with spring beans", postRepository);
assertFalse(postRepository.findTop10ByTitleOrderByIdAsc().isEmpty());
}
}


PostService:

@Service("postService")
@Transactional
public class PostService {

@Autowired
private PostRepository postRepository;

...
}


PostRepository:

public interface PostRepository extends BaseRepository<Post, Long> {
}


I tried to create one more @Beans annotated method in HibernateConfig that was bulding JpaVendorAdapter instance but when I run the test it was failing with:


Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.persistence.EntityManager]: Factory method 'createSharedEntityManager' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 59 more
Caused by: java.lang.NullPointerException
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.initProxyClassLoader(SharedEntityManagerCreator.java:199)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.<init>(SharedEntityManagerCreator.java:191)
at org.springframework.orm.jpa.SharedEntityManagerCreator.createSharedEntityManager(SharedEntityManagerCreator.java:163)
at org.springframework.orm.jpa.SharedEntityManagerCreator.createSharedEntityManager(SharedEntityManagerCreator.java:120)
at org.springframework.orm.jpa.SharedEntityManagerCreator.createSharedEntityManager(SharedEntityManagerCreator.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 60 more


my pom.xml

so I think something else is wrong in the setup.

Any help how to design and setup it in a better way very appreciated.

skm skm
Answer

In order for your entityManagerFactory to know the provider class as well as jpa dialect, you need to set jpaVendorAdaptor on your entityManagerFactory bean. This can be done as

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setShowSql(true);
    jpaVendorAdapter.setDatabase(Database.HSQL);
    jpaVendorAdapter.setDatabasePlatform(MySQL5Dialect.class.getName());
    jpaVendorAdapter.setGenerateDdl(false);
    return jpaVendorAdapter;
}

and calling this method inside your entityManagerFactory as follows :

lef.setJpaVendorAdapter(jpaVendorAdapter());
Comments