Spring-boot + JPA EntityManager注入失败 [英] Spring-boot+JPA EntityManager inject fails

查看:124
本文介绍了Spring-boot + JPA EntityManager注入失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的J2EE应用程序中,我尝试通过将EntityManager注入DAO层来使用spring-boot和JPA技术.但是,我遇到了一些问题...我的CRUD用户存储库:

In my J2EE application I try to use spring-boot and JPA technologies with injection of EntityManager into DAO layer. However, I have some problems... My repository for user CRUD:

@Repository
public class UserRepositoryImpl implements UserRepository {

@PersistenceContext(unitName = "data")
private EntityManager entityManager;
// and crud methods
}

我的spring-boot应用程序类:

My spring-boot application class:

@SpringBootApplication
public class App {
    public static void main(String [] args) {
        SpringApplication.run(App.class, args);
    }

}

最后是我的persistence.xml,位于src/main/resources/META-INF文件夹中:

And finally my persistence.xml, located in src/main/resources/META-INF folder:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence          http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="data" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.example.domain.User</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.connection.autocommit" value="false" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
        <property name="hibernate.c3p0.min_size" value="4" />
        <property name="hibernate.c3p0.max_size" value="128" />
        <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:1433;databaseName=qwerty;sendStringParametersAsUnicode=false" />
        <property name="javax.persistence.jdbc.user" value="sa" />
        <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="javax.persistence.jdbc.password" value="" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="false" />
    </properties>
</persistence-unit>

因此,当我尝试使用此注入的entityManager时,会收到NullPointerException.注入其他@Autowired字段没有任何问题.此代码有什么问题?我需要一些额外的配置吗? 我是一个初学者(甚至不是初级开发人员),而且对于Spring-boot是什么以及如何配置它(例如xml文件中的Spring)确实有一些误解.如果由于注入EM而需要这种xml配置,请说明如何执行此操作.

So, when I try using this injected entityManager I get NullPointerException. Other @Autowired fields are injected without any problems. What's wrong with this code? Do I need some extra configuration? I am a beginner (not even a Junior developer) and I do have some misunderstanding of what Spring-boot is and how to configure it, like Spring in xml file. If such an xml configuration is needed due to inject EM, please, show how to do it.

upd2.依赖关系

<dependencies>
    <!-- logger -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>

    <!-- db -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
    </dependency>
    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
    </dependency>
    <dependency>
        <groupId>com.microsoft</groupId>
        <artifactId>sqljdbc4</artifactId>
    </dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.1</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.5.3</version>
    </dependency>

    <!-- csv -->
    <dependency>
        <groupId>com.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>3.3</version>
    </dependency>

    <!-- spring-boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>1.2.4.RELEASE</version>

        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>

    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
        <version>1.2.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.2.4.RELEASE</version>

        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>

    </dependency>

推荐答案

您应该对spring-boot-starter-data-jpa使用依赖项

You should use the dependency for spring-boot-starter-data-jpa

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

要使用持久性xml,您应该按照文档中的说明定义一个bean.

And to use a persistence xml you should define a bean as it says in the documentation.

Spring不需要使用XML来配置JPA提供程序,并且Spring Boot假定您想利用该功能.如果您更喜欢使用persistence.xml,则需要定义自己的@Bean,其类型为LocalEntityManagerFactoryBean(标识为"entityManagerFactory",并在其中设置持久性单元名称.

Spring doesn’t require the use of XML to configure the JPA provider, and Spring Boot assumes you want to take advantage of that feature. If you prefer to use persistence.xml then you need to define your own @Bean of type LocalEntityManagerFactoryBean (with id ‘entityManagerFactory’, and set the persistence unit name there.

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-use-traditional-persistence-xml

或者您可以完全跳过persistence.xml并在application.properties文件中定义连接属性.

Or you could skip the persistence.xml entirely and define connection properties in the application.properties file.

引用文档

DataSource配置由spring.datasource.*中的外部配置属性控制.例如,您可以在application.properties中声明以下部分:

DataSource configuration is controlled by external configuration properties in spring.datasource.*. For example, you might declare the following section in application.properties:

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-connect-to-production-database

(更改驱动程序和其他数据以匹配您的环境)

(change the driver and other data to match your environment)

祝你好运!

这篇关于Spring-boot + JPA EntityManager注入失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆