Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Hibernate在获取max id时给了我旧对象_C#_Hibernate - Fatal编程技术网

C# Hibernate在获取max id时给了我旧对象

C# Hibernate在获取max id时给了我旧对象,c#,hibernate,C#,Hibernate,我正在使用hibernate和SpringMVC。 我对整个web应用程序使用单会话工厂实例/线程。 我正在基于每个请求使用会话对象 我获取max id,它给了我正确的值,然后我用相同的增量max id更新表中的某些内容 我面临的问题是,当我再次获取max id时,它会给我相同的先前max id,但不是更新的max id 棘手的是,在异常出现后,如果我再次尝试,它会给我正确的max id值 注意:-在每次获取max id时,我都会打开一个新的会话对象,然后最后将其关闭 这是我的代码:- publ

我正在使用hibernate和SpringMVC。 我对整个web应用程序使用单会话工厂实例/线程。 我正在基于每个请求使用会话对象

我获取max id,它给了我正确的值,然后我用相同的增量max id更新表中的某些内容

我面临的问题是,当我再次获取max id时,它会给我相同的先前max id,但不是更新的max id

棘手的是,在异常出现后,如果我再次尝试,它会给我正确的max id值

注意:-在每次获取max id时,我都会打开一个新的会话对象,然后最后将其关闭

这是我的代码:-

public static void prepareLogin(HttpServletRequest request,
            String loggedUser, int loggedUserType, int loggedUserId) {


        Session session = HibernateUtil.openSession();
        Transaction tr = session.beginTransaction();

        // fetching the max id from login manager table to update the login and
        // logout or various details
        int loginManagerMaxIdInt = 0;
        Criteria c2 = session.createCriteria(LoginManager.class);
        c2.addOrder(Order.desc("id"));
        c2.setMaxResults(1);
        LoginManager loginManager = (LoginManager) c2.uniqueResult();
        if (loginManager != null) {
            loginManagerMaxIdInt = loginManager.getId();
        }

        loginManagerMaxIdInt++;

        System.out.println("logManId: " + loginManagerMaxIdInt);

        // preparing the new loginManager object to be stored in the DB
        LoginManager newLoginManager = new LoginManager();
        newLoginManager.setId(loginManagerMaxIdInt);
        newLoginManager.setUserId(loggedUserId);
        newLoginManager.setLoginTime(new Date(System.currentTimeMillis()));
        UserTypeMaster userTypeMaster = new UserTypeMaster();
        userTypeMaster.setId(loggedUserType);
        newLoginManager.setUserTypeMaster(userTypeMaster);

        // binding the username, userType and loginManager id value to
        // the session
        // object
        request.getSession().setAttribute("loggedUser", loggedUser);
        request.getSession().setAttribute("loggedUserType", loggedUserType);
        request.getSession().setAttribute("loggedLoginManagerId",
                loginManagerMaxIdInt);

        // finally saving it and commiting
        session.save(newLoginManager);
        tr.commit();
        session.close();

    }
第二次编辑: 保存此对象后,我将使用以下代码更新数据库中的同一对象:

// logout method to logout the user by just invalidating the session object
// because in that session object only all important object are bind
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public ModelAndView logout(HttpServletRequest request) {

    //new session
    Session session = HibernateUtil.openSession();
    //begin transaction logout time updation
    Transaction tr = session.beginTransaction();

    //fetching the login_Manager row from login_manager table for current 
    //logged User 
    LoginManager loginManager = (LoginManager) session.get(
            LoginManager.class,
            Utility.getLoggedLoginManagerId(request));

    //changing the logut time to current time in the fetched object
    loginManager.setLogoutTime(new Date(System.currentTimeMillis()));

    //updating the same object to the db
    session.update(loginManager);
    tr.commit();

    //invalidating the sessin object 
    request.getSession().invalidate();

    //finally closing the session
    session.close();
    return new ModelAndView("redirect:" + "login");
}
注意:如果我不使用第二种方法,那么我的代码工作正常。i、 如果我只保存新对象,那么一切都很好,max id也可以完美地抓取


请帮我解决这个问题。

阅读会话刷新,它将会话数据与数据库同步。如果会话未同步,则不会看到更新的数据

示例:在提交/更新行之前刷新会话,您将看不到预期的结果,但是下一次刷新应该可以做到这一点(将会话与数据库同步)


“异常”之后,您将打开一个新会话,其中包含数据库的实际状态,这就是它工作的原因。

首先,很抱歉我在本文中提出了错误的问题

问题与此
2
有关


我在配置文件中添加了上述代码,现在一切正常。

您的代码有问题。让我发布我的代码您还没有发布读取和增加值的代码。代码的其余部分在哪里?在上面使用条件的代码中,我在那里读取max id,然后通过loginManagerMaxIdInt++递增相同的检索值;是的,我看得出来。但问题是,当你在别处阅读max时,你并没有得到你所期望的。您还没有发布该代码。您能为samei分享推荐的文章吗?使用2解决了这个问题