Hibernate session.save()问题

时间:2017-11-08 17:15:57

标签: java mysql hibernate

我正在尝试将实体Patient插入mysql db,但session.save()失败并出现以下错误:

  

exception:“org.hibernate.PropertyAccessException”,消息:“可以   不通过反射设置字段值[7]值   ...... .projects.dentistappointmentsys.models.Patient.id“,...}

这是我的实体:

function grid_timing(nX, nY)
  [xt, yt] = teepee_grid(nX, nY);
  [xg, yg] = gnovice_grid(nX, nY);
  [xA, yA] = Aero_grid(nX, nY);
  [xD, yD] = DeviL_grid(nX, nY);
  if ~isequal(xt, xg, xA, xD) || ...
     ~isequal(yt, yg, yA, yD)
    disp('Solutions don''t match!');
  end
  fprintf('teepee:  %g\n', timeit(@() teepee_grid(nX, nY), 2));
  fprintf('gnovice: %g\n', timeit(@() gnovice_grid(nX, nY), 2));
  fprintf('Aero:    %g\n', timeit(@() Aero_grid(nX, nY), 2));
  fprintf('Dev-iL:  %g\n', timeit(@() DeviL_grid(nX, nY), 2));
end
function [xVec, yVec] = gnovice_grid(nX, nY)
  xVec = repmat(linspace(0,1,nX).', [nY 1]);
  yVec = repelem(linspace(0,1,nY).', nX);
end
function [xVec, yVec] = Aero_grid(nX, nY)
  yVec = linspace(0,1,nY).*ones(nX,1);
  xVec = linspace(0,1,nX).'.*ones(1,nY);
  yVec = yVec(:);
  xVec = xVec(:);
end
function [xVec, yVec] = DeviL_grid(nX, nY)
  [YY, XX] = meshgrid(linspace(0,1,nY), linspace(0,1,nX));
  xVec = XX(:);
  yVec = YY(:);
end
function [xVec, yVec] = teepee_grid(nX, nY)
  xArray    = repmat(linspace(0,1,nX), nY, 1);
  xVec      = reshape(xArray', [nX*nY 1]);
  yArray    = repmat(linspace(0,1,nY), nX, 1)';
  yVec      = reshape(yArray' , [nX*nY 1]);
end

这是我的会议:

@Entity
@Table(name="patient")
public class Patient {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    Long id;

    String firstName;

    String lastName;

    String email;

    String password;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Patient(){}

    public Patient(String firstName, String lastName, String email, String password) {

        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
    }
}

知道为什么会出现此错误或如何修复错误?提前谢谢。

这是我的堆栈跟踪:

Hibernate:从hibernate_sequence中选择next_val作为id_val进行更新 Hibernate:更新hibernate_sequence set next_val =? next_val =?

SessionFactory factory = new Configuration().configure()
                .buildSessionFactory();
        Session session = factory.openSession();

        //Add new Employee object
        Patient p1 = new Patient();
        p1.setEmail("lokesh@mail.com");
        p1.setFirstName("lokesh");
        p1.setLastName("gupta");
        p1.setPassword("gupta123");

        session.save(p1);

        session.flush();
        session.close();

更新:更改@GeneratedValue(策略= GenerationType.IDENTITY)

新的堆栈跟踪:

org.hibernate.PropertyAccessException: Could not set field value [8] value by reflection : [class com.nbu.projects.dentistappointmentsys.models.Patient.id] setter of com.nbu.projects.dentistappointmentsys.models.Patient.id
    at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:58)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.setIdentifier(AbstractEntityTuplizer.java:260)
    at org.hibernate.persister.entity.AbstractEntityPersister.setIdentifier(AbstractEntityPersister.java:4680)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:168)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
    at com.nbu.projects.dentistappointmentsys.controllers.PatientController.getPatients(PatientController.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field com.nbu.projects.dentistappointmentsys.models.Patient.id to com.nbu.projects.dentistappointmentsys.models.Patient
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75)
    at java.lang.reflect.Field.set(Field.java:764)
    at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:38)
    ... 67 more

0 个答案:

没有答案