gpt4 book ai didi

java - 考虑定义 EntityManagerFactory 类型的 bean Spring boot

转载 作者:行者123 更新时间:2023-11-29 15:44:26 26 4
gpt4 key购买 nike

我正在使用 Spring boot 并且运行时出现此错误:

A component required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found.

Action:

Consider defining a bean of type javax.persistence.EntityManagerFactory in your configuration.

我不知道我应该做什么,因为我做的一切都是基于类(class)

我使用@PersistanceUnit来注入(inject)EntityManagerFactory

<?xml version="1.0" encoding="UTF-8"?>
<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>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pl.javastart</groupId>
<artifactId>spring-jpa-boot2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-jpa-boot2</name>
<description>Spring boot app</description>

<properties>
<java.version>11</java.version>
</properties>

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

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
package pl.javastart.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Repository;
import pl.javastart.model.Book;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceUnit;

@Repository
public class BookDaoImpl implements BookDao {


@PersistenceUnit
private EntityManagerFactory emFactory;


public BookDaoImpl(){

}

@Override
public void save(Book book) {
EntityManager entityManager = emFactory.createEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
entityManager.persist(book);
tx.commit();
entityManager.close();
}

@Override
public Book get(Long id) {
EntityManager entityManager = emFactory.createEntityManager();
Book book = entityManager.find(Book.class, id);
entityManager.close();
return book;
}
}
package pl.javastart;

import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import pl.javastart.dao.BookDao;
import pl.javastart.model.Book;

@Configuration
@ComponentScan
public class SpringJpaApplication {
public static void main(String[] args) throws InterruptedException {


ConfigurableApplicationContext ctx = SpringApplication.run(SpringJpaApplication.class, args);
BookDao dao = ctx.getBean(BookDao.class);
Book book = new Book("1234567890468", "Spring is so cool", "Javastart");
dao.save(book);
Thread.sleep(5000);
}
}

如何解决这个问题?

最佳答案

我向maven添加了两个依赖项:

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.4.0-b180830.0359</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.8.Final</version>
</dependency>

一切正常

关于java - 考虑定义 EntityManagerFactory 类型的 bean Spring boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57227004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com