我正在try 编译我的Spring Boot项目,但由于错误required a bean of type 'jakarta.persistence.EntityManager' that could not be found.the error is in TypeHunterDAOJpaImplementation.java,我在运行应用程序时遇到了问题.
application.properties
# PostgreSQL
spring.datasource.url=jdbc:postgresql://localhost:5432/my_database
spring.datasource.username=postgres
spring.datasource.password=12345678
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
-
TypeHunter.java
个
import jakarta.persistence.*;
@Entity
@Table(name="type_hunters")
public class TypeHunter {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="description")
private String description;
public TypeHunter() { // No-arg constructor for JPA
}
public TypeHunter(int id, String description) {
this.id = id;
this.description = description;
}
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
}
-
TypeHunterDAO.java
个
import java.util.List;
public interface TypeHunterDAO {
List<TypeHunter> findAll();
}
-
TypeHunterDAOJpaImplementation.java
个
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.TypedQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class TypeHunterDAOJpaImplementation implements TypeHunterDAO {
@PersistenceContext
private EntityManager entityManager;
@Autowired
public TypeHunterDAOJpaImplementation(EntityManager theEntityManager){
entityManager = theEntityManager;
}
@Override
public List<TypeHunter> findAll() {
if (entityManager == null) throw new IllegalStateException("EntityManager does not work correctly.");
TypedQuery<TypeHunter> query = entityManager.createQuery("from TypeHunter", TypeHunter.class);
return query.getResultList();
}
}
-
TypeHunterREST.java
个
import com.user.springboot3.project.DAO.TypeHunterDAO;
import com.user.springboot3.project.Entity.TypeHunterHunter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RequestMapping("/api")
@RestController
public class TypeHunterRest {
private TypeHunterDAO type_hunter;
public TypeHhnterRest(TypeHunterDAO typeHunterDAO) {
this.type_hunter = typeHunterDAO;
}
@GetMapping("/type-hunter")
public List<TypeHunter> findAll() {
return type_hunter.findAll();
}
}
建议的操作为Consider defining a bean of type 'jakarta.persistence.EntityManager' in your configuration.
在我的pom.xml
人中有:
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.6.6.Final</version>
</dependency>
我想,这肯定起作用了.