Search code examples
javaspring-bootmybatisspring-mybatismybatis-generator

AbstractMethodError and null maper when calling the DAO in a service


I'm building a standalone app with Spring boot, Mybatis (spring and generator), I think I configured everything alright but the autowired mapper is null, and I get an AbstractMethodError when calling it.

Actually I think it's because the null mapper. I've been searching about it and I've followed every possible solution, but nothing worked.

About the code: I'm using JavaFX too, but that doesn't have any effect on my problem.

my.project.core.Main.java

@SpringBootApplication
@MapperScan({"my.project.persistence"})
public class Main extends Application{

    private ConfigurableApplicationContext springContext;

    public static void main(String[] args){
        launch(args);
    }

    public void start(Stage stage) throws Exception {

        springContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);

        //Some code to display the views

        System.out.println("test 1: " + springContext.getBean(UsuarioMapper.class));

        stage.show();
    }
}

(DAO generated with MyBatis-Generator) my.project.persistence.UsuarioMapper.java

@Mapper //I've also tried @Component
public interface UsuarioMapper {

    int countByExample(UsuarioExample example);

    int deleteByExample(UsuarioExample example);

    int deleteByPrimaryKey(String codUsuario);

    int insert(UsuarioBean record);

    //Etc etc etc
}

(Service) my.project.services.ServicioUsuario.java

@Service
public class ServicioUsuario {

    @Autowired private UsuarioMapper usuarioMapper;

    public boolean userExists(String user){

        System.out.println("test 2: " + usuarioMapper);
        UsuarioBean bean = usuarioMapper.selectByPrimaryKey(user);
        return bean!=null;
    }

}

(Config) my.project.config.SpringConfiguration.java

@Configuration
public class SpringConfiguration {

    /*========== SERVICES ==========*/

    @Bean 
    public ServicioUsuario servicioUsuario() throws Exception{
        return new ServicioUsuario();
    }

    /*========== DAO ==========*/

    //I've already tried passing userMapper().getObject() in ServicioUsuario
    //constructor, but the output is the same, null mapper
    @Bean
    public MapperFactoryBean<UsuarioMapper> userMapper() throws Exception {
        MapperFactoryBean<UsuarioMapper> factoryBean = new MapperFactoryBean<UsuarioMapper>(UsuarioMapper.class);
        factoryBean.setSqlSessionFactory(sqlSessionFactory());
        return factoryBean;

    /*========== CONNECTION ==========*/

    @Bean
    public DataSource dataSource() throws ClassNotFoundException, SQLException{
        OracleDataSource dataSource = new OracleDataSource();

        dataSource.setDriverType("thin");
        dataSource.setServerName("localhost");
        dataSource.setDatabaseName("xe");
        dataSource.setPortNumber(1521);
        dataSource.setUser("testuser");
        dataSource.setPassword("pass123");

        Class.forName("oracle.jdbc.driver.OracleDriver");

        return dataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
        return sqlSessionFactory;
    }

    @Bean
    public DataSourceTransactionManager transactionManager() throws ClassNotFoundException, SQLException {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public SqlSessionTemplate sqlSession() throws Exception {
          return new SqlSessionTemplate(sqlSessionFactory());
        }

}

Output (summary):

18:30:41.023 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'springConfiguration'
18:30:41.095 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'servicioUsuario'
18:30:41.096 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userMapper'
18:30:41.097 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'sqlSessionFactory'
18:30:41.107 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'dataSource'
18:30:41.231 [JavaFX Application Thread] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration
18:30:41.273 [JavaFX Application Thread] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Property 'mapperLocations' was not specified or no matching resources found
18:30:41.481 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'transactionManager'
18:30:41.490 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'sqlSession'
test 1: null
test 2: null
18:32:26.250 [JavaFX Application Thread] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
18:32:26.501 [JavaFX Application Thread] DEBUG java.sql.Connection - ooo Connection Opened
//And now the full stacktrace
Exception in thread "JavaFX Application Thread" 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)
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:40)
    at org.mybatis.spring.SqlSessionUtils.getSqlSession(SqlSessionUtils.java:100)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:428)
    at com.sun.proxy.$Proxy14.selectOne(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:66)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:35)
    at com.sun.proxy.$Proxy15.selectByPrimaryKey(Unknown Source)
    at my.project.services.ServicioUsuario.userExists(ServicioUsuario.java:42)
    at my.project.gui.LoginController.btAceptar(LoginController.java:70)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)

Should I implement the mapper interface that MyBatis Generator generated? Do I have any bean configured wrong? I've tried everything and that mapper just want to keep being null. Thanks.


Solution

  • The stacktrace clearly shows that the mapper is injected and it is not null:

        at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:35)
        at com.sun.proxy.$Proxy15.selectByPrimaryKey(Unknown Source)
        at my.project.services.ServicioUsuario.userExists(ServicioUsuario.java:42)
    

    These lines show that invocation of the mapper does happen and it is not null.

    The error happens because of the versions incompatibility between mybatis and spring-mybatis and spring.

    You need to check what versions of these libraries are really used (the actual way how to do that differs for maven and gradle) and make sure that they are all compatible. You can use this site to check this.

    For example from this page https://mvnrepository.com/artifact/org.mybatis/mybatis-spring/2.0.3 I can see that mybatis-spring version 2.0.3 uses spring version 5.1.10.RELEASE and mybatis of version 3.5.3. Then you compare this with actual dependencies version that you build tool (mvn or gradle) resolved.

    For gradle this is done with dependencies task.

    For maven use dependencies plugin.