I am trying to use arquillian-persistence-dbunit where I have to seed the database with datas but none of my solutions work. My project is based on Spring and we are using JavaConfig.
In my POM, I have the arquillian jars :
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.10.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Arquillian dependencies -->
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-persistence-dbunit</artifactId>
<version>1.0.0.Alpha7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-service-integration-spring-inject</artifactId>
<version>1.1.0.Alpha1</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-service-integration-spring-javaconfig</artifactId>
<version>1.1.0.Alpha1</version>
</dependency>
My test class :
#RunWith(Arquillian.class)
#Transactional(PersistanceTestConfig.TRANSACTION_NAME)
#SpringAnnotationConfiguration(classes = {ArquillianConfiguration.class})
#DataSource(PersistanceTestConfig.MY_DATASOURCE)
#UsingDataSet({"database/myDataset.yml"})
public class MyTestClass{
#Deployment(testable = false)
public static WebArchive createArchive(){
//returns a webArchive
}
#Test
public void myTestMethod(){
//do something
}
}
My config classes :
#Configuration
#Import({PersistanceTestConfig.class})
#ComponentScan(basePackages = {"com.myapp.dao", ""com.myapp.services"})
#EnableCaching
#EnableTransactionManagement
public class ArquillianConfiguration {}
And the PersistenceTestConfig :
public class PersistanceTestConfig {
public static final String TRANSACTION_NAME = "transaction";
public static final String MY_DATASOURCE = "datasource";
private Properties getJPAProperties() {
final Properties jpaProperties = new Properties();
jpaProperties.setProperty("hibernate.hbm2ddl.auto", "create");
jpaProperties.setProperty("hibernate.show_sql", "true");
jpaProperties.setProperty("hibernate.format_sql", "true");
jpaProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
return jpaProperties;
}
#Bean(name = MY_DATASOURCE)
public DataSource getDataSource(){
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.continueOnError(false).ignoreFailedDrops(true).build();
}
#Bean
#Autowired
public LocalContainerEntityManagerFactoryBean getEntityManagerREFCS( DataSource dataSource) {
final LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setDataSource(dataSource);
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
localContainerEntityManagerFactoryBean.setPackagesToScan("com.myapp.dao.beans");
localContainerEntityManagerFactoryBean.setPersistenceUnitName("pUnitName");
localContainerEntityManagerFactoryBean.setJpaProperties(getJPAProperties());
return localContainerEntityManagerFactoryBean;
}
}
I have tried many solutions, but I still have the same error :
org.jboss.arquillian.transaction.impl.lifecycle.TransactionProviderNotFoundException: Transaction provider for given test case has not been found.
What bothers me is there are not so many documentations on how to use arquillian-persistence-dbunit with JavaConfig. The only example that I see are badsed on xml configuration only. For instance, in this project.
Has Anyone used arquillian-persistence-dbunit with JavaConfig.
Related
Then I build and dismantle my project in several ways, I find this error which has me stuck from one moment to the next started to come out and tells me that I can't import one of the java configuration files (the one for Hibernate).
The project is composed of a .ear, which contains a .jar, a .ejb and the web application.
Please help with the following error:
ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "ear-1.0.0.ear")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.subunit.\"ear-1.0.0.ear\".\"web-1.0.0.war\".undertow-deployment" => "java.lang.RuntimeException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.creatics.minstituto.config.HibernateConfig]; nested exception is java.lang.IllegalArgumentException: class org.springframework.context.annotation.AutoProxyRegistrar is not assignable to interface org.springframework.context.annotation.ImportBeanDefinitionRegistrar
Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.creatics.minstituto.config.HibernateConfig]; nested exception is java.lang.IllegalArgumentException: class org.springframework.context.annotation.AutoProxyRegistrar is not assignable to interface org.springframework.context.annotation.ImportBeanDefinitionRegistrar
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.creatics.minstituto.config.HibernateConfig]; nested exception is java.lang.IllegalArgumentException: class org.springframework.context.annotation.AutoProxyRegistrar is not assignable to interface org.springframework.context.annotation.ImportBeanDefinitionRegistrar
Caused by: java.lang.IllegalArgumentException: class org.springframework.context.annotation.AutoProxyRegistrar is not assignable to interface org.springframework.context.annotation.ImportBeanDefinitionRegistrar"}}
These are the spring and hibernate libraries of the pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.4.10.Final</version>
</dependency>
This is my SpringInitializer:
public class SpringInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
// Load database and spring security configuration
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { HibernateConfig.class, SpringSecurityConfig.class };
}
// Load spring web configuration
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { SpringWebConfig.class };
}
#Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
This is my HibernateConfig
#Configuration
#PropertySource({ "classpath:application.properties", "classpath:hibernate.properties" })
#EnableTransactionManagement
#ComponentScans(value = {
#ComponentScan("com.creatics.dao"), #ComponentScan("com.creatics.minstituto.dao"),
#ComponentScan("com.creatics.service"), #ComponentScan("com.creatics.minstituto.service")
})
public class HibernateConfig {
#Value("${metadata.jndi}")
private String metadataJNDI;
#Value("${hibernate.dialect}")
private String dialect;
#Value("${hibernate.show_sql}")
private String showSql;
#Value("${hibernate.hbm2ddl.auto}")
private String hbm2ddlAuto;
#Value("${hibernate.c3p0.min_size}")
private String c3p0MinSize;
#Value("${hibernate.c3p0.max_size}")
private String c3p0MaxSize;
#Value("${hibernate.c3p0.acquire_increment}")
private String c3p0AcquireIncrement;
#Value("${hibernate.c3p0.timeout}")
private String c3p0Timeout;
#Value("${hibernate.c3p0.max_statements}")
private String c3p0MaxStatements;
#Bean
public HibernateTransactionManager getTransactionManager() throws Exception {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(getSessionFactory());
return transactionManager;
}
#Bean
public SessionFactory getSessionFactory() throws Exception {
LocalSessionFactoryBuilder sessionFactoryBuilder = new LocalSessionFactoryBuilder(getDataSource());
sessionFactoryBuilder.scanPackages("com.creatics.minstituto.model");
sessionFactoryBuilder.addProperties(getHibernateProperties());
return sessionFactoryBuilder.buildSessionFactory();
}
#Bean
public DataSource getDataSource() throws Exception {
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
dataSourceLookup.setResourceRef(true);
return dataSourceLookup.getDataSource(metadataJNDI);
}
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put(DIALECT, dialect);
properties.put(SHOW_SQL, showSql);
properties.put(HBM2DDL_AUTO, hbm2ddlAuto);
properties.put(C3P0_MIN_SIZE, c3p0MinSize);
properties.put(C3P0_MAX_SIZE, c3p0MaxSize);
properties.put(C3P0_ACQUIRE_INCREMENT, c3p0AcquireIncrement);
properties.put(C3P0_TIMEOUT, c3p0Timeout);
properties.put(C3P0_MAX_STATEMENTS, c3p0MaxStatements);
return properties;
}
}
this is hibernate.properties:
## jndi Properties
metadata.jndi = java:/PostgresDS/database
## Hibernate Properties
hibernate.dialect = org.hibernate.dialect.PostgreSQL95Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=validate
#C3P0 properties
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=150
I try to create Spring data jpa program without SpringBoot.
Configuration class:
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(basePackages = {"springdata.repository"})
#ComponentScan(basePackages = {"springdata.entities.service"})
#PropertySource("classpath:data.properties")
public class SpringDataConfig {
#Autowired
private Environment env;
#Bean
public DataSource dataSource() {
final BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setUrl(env.getProperty("jdbc.url"));
basicDataSource.setUsername(env.getProperty("jdbc.user"));
basicDataSource.setPassword(env.getProperty("jdbc.password"));
return basicDataSource;
}
#Bean
public Properties hibernateProperties() {
final Properties hibernateProp = new Properties();
hibernateProp.put("hibernate.dialect", "org.hibernate.H2Dialect");
hibernateProp.put("hibernate.format_sql", true);
hibernateProp.put("hibernate.use_sql_comments", true);
hibernateProp.put("hibernate.show_sql", true);
hibernateProp.put("hibernate.hbm2ddl.auto", "update");
hibernateProp.put("hibernate.max_fetch_depth", 3);
hibernateProp.put("hibernate.jdbc.batch_size", 10);
hibernateProp.put("hibernate.jdbc.fetch_size", 50);
return hibernateProp;
}
#Bean
public JpaVendorAdapter jpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
#Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(final JpaVendorAdapter jpaVendorAdapter,
final Properties hibernateProperties,
final DataSource dataSource) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPackagesToScan("springdata.entities");
em.setDataSource(dataSource);
em.setJpaVendorAdapter(jpaVendorAdapter);
em.setJpaProperties(hibernateProperties);
em.afterPropertiesSet();
return em;
}
}
Test class:
public class Test {
public static void main(final String[] args) throws SQLException {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringDataConfig.class);
DuckService duckService = ctx.getBean(DuckService.class);
System.out.println(duckService.findAll().size());
}
}
maven dependency:
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.1.0.Alpha2</version>
</dependency>
</dependencies>
And there are exception:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in springdata.SpringDataConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
I try to find decision of this proplem, and on SO was similar question, and solution was to replace javax.persistance on hibernate.javax.persistance. But in my case, I have new exception.
Change hibernate dialect
hibernateProp.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
I'm new in Java! When I run the Junit test the console show me exception and I don't know how to resolve this. What is wrong with my Hibernate configuration? This code perfect works with H2 database, but when I change database and config parameters to Mysql 8.0 the Eclipse return exception. I hope that someone can give me useful advice and explain how resolve this problem. Thanks.
This is my Hibernate Configuration for Mysql 8.0
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
#Configuration
#ComponentScan(basePackages= {"com.savaleks.websiteback.dto"})
#EnableTransactionManagement
public class HibernateConfig {
private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/website";
private final static String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";
private final static String DATABASE_DIALECT = "org.hibernate.dialect.MySQL8Dialect";
private final static String DATABASE_USERNAME = "root";
private final static String DATABASE_PASSWORD = "";
#Bean
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
// Database connection information
dataSource.setDriverClassName(DATABASE_DRIVER);
dataSource.setUrl(DATABASE_URL);
dataSource.setUsername(DATABASE_USERNAME);
dataSource.setPassword(DATABASE_PASSWORD);
return dataSource;
}
#Bean
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);
builder.addProperties(getHibernateProperties());
builder.scanPackages("com.savaleks.websiteback.dto");
return builder.buildSessionFactory();
}
// All Hibernate properties returned in this method
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", DATABASE_DIALECT);
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
return null;
}
#Bean
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
return transactionManager;
}
}
And this is console output
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryDAO': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSessionFactory' defined in com.savaleks.websiteback.config.HibernateConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'getSessionFactory' threw exception; nested exception is java.lang.NullPointerException
pom.xml file
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.20.RELEASE</spring.version>
<mysql.version>8.0.13</mysql.version>
<hibernate.version>5.2.17.Final</hibernate.version>
</properties>
<dependencies>
<!-- JUNIT TEST -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- SPRING CONTEXT -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- MYSQL DATABASE -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- HIBERNATE -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- DATABASE CONNECTION POOLING -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
If need more information, I can load more classes and console output. What could be wrong? any ideas?
Change to:
#Bean
public HibernateTransactionManager getTransactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(getSessionFactory());
return transactionManager;
}
or,
#Bean
public HibernateTransactionManager getTransactionManager(#Qualifier("getSessionFactory") SessionFactory sessionFactory) {
// your code here
}
or,
#Bean
public SessionFactory sessionFactory(DataSource dataSource) {
// your code here
}
There are few more ways.
Further Reading:
#Bean Methods in #Configuration Classes paragraph in Spring Framework javadoc of #Bean.
1.12.1. Basic Concepts: #Bean and #Configuration in Spring Framework Reference (version 5.1.3.RELEASE)
We configured springsessions using redis as session store. With bigger load it ended in a result where users happen to get back random session data.
Unfortunately we are not able to reproduce it again and it is not reproducable in test environment.
Has anyone had similar experience and can maybe point a direction what to look at?
I have used spring session using redis session store in my project as follows ,it works normal ,i share it , hope it helps!
used spring boot starter version : 1.4.3
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>biz.paluch.redis</groupId>
<artifactId>lettuce</artifactId>
<version>3.5.0.Final</version>
</dependency>
Spring session redis configuration class :
package az.com.cybernet.aas.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.lettuce.
LettuceConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.
EnableRedisHt
tpSession;
import org.springframework.session.web.http.CookieHttpSessionStrategy;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.session.web.http.HttpSessionStrategy;
#EnableRedisHttpSession
public class RedisConfig {
#Value("${spring.redis.host}")
private String hostname;
#Value("${spring.redis.port}")
private String port;
// #Value("${spring.redis.password}")
// private String password;
#Bean
public LettuceConnectionFactory connectionFactory() {
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
connectionFactory.setHostName(hostname);
// connectionFactory.setPassword(password);
connectionFactory.setDatabase(2);
connectionFactory.setPort(Integer.parseInt(port));
return connectionFactory;
}
#Bean
public CookieSerializer cookieSerializer() {
DefaultCookieSerializer serializer = new DefaultCookieSerializer();
serializer.setCookieName("JSESSIONID");
serializer.setCookiePath("/");
serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
return serializer;
}
#Bean
public HttpSessionStrategy strategy() {
CookieHttpSessionStrategy cookieHttpSessionStrategy = new CookieHttpSessionStrategy();
cookieHttpSessionStrategy.setCookieSerializer(cookieSerializer());
return cookieHttpSessionStrategy;
}
}
I am trying to set up my mybatis-spring like shown in the following examples:
1)Code from a previous answer on stackoverflow, a few answer down
(MyBatis-Spring + #Configuration - Can't autowire mapper beans)
#Configuration
#MapperScan("org.mybatis.spring.sample.mapper")
public class AppConfig
{
#Bean
public DataSource dataSource()
{
return new EmbeddedDatabaseBuilder().addScript("schema.sql").build();
}
#Bean
public DataSourceTransactionManager transactionManager()
{
return new DataSourceTransactionManager(dataSource());
}
#Bean
public SqlSessionFactory sqlSessionFactory() throws Exception
{
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
return sessionFactory.getObject();
}
}
2)Code from their documentation (http://www.mybatis.org/spring/mappers.html)
Usage:
public class FooServiceImpl implements FooService {
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public User doSomeBusinessStuff(String userId) {
return this.userMapper.getUser(userId);
}
}
Registering Mapper with #MapperScan:
#Configuration
#MapperScan("org.mybatis.spring.sample.mapper")
public class AppConfig {
#Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().addScript("schema.sql").build()
}
#Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
return sessionFactory.getObject();
}
}
My code which isn't working is as shown below:
My Application with nested AppConfig:
#SpringBootApplication
#MapperScan(basePackages="com.tjwhalen.game.service.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
//#MapperScan(basePackages="com.tjwhalen.game.service.dao")
public class AppConfig {
#Autowired
DataSource datasource;
#Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(datasource);
}
#Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(datasource);
//sqlSessionFactory.setTypeAliasesPackage("com.tjwhalen.game.model");
return (SqlSessionFactory) sqlSessionFactory.getObject();
}
#Bean
public ItemSummaryDbService itemSummaryDbService() {
return new ItemSummaryDbServiceImpl();
}
}
}
My service:
public class ItemSummaryDbServiceImpl implements ItemSummaryDbService {
#Autowired
private ItemSummaryMapper itemSummaryMapper;
public void setItemSummaryMapper(ItemSummaryMapper itemSummaryMapper) {
this.itemSummaryMapper = itemSummaryMapper;
}
public void writeItemSummarys(List<ItemSummary> itemSummarys) {
for(ItemSummary itemSummary : itemSummarys) {
itemSummaryMapper.insertItemSummary(itemSummary);
}
}
public List<ItemSummary> lookupItemSummarys() {
return itemSummaryMapper.selectItemSummarys();
}
}
My mapper in the package indicated by the #MapperScan annotaion:
package com.tjwhalen.game.service.dao;
import java.util.List;
import com.tjwhalen.game.model.ItemSummary;
public interface ItemSummaryMapper {
public void insertItemSummary(ItemSummary itemSummary);
public List<ItemSummary> selectItemSummarys();
}
My usage:
public class LoadItems implements CommandLineRunner {
private final static Logger logger = LoggerFactory.getLogger(LoadItems.class);
#Autowired
private ItemSummaryDbService service;
public void run(String... arg0) throws Exception {
logger.info("LoadItems is running");
ArrayList<ItemSummary> list = new ArrayList<ItemSummary>();
list.add(new ItemSummary(1, "one", 1));
service.writeItemSummarys(list);
}
}
My stacktrace:
java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransactionFactory.newTransaction(Ljava/sql/Connection;Z)Lorg/apache/ibatis/transaction/Transaction;
at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:77) ~[ibatis-core-3.0.jar:na]
at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:40) ~[ibatis-core-3.0.jar:na]
at org.mybatis.spring.SqlSessionUtils.getSqlSession(SqlSessionUtils.java:102) ~[mybatis-spring-1.3.0.jar:1.3.0]
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:429) ~[mybatis-spring-1.3.0.jar:1.3.0]
at com.sun.proxy.$Proxy41.insert(Unknown Source) ~[na:na]
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:279) ~[mybatis-spring-1.3.0.jar:1.3.0]
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:54) ~[ibatis-core-3.0.jar:na]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:35) ~[ibatis-core-3.0.jar:na]
at com.sun.proxy.$Proxy42.insertItemSummary(Unknown Source) ~[na:na]
at com.tjwhalen.game.service.impl.ItemSummaryDbServiceImpl.writeItemSummarys(ItemSummaryDbServiceImpl.java:32) ~[classes/:na]
at com.tjwhalen.game.loader.LoadItems.run(LoadItems.java:28) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:782) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:769) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at com.tjwhalen.game.Application.main(Application.java:37) [classes/:na]
And finally my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tjwhalen.game</groupId>
<artifactId>Runescape-App</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.4-702.jdbc4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core -->
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-core</artifactId>
<version>3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.2.RELEASE</version>
</dependency> -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.batch/spring-batch-infrastructure -->
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-infrastructure</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I've had a lot of problems in the past regarding incorrect pom.xml, so I looked at each dependency involving database access and made sure the project had the correct provided dependencies. I also checked that the versions were the same as mentioned in the provided dependencies.
What does the error message AbstractMethodError indicate?
Feel free to ask any clarifying questions
I figured it out. I added a dependency
<!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core -->
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-core</artifactId>
<version>3.0</version>
</dependency>
and removing it fixed. I don't know why this dependency was here, I went through and checked the rest of my dependencies to see if they depended on ibatis-core and they didn't.
So it was an oversight on my part, and shows that AbstractMethodError is likely a dependency issue, and that is the first thing that should be checked when facing it