【问题标题】:Hibernate, select by id or unique column休眠,按 id 或唯一列选择
【发布时间】:2010-06-17 03:18:14
【问题描述】:

我正在使用 java 的 hibernate,并且我希望能够通过 id 或名称从数据库中选择用户。

Hibernate 的好处是它通过 id 缓存结果,但我似乎无法让它按名称缓存。

static Session openSession = Factory.openSession();

public static User GetUser(int Id) {
    return (User) openSession.get(User.class, new Integer(Id));
}

public static User GetUser( String Name ){
   return (User) openSession.createCriteria( User.class ).
           add( Restrictions.eq("username", Name) ).
           uniqueResult();

}

如果我多次使用 GetUser(1),hibernate 只会显示它第一次执行。

但是每次我使用 GetUser("user1") 时,hibernate 都会显示它正在对数据库执行新的查询。

同时缓存字符串标识符的最佳方法是什么?

【问题讨论】:

    标签: java hibernate select unique


    【解决方案1】:

    按名称查询时需要使用查询缓存,避免数据库访问。

    http://www.javalobby.org/java/forums/t48846.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      相关资源
      最近更新 更多