Hibernate 弹簧靴&x2B;JPA注入EntityManager没有可用的EntityManager
我试图使用spring boot将EntityManager注入到我的DAO中,但得到一个Hibernate 弹簧靴&x2B;JPA注入EntityManager没有可用的EntityManager,hibernate,jpa,spring-boot,entitymanager,Hibernate,Jpa,Spring Boot,Entitymanager,我试图使用spring boot将EntityManager注入到我的DAO中,但得到一个InvalidDataAccessApiUsageException表示没有可用的事务EntityManager。我的印象是,只要Spring Boot从application.yml中获得我的数据源信息,并且我用@persistencecontext标记DAO中的EntityManager字段,它基本上会为我处理所有的事情,因此,我不需要在自己的代码中使用EntityManagerFactory,也不需要
InvalidDataAccessApiUsageException
表示没有可用的事务EntityManager。我的印象是,只要Spring Boot从application.yml
中获得我的数据源信息,并且我用@persistencecontext
标记DAO中的EntityManager
字段,它基本上会为我处理所有的事情,因此,我不需要在自己的代码中使用EntityManagerFactory,也不需要persistence.xml
文件。我错了吗
我的刀:
@Repository
public class PersonDaoJpa implements PersonDao {
@PersistenceContext
private EntityManager em;
...
DAO methods
...
}
我的Spring启动配置:
@SpringBootApplication
@EnableTransactionManagement
@ComponentScan(basePackages = {ProgramInfo.BASE_PACKAGE})
@EntityScan(basePackages = {ProgramInfo.BASE_PACKAGE})
public class PersonServiceConfiguration {
public static void main(String[] args) {
SpringApplication.run(PersonServiceConfiguration.class, args);
}
}
我的JUnit测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PersonServiceConfiguration.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class })
@EnableTransactionManagement
@DatabaseSetup("/test_data_start.xml")
public class PersonDaoJpaTest {
@Autowired
private PersonDaoJpa dao;
...
JUnit tests
...
}
我的application.yml
文件:
spring:
profiles.active: default
---
spring:
profiles: default
spring.datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/database?autoReconnect=true
username: user
password: pwd
spring.jpa.show-sql: false
spring.jpa.database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
</parent>
<groupId>myId</groupId>
<artifactId>myArtifictId</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
最后是我的pom.xml
文件:
spring:
profiles.active: default
---
spring:
profiles: default
spring.datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/database?autoReconnect=true
username: user
password: pwd
spring.jpa.show-sql: false
spring.jpa.database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
</parent>
<groupId>myId</groupId>
<artifactId>myArtifictId</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
但是如果没有注释,我会得到与上面描述的相同的异常。find
和flush
方法如下:
@Override
public Person find(Long id) {
return em.find(Person.class, id);
}
public void flush() {
em.flush();
}
您在实现中变得越具体,Spring Boot在为您所做的事情中就会退却得越多 因为您已经创建了自己的实现,所以现在还需要配置自己的实体管理器,而不是定义一个实现crudepository的接口(例如) 以下是一些文档链接,可以向您展示如何做您需要的事情:
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html
http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.create-instances.java-config
尝试将@Transactional放在您的存储库中?如果您能够将完整的项目作为示例上载到github,我不介意仔细阅读代码,看看是否可以使用尽可能少的自定义配置使其工作。