spirng4 spring data jpa configuration record

This article is from http://fair-jm.iteye.com/ Please indicate the source

 

A few days ago, I watched the spring-data-jpa part of spring combat (fourth edition) and found some problems.

Here record the configuration after troubleshooting

 

The first is the pom. The worst thing about this book is that it doesn't tell you which dependencies you want.

When I was practicing, I found that the flashMode method in hibernate 5.2.1 and spring 4.2 could not be found. I checked and found that hibernate suddenly removed this method in a version. As a result, spring 4.2 and above kneeled. I used It is spring 4.3.2, but the latest spring-data-jpa that depends on it still indirectly depends on spring-jdbc and spring-orm of 4.2.6. After directly relying on the above two jars of 4.3.2 in the pom, the problem is eliminated.

pom (also included in this book from the web):

 

    <properties>

        <!-- Generic properties -->
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Web -->
        <servlet.version>3.1.0</servlet.version>

        <!-- Spring -->
        <spring-framework.version>4.3.2.RELEASE</spring-framework.version>
        <spirng-data-jpa.version>1.10.2.RELEASE</spirng-data-jpa.version>

        <!-- Logging -->
        <logback.version>1.1.7</logback.version>
        <slf4j.version>1.7.21</slf4j.version>

        <!-- Test -->
        <junit.version>4.11</junit.version>

        <!-- Other -->
        <common-lang.version>3.4</common-lang.version>
        <mysql.version>5.1.39</mysql.version>
        <hibernate-jpa.version>1.0.1.Final</hibernate-jpa.version>
        <hibernate.version>5.2.1.Final</hibernate.version>
        <druid.version>1.0.24</druid.version>
        <jackson.version>2.8.1</jackson.version>
        <aspectj.version>1.8.9</aspectj.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${spirng-data-jpa.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
        <!-- Other Web dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring and Transactions -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- Logging with SLF4J & LogBack -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        
        <!-- db related -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>${hibernate-jpa.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <!-- third part tools -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${common-lang.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>

        <!-- Test Artifacts -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-framework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

 

 

The configuration of db is also slightly different from the book:

 

@Configuration
//package where the repository is located
@EnableJpaRepositories(basePackages = "com.xxx.db")
@EnableTransactionManagement
@Profile("dev")
public class DbDevConfig {

    @Bean(initMethod = "init", destroyMethod = "close")
    public DataSource dataSource() {
        final DruidDataSource source = new DruidDataSource();
        source.setUrl("jdbc:mysql://localhost:3306/xxx");
        source.setUsername("");
        source.setPassword("");
        source.setInitialSize(1);
        source.setMinIdle(1);
        source.setMaxActive(20);
        source.setDriverClassName("com.mysql.jdbc.Driver");
        return source;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        final HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(Database.MYSQL);
        adapter.setShowSql(true);
        adapter.setGenerateDdl(false);
        adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
        return adapter;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        final LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
        emfb.setDataSource(dataSource());
        emfb.setJpaVendorAdapter(jpaVendorAdapter());
        //The package where the entity is located
        emfb.setPackagesToScan("com.xxx.domain");
        emfb.afterPropertiesSet();
        return emfb;
    }

    @Bean
    public JpaTransactionManager transactionManager() {
        return new JpaTransactionManager(entityManagerFactory().getObject());
    }
}

 The packageToScan of emfb is not configured in the book, which will cause an error that persistence.xml cannot be found in the runtime, and according to the book, the configuration of the containerEntityManager does not require this xml.

 

 

When using the repository, if you use getOne, it will throw no session exception, but use findOne will not. It is estimated that it is a lazy loading problem.

 

It is estimated that using spring-boot will save a lot of effort, but it will be more troublesome to troubleshoot.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326705157&siteId=291194637