3

HEllo,

I'm using nhibernate and have problems regarding user registration on my site.

When user wants to register I create new user record in the database and immidiatelly after that the system is logging the user in.

Well here lies the problem... When creating user record I'm using

NHibernateSession.Save(entity); //does not saves user object immediately to the database. It's kept in the session.

And when I want to log the user in, i load the user by his user name, and then I get null user object.

Why am I getting a null object and how can I make it work?

Thanks

4

2 回答 2

3

好的,我刚刚测试了这个:

        ISession session = s.CreateSession();

        User user = new User();

        user.Number = 122;
        user.UserName = "u";
        user.Id = 1;

        session.Save(user);
        User user1 = session.CreateCriteria<User>().Add(Restrictions.Eq("UserName", "u")).UniqueResult<User>();

        session.Flush();

首先从 CreateCriteria 执行 Select,然后在 Flush 插入时执行。所以这就是为什么它没有找到任何东西。

我也进行了测试,Get<User>(1)它返回传递给 Save 方法的实体 - 不执行任何查询。

仍然-为什么要查询数据库,因为那里有实体?

另外,你说你使用 Get 然后说你想通过 UserName 加载 - UserName 是主键吗?Get 尝试按主键加载。

于 2009-08-17T16:12:53.913 回答
1

如果您的 Save 和 Get 是从不同的会话完成的,那么 Get 将返回 null,因为该对象仅存在于其他会话的内部缓存中,直到它被刷新。

我不确定二级缓存是否会有所作为(如果二级缓存是在保存或刷新时写入的,我不确定)。

于 2009-08-17T12:39:55.047 回答