我是Hibernate的新手,正在遇到问题。
我正在尝试使用SpringBoot + Hibernate的REST服务教程
我的班级档案如下:
用户实体:
@Entity
@Table(name="USER_DETAILS")
public class User
{
@Id
@Column(name = "user_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;
@Column(name = "user_name")
private String userNamee;
@Column(name = "password")
private String password;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUserNamee() {
return userNamee;
}
public void setUserNamee(String userNamee) {
this.userNamee = userNamee;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserBean:
public class UserBean
{
private Long userId;
private String userNamee;
private String password;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUserNamee() {
return userNamee;
}
public void setUserNamee(String userNamee) {
this.userNamee = userNamee;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
控制器:
@RestController
public class UserController
{
@Autowired
private IUserService userService;
@RequestMapping(value = "/saveData", method = RequestMethod.GET)
public List<UserBean> saveData()
{
UserBean userBean = new UserBean();
userBean.setUserNamee("admin");
userBean.setPassword("admin@123");
userService.saveUser(userBean);
return userService.users();
}
}
服务:
@Service
public class UserService implements IUserService
{
@Autowired
private IUserDao userDao;
@Override
@Transactional
public void saveUser(UserBean userBean)
{
User user = new User();
BeanUtils.copyProperties(userBean, user);
userDao.saveUser(user);
}
@Override
@Transactional
public List<UserBean> users()
{
List<UserBean> list = new ArrayList<UserBean>();
List<User> repolist = userDao.users();
for (User user : repolist)
{
UserBean userBean = new UserBean();
BeanUtils.copyProperties(user, userBean);
list.add(userBean);
}
return list;
}
}
道:
@Repository
public class UserDao implements IUserDao
{
@Autowired
private SessionFactory sessionFactory;
@Override
public void saveUser(User user)
{
try
{
sessionFactory.getCurrentSession().saveOrUpdate(user);
} catch (Exception e)
{
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
@Override
public List<User> users()
{
return sessionFactory.getCurrentSession().createCriteria(User.class).list();
}
}
每当我调用/ saveData时,都会出现错误。
org.hibernate.property.access.spi.PropertyAccessException:通过反射访问持久性[com.test.app.entity]字段[私有java.lang.Long com.test.app.entity.User.userId]时出错.User#userId]:com.test.app.entity.User@49630afc 在org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:43) 在org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:225) 在org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4675) 在org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4376) 在org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226) 在org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:510) 在org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:83) 在org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) 在org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:648) 在org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:640) 在org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:635) 在com.test.app.daoimpl.UserDao.saveUser(UserDao.java:26) 在com.test.app.daoimpl.UserDao $$ FastClassBySpringCGLIB $$ 968d450f.invoke() 在org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 在org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 在org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 在org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) 在com.test.app.daoimpl.UserDao $$ EnhancerBySpringCGLIB $$ ffad9644.saveUser() 在com.test.app.serviceimpl.UserService.saveUser(UserService.java:30) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源) 在java.lang.reflect.Method.invoke(未知来源) 在org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) 在org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 在org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99) 在org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) 在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) 在com.sun.proxy。$ Proxy76.saveUser(未知来源) 在com.test.app.controller.UserController.saveData(UserController.java:39) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源) 在java.lang.reflect.Method.invoke(未知来源) 在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) 在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) 在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) 在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) 在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) 在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) 在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) 在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) 在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) 在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 在org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:868) 在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1455) 在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 在java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源) 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源) 在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.lang.Thread.run(未知来源) 引起原因:java.lang.IllegalArgumentException:无法将java.lang.Long字段com.test.app.entity.User.userId设置为com.test.app.entity.User 在sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(未知来源) 在sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(未知来源) 在sun.reflect.UnsafeFieldAccessorImpl.ensureObj(未知来源) 在sun.reflect.UnsafeObjectFieldAccessorImpl.get(未知来源) 在java.lang.reflect.Field.get(未知来源) 在org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:39) ...还有88个
我做错了什么? 提前致谢。