1

Entity class:

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 query :

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);

Error on console:

 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
  • I am not getting your code. first you are creating criteria to fetch LoginHistory by it's id and without assigning into to any object you are creating new object of LoginHistory and setting LogoutTime in it and then sending that object for update which may have id as null Commented Feb 26, 2015 at 8:15

1 Answer 1

2

I guess you should write like this : (by considering you want to update LoginHistory which is already in DB having id that you are passing)

hibernate query :

            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);
Sign up to request clarification or add additional context in comments.

5 Comments

Now the error is gone but still the update query is not working,its only selecting the row with where caluse
You must call session.close() and session.flush() after finishing update this will write changes in DB.
You can also google for hibernate transaction concept to learn about hibernate transaction n session.
used : sessionFactory.getCurrentSession().close(); still no update
problem solved i was not using commit(); thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.