我正在尝试使用Hibernate创建小型String启动应用程序。尝试保存对象(用户)
时出现以下错误java.lang.IllegalArgumentException: Unknown entity: com.prithvi.thoughtprocess.beans.dao.impl.User
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1149) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_51]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_51]
我在谷歌上搜索了这个,但没有找到解决方案。 以下是我的用户实体 -
package com.prithvi.thoughtprocess.beans.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="user")
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3367865397662004005L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
UserDAOImpl类如下 -
package com.prithvi.thoughtprocess.beans.dao.impl;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import com.prithvi.thoughtprocess.beans.dao.UserDAO;
import com.prithvi.thoughtprocess.beans.domain.User;
@Repository
public class UserDAOImpl implements UserDAO{
@PersistenceContext
private EntityManager entityManager;
@Override
public <S extends User> S save(S entity) {
entityManager.persist(entity);
return null;
}}
application.properties文件
spring.datasource.url=jdbc:mysql://localhost:3306/bookbank
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
以下是包含主要方法的类 -
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.prithvi.thoughtprocess.dto.BookDto;
@ComponentScan(basePackages = {"com.prithvi.thoughtprocess.beans"})
@SpringBootApplication
public class Example {
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
控制器类 -
package com.prithvi.thoughtprocess.beans.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.prithvi.thoughtprocess.beans.service.UserService;
import com.prithvi.thoughtprocess.dto.UserDto;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "user/save", method = RequestMethod.POST)
String save(@RequestBody UserDto userDto) {
userService.saveUser(userDto);
return "User save successfully";
}
@RequestMapping(value = "user/list", method = RequestMethod.GET)
List<UserDto> list() {
return userService.listUser();
}
}
调用DAO层的服务类 -
package com.prithvi.thoughtprocess.beans.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.prithvi.thoughtprocess.beans.dao.UserDAO;
import com.prithvi.thoughtprocess.beans.domain.User;
import com.prithvi.thoughtprocess.beans.service.UserService;
import com.prithvi.thoughtprocess.dto.UserDto;
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserDAO userDAO;
@Override
public void saveUser(UserDto userDto) {
userDAO.save(convertToEntity(userDto));
}
public User convertToEntity(UserDto userDto){
User user = new User();
user.setId(userDto.getId());
user.setName(userDto.getName());
return user;
}
public UserDto convertToDto(User user){
UserDto userDto = new UserDto();
userDto.setId(user.getId());
userDto.setName(user.getName());
return userDto;
}
@Override
public List<UserDto> listUser() {
//List<User> users = userDAO.list();
List<UserDto> userDtos = new ArrayList<>();
/*for (User user : users) {
userDtos.add(convertToDto(user));
}*/
return userDtos;
}
}
答案 0 :(得分:2)
将@EntityScan( basePackages = {"com.prithvi.thoughtprocess.beans"}
添加到您的Application类。这是hibernate扫描你的实体所必需的。