6

I am trying to use Spring Data JPA with MyBatis. Since there isnt a Vendor Adapter for MyBatis, what is the alternative here?

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.abc.xyz.domain"/>
</bean>

I am getting the below exception when I tried to initialize my application.

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No PersistenceProvider specified in EntityManagerFactory configuration, and chosen PersistenceUnitInfo does not specify a provider class name either

Thanks

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
srini
  • 544
  • 1
  • 11
  • 23
  • 9
    MyBatis is not a JPA implementation. Don't see how you could use Spring-Data-JPA with it. – JB Nizet Nov 11 '15 at 14:46
  • @JBNizet Interesting that you mentioned it. I happened to assume that MyBatis is a ORM implementing JPA like Hibernate does. Guess this question is invalid in that case. Thank you. – srini Nov 11 '15 at 15:09
  • You can show the complete spring xml file and complete stack trace. It will be useful to identify – Karthikeyan May 25 '17 at 06:02
  • 1
    You can use both of them together (which I have been doing) but according to your info above, it is not enough to determine what you are trying to achieve. – Ian Lim Jan 06 '18 at 15:09

6 Answers6

11

Mybatis does not implement JPA. Mybatis is not ORM Framework. JPA is ORM Specification which is implemented by Hibernate, Toplink, Eclipselink . Since Mybatis does not mplement JPA, it does not come under the list of JPA providers. Hence, you cannot use mybatis as a JPA framework. Mybatis is a data mapper framework which is completely different framework compared to JPA. In JPA and ORM frameworks, you map Objects /Entities to the corresponding sql tables and you work on objects and not on tables directly unless you use their native queries. In mybatis , you play directly with sql data.. Hope this clears the difference between mybatis and JPA. Hence when you want mybatis with spring data you use spring data mybatis independently and not spring data JPA.

Pankaj Shet
  • 179
  • 2
  • 8
4

Spring Data MyBatis

If you would not like to use a JPA implementation like Spring-Data-JPA module, but you like use Spring-Data you can find Spring-Data-Mybatis a useful project.

I know that this is not precise answer to your question but I hope that this answer can be interesting.

Community
  • 1
  • 1
  • 1
    This project is not up to date nor have a working demo. The author doesn't provide any information. – Dimitri Kopriwa Nov 09 '16 at 13:25
  • Last commits is on Oct 6, 2016 so they are working on the project. On Issues tab I see some topic closed 6 minutes ago. If the project is not working now will be a update to Spring-Data version trouble. Try to open an issue. –  Nov 09 '16 at 14:51
  • I am the one who open the issues. The plugin is not up to date and can't work with latest spring version, I am on it since more than a week and the author is not really answering to my questions. If you are a user of the plugin maybe you couldn't answer to the current issue :) – Dimitri Kopriwa Nov 09 '16 at 16:02
2

Why not try spring-data-jpa-extra

It provide a dynamic query solution for spring-data-jpa like mybatis, but much easier than mybatis.

I think you would like it : )

stormning
  • 41
  • 3
1

Spring-Data-Mybatis Hatunet version

I am using this project: https://github.com/hatunet/spring-data-mybatis

It fit very well with spring-data-mybatis and it have also the paginated repository.

Work very well on production project.

update 08/2020

The projet as moved to another webspace and evolved: https://github.com/easybest/spring-data-mybatis

Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
1

Here is the configuration of mybatis and jpa in spring framework. Mybatis and jpa are different framework so you cannot use mybatis as a JPA framework. Feel free to ask any question if you cannot catch up the configuration.

package com.mastering.springbatch.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = {"com.mastering.springbatch.dao",
                "com.mastering.springbatch.repository"})
@EntityScan("com.mastering.springbatch.entity")
public class DataConfig {
    private final String ENTITY_PACKAGE = "com.mastering.springbatch.entity";
    private DriverManagerDataSource dataSource;

    @Primary
    @Bean(value = "customDataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        this.dataSource = dataSource;
        return dataSource;
    }

    @Primary
    @Bean(value = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean emf =
                new LocalContainerEntityManagerFactoryBean();
        emf.setPackagesToScan(ENTITY_PACKAGE);
        emf.setDataSource(dataSource());
        emf.setJpaVendorAdapter(jpaVendorAdapter());
        return emf;
    }

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


    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager tm = new JpaTransactionManager();
        tm.setEntityManagerFactory(entityManagerFactory().getObject());
        return tm;
    }

    private JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setShowSql(true);
        jpaVendorAdapter.setGenerateDdl(true);
        jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
        return jpaVendorAdapter;
    }
}

here is the build.gradle file

buildscript {
    ext {
        springBootVersion = '2.1.8.RELEASE'
        springBootDepManagementVersion = '1.0.8.RELEASE'
    }
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "io.spring.gradle:dependency-management-plugin:${springBootDepManagementVersion}"
    }
}

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
apply plugin: 'idea'


group 'com.learning'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

configurations {
    implementation.exclude module: "spring-boot-starter-tomcat"
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile 'org.mybatis:mybatis:3.5.0'
    compile 'org.mybatis:mybatis-spring:2.0.0'

    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-batch")
    implementation("org.springframework.boot:spring-boot-starter-web")
    
    implementation("mysql:mysql-connector-java:8.0.14")
    
//    implementation("io.springfox:springfox-swagger2:2.7.0")
//    implementation("io.springfox:springfox-swagger-ui:2.7.0")
    
    implementation("org.projectlombok:lombok:1.18.10")
    annotationProcessor("org.projectlombok:lombok:1.18.10")
    compile group: 'commons-io', name: 'commons-io', version: '2.6'

    testAnnotationProcessor("org.projectlombok:lombok:1.18.10")
    testCompile("junit:junit:4.12")
    testCompile("org.mockito:mockito-core:2.1.0")
    testCompile("org.springframework.boot:spring-boot-starter-test")

}

springBoot {
    buildInfo()
}

Atif
  • 210
  • 2
  • 11
0

A famous person also defines mybatis is orm.
(https://martinfowler.com/bliki/OrmHate.html => The widely available open source ORMs (such as iBatis, Hibernate, and Active Record))
jpa is worse in speed, debugging, N+1 Problem,etc than mybatis.
If you list only the bad things, there are more than 10 things.
In many ways, it's the worst choice than mybatis.
Jpa in Java is the worst.
There are no advantages of jpa.
Here on YouTube it also lists the shortcomings of jpa. https://youtu.be/EpYBP7EZ8Y4?t=370 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>
To @Maksim Eliseev
https://en.wikipedia.org/wiki/Persistence_framework Jpa(Java Persistence API) also Persistence framework

Zombi e
  • 1
  • 1
  • I don't understand why the moderator is deleting the post. It's just an informative article. Please leave your reasons in the comments. – Zombi e Aug 12 '23 at 16:00
  • Should I say that jpa is always good? If you think jpa is good, please let me know why. – Zombi e Aug 12 '23 at 16:02