在更新查询期间在实体类中获取空标识符错误

时间:2015-02-26 07:44:58

标签: java hibernate

实体类:

package com.rapid.admin.entity;

import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * LoginHistory entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "login_history", catalog = "project")
public class LoginHistory implements java.io.Serializable {
// Fields

    private Integer id;
    private String username;
    private Timestamp loginTime;
    private Timestamp logoutTime;
    private Boolean status;
    private String timeId;

// Constructors

/** default constructor */
public LoginHistory() {
}

/** full constructor */
public LoginHistory(String username, Timestamp loginTime,
        Timestamp logoutTime, Boolean status, String timeId) {
    this.username = username;
    this.loginTime = loginTime;
    this.logoutTime = logoutTime;
    this.status = status;
    this.timeId = timeId;
}

// Property accessors
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

@Column(name = "username", length = 45)
public String getUsername() {
    return this.username;
}

public void setUsername(String username) {
    this.username = username;
}

@Column(name = "login_time", length = 19)
public Timestamp getLoginTime() {
    return this.loginTime;
}

public void setLoginTime(Timestamp loginTime) {
    this.loginTime = loginTime;
}

@Column(name = "logout_time", length = 19, updatable= true)
public Timestamp getLogoutTime() {
    return this.logoutTime;
}

public void setLogoutTime(Timestamp logoutTime) {
    this.logoutTime = logoutTime;
}

@Column(name = "status")
public Boolean getStatus() {
    return this.status;
}

public void setStatus(Boolean status) {
    this.status = status;
}

@Column(name = "time_id", length = 45)
public String getTimeId() {
    return this.timeId;
}

public void setTimeId(String timeId) {
    this.timeId = timeId;
}

Hibernate查询:

sessionFactory.getCurrentSession().beginTransaction();
Criteria criteria = 
    sessionFactory.getCurrentSession().createCriteria(LoginHistory.class);
criteria.add(Restrictions.eq("id",id));
LoginHistory loginHistory = new LoginHistory();
loginHistory.setLogoutTime(AdminUtil.getTimeStamp());

sessionFactory.getCurrentSession().update(loginHistory);

控制台出错:

 org.hibernate.TransientObjectException: The given object has a null identifier: com.rapid.admin.entity.LoginHistory
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.getUpdateId(DefaultSaveOrUpdateEventListener.java:272)
        at org.hibernate.event.def.DefaultUpdateEventListener.getUpdateId(DefaultUpdateEventListener.java:69)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:240)
        at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:56)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
        at org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:592)
        at org.hibernate.impl.SessionImpl.update(SessionImpl.java:580)
        at org.hibernate.impl.SessionImpl.update(SessionImpl.java:572)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
        at $Proxy21.update(Unknown Source)
        at com.rapid.admin.daoimpl.LoginDaoImpl.updateLogoutTime(LoginDaoImpl.java:117)
        at com.rapid.admin.serviceimpl.LoginServiceImpl.updateLogoutTime(LoginServiceImpl.java:53)
        at com.rapid.admin.controller.LoginController.logout(LoginController.java:87)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:619)
    org.hibernate.TransientObjectException: The given object has a null identifier: com.rapid.admin.entity.LoginHistory

1 个答案:

答案 0 :(得分:1)

我想你应该这样写:(考虑你要更新LoginHistory已经在你正在通过的id的数据库中

hibernate查询:

            sessionFactory.getCurrentSession().beginTransaction();
            Criteria criteria = sessionFactory.getCurrentSession().createCriteria(LoginHistory.class);
            criteria.add(Restrictions.eq("id",id));
            LoginHistory loginHistory = (LoginHistory)criteria.uniqueResult();
            loginHistory.setLogoutTime(AdminUtil.getTimeStamp());
            sessionFactory.getCurrentSession().update(loginHistory);