【问题标题】:How to correctly describe sessionFactory()?如何正确描述 sessionFactory()?
【发布时间】:2013-12-28 09:05:00
【问题描述】:

我正在使用最终的休眠版本 (4.3.0) 并尝试创建 sessionFactory() 实例,但似乎一切都已弃用。

我试试这个:

它显示,找不到像"buildServiceRegistry()" 这样的符号。 ServiceRegistryBuilder() 也已弃用。那么,坦率地说如何实例化它呢?

代码在这里:http://pastebin.com/EkqfRxkB

【问题讨论】:

  • 请不要张贴图片,将实际代码写到问题的正文中。
  • 对不起,我以为会更清楚
  • 嗯,这会让想要尝试你的代码的人更难。
  • 使用 StandardServiceRegistryBuilder 代替

标签: java hibernate sessionfactory


【解决方案1】:
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class Regiutil 
{
    public static final SessionFactory sf=build();
    public static SessionFactory build()
    {
        SessionFactory s=null;
        try
        {
            s= new AnnotationConfiguration().configure().buildSessionFactory();
            return s;
        }
        catch(Throwable t)
        {

        }
        return s;
    }
    public static SessionFactory getsessionfactory()
    {
        return sf;
    }
}

然后我们可以像这样在其他文件/类中轻松使用它:

     public static Session ss=null;
     public static Transaction t= null;

    public static boolean insertdata(Loginmodel e1)
     {
         try
         {
             ss = Regiutil.getsessionfactory().openSession();
             t= ss.beginTransaction();
             ss.save(e1);
             t.commit();
             return true;
         }
         catch (Exception e) 
         {
             return false;
         }
         finally
         {
             ss.close();
         }
     }

【讨论】:

    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 2015-10-09
    • 2018-06-01
    • 2021-10-09
    • 2012-02-19
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    相关资源
    最近更新 更多