I am learning Spring Data and creating a project. Project like a list of exercises, save data in MySql thanks to Hibernate, but something wrong with it, and i don't know what
My controller:
@Controller
public class UserController {
private final IUserService userService;
@Autowired
public UserController(IUserService userService) {
this.userService = userService;
}
@PostMapping(path = "/user/create")
public ResponseEntity<UserPojo> createUser(@RequestBody User user) {
UserPojo result = userService.createUser(user);
return new ResponseEntity<>(result, HttpStatus.OK);
}
@GetMapping(path = "/user/{id}")
public ResponseEntity<UserPojo> getUser(@PathVariable Long id) {
UserPojo result = userService.getUser(id);
return new ResponseEntity<>(result, HttpStatus.OK);
}
}
Service:
@Service
public class UserService implements IUserService {
@PersistenceContext
EntityManager entityManager;
private final Converter converter;
@Autowired
public UserService(Converter converter) {
this.converter = converter;
}
@Override
@Transactional
public UserPojo createUser(User user) {
entityManager.persist(user);
return converter.userToPojo(user);
}
But when i ran it in Postman, i got this - I don't know why it appears:
More info:
java.lang.ClassNotFoundException: jakarta.persistence.EntityManagerFactory
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1449)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1257)
java.base/java.lang.Class.forName0(Native Method)
java.base/java.lang.Class.forName(Class.java:488)
java.base/java.lang.Class.forName(Class.java:467)
java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
My POM and more code is here:
GitHub
You have a weird and inconsistent mix of dependencies here
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.9.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.0.0</version>
</dependency>
When you use Spring Data JPA version 3.0.0 you should use Hibernate 6+. To get started you shouldn't specify the versions for Hibernate since those are specified in the Spring Data JPA pom.xml
.
And you shouldn't need hibernate-jpa-2.0-api
at all.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.