Spring-boot+JPA EntityManager 注入失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30753579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring-boot+JPA EntityManager inject fails
提问by goldaniga
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:
在我的 J2EE 应用程序中,我尝试使用 spring-boot 和 JPA 技术将 EntityManager 注入 DAO 层。但是,我有一些问题...我的用户 CRUD 存储库:
@Repository
public class UserRepositoryImpl implements UserRepository {
@PersistenceContext(unitName = "data")
private EntityManager entityManager;
// and crud methods
}
My spring-boot application class:
我的 spring-boot 应用程序类:
@SpringBootApplication
public class App {
public static void main(String [] args) {
SpringApplication.run(App.class, args);
}
}
}
And finally my persistence.xml, located in src/main/resources/META-INF folder:
最后是我的 persistence.xml,位于 src/main/resources/META-INF 文件夹中:
<?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>
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.
所以,当我尝试使用这个注入的 entityManager 时,我得到 NullPointerException。其他@Autowired 字段注入没有任何问题。这段代码有什么问题?我需要一些额外的配置吗?我是初学者(甚至不是初级开发人员),我确实对 Spring-boot 是什么以及如何配置它有一些误解,例如 xml 文件中的 Spring。如果由于注入 EM 而需要这样的 xml 配置,请展示如何操作。
upd2. Dependencies
更新2。依赖关系
<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.Hymanson.core</groupId>
<artifactId>Hymanson-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>
回答by stalet
You should use the dependency for spring-boot-starter-data-jpa
您应该使用 spring-boot-starter-data-jpa 的依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
And to use a persistence xml you should define a bean as it says in the documentation.
要使用持久性 xml,您应该按照文档中的说明定义一个 bean。
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.
Spring 不需要使用 XML 来配置 JPA 提供程序,并且 Spring Boot 假定您希望利用该功能。如果您更喜欢使用persistence.xml,那么您需要定义您自己的LocalEntityManagerFactoryBean 类型的@Bean(id 为'entityManagerFactory',并在那里设置持久性单元名称。
Or you could skip the persistence.xml entirely and define connection properties in the application.properties file.
或者您可以完全跳过persistence.xml 并在application.properties 文件中定义连接属性。
Quote from the documentation
从文档中引用
DataSource configuration is controlled by external configuration properties in spring.datasource.*. For example, you might declare the following section in application.properties:
数据源配置由 spring.datasource.* 中的外部配置属性控制。例如,您可以在 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
(change the driver and other data to match your environment)
(更改驱动程序和其他数据以匹配您的环境)
Good luck!
祝你好运!
回答by BERGUIGA Mohamed Amine
you can use java configuration in order to configure jpa persistence. The code bellow show you sample example of configuration:
您可以使用 java 配置来配置 jpa 持久性。下面的代码向您展示了配置示例:
@Component
public class JpaConfiguration {
@Bean
@Primary
public DataSource dataSource() {
final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriver(new org.postgresql.Driver());
dataSource.setUrl("jdbc:postgresql://localhost:5432/users");
dataSource.setUsername("postgres");
dataSource.setPassword("admin");
return dataSource;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(true);
jpaVendorAdapter.setShowSql(true);
jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect");
return jpaVendorAdapter;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setPackagesToScan("tn.bergit.crud.entity");
lef.setDataSource(dataSource());
lef.setJpaVendorAdapter(jpaVendorAdapter());
Properties properties = new Properties();
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.jdbc.fetch_size", "100");
properties.setProperty("hibernate.hbm2ddl.auto", "update");
lef.setJpaProperties(properties);
return lef;
}
}
You can see this example on github(click here)
你可以在 github 上看到这个例子(点击这里)
回答by Eagle_Eye
The class from which you're asking for the EntityManager should be a Spring Bean if you're using the following
如果您使用以下内容,则您要求 EntityManager 的类应该是 Spring Bean
public class Foo {
@PersistenceContext
private EntityManager entityManager;
}
Here, if you're using newkeyword to get an instance of Foo class above, then EntityManagerwill be null.
在这里,如果您使用new关键字来获取上述 Foo 类的实例,则EntityManager将为null。
Hope this helps someone!
希望这可以帮助某人!
回答by vedat
if you want to keep using persistence.xml file just add the below code in your configuration class
如果您想继续使用persistence.xml 文件,只需在您的配置类中添加以下代码
@Bean
public LocalEntityManagerFactoryBean entityManagerFactory(){
LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
factoryBean.setPersistenceUnitName("data");
return factoryBean;
}