Java Hibernate声明连接有效 [英] Java hibernate declared connection effective

查看:43
本文介绍了Java Hibernate声明连接有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是显示信息数据表的命令,我看到它是重复的声明

This is the command to display information data sheet, I see the declaration it is repeated

代码1

public class DBTable {

    public List<MdGmail> showGmail() {
       Configuration conf = new Configuration().configure("hibernate.cfg.xml");
       SessionFactory sf = conf.buildSessionFactory();
       currentSession = sf.getCurrentSession();
       currentSession.beginTransaction();
       return currentSession.createCriteria(MdGmail.class).list();
    }

    public List<MdBlogger> showBlogger() {
       Configuration conf = new Configuration().configure("hibernate.cfg.xml");
       SessionFactory sf = conf.buildSessionFactory();
       currentSession = sf.getCurrentSession();
       currentSession.beginTransaction();    
       return currentSession.createCriteria(MdBlogger.class).list();
    }
}

在我的类DBTable中,我创建了一个列表函数以将数据库中的数据表显示为表gmail,顺序... 在第一个代码1中,需要重写每个函数的声明,所以我想要的是为所有带有代码2的人创建一个连接函数.

In my class DBTable,I create a list function to display data table in a database as table gmail,order... With the first code 1 need to rewrite the declaration for each function, so what I want is to create a connection function for all with code 2

但是使用此声明的方式,它不会更新数据库中已更改的值

But with the way this declaration it will not update the value was changed in the database

代码2

public class DBTable {

    private static final Configuration conf = new Configuration().configure("hibernate.cfg.xml");
    private static final SessionFactory sf = conf.buildSessionFactory();
    private static Session currentSession;

    public DBTable() {
        currentSession = sf.getCurrentSession();
        currentSession.beginTransaction();
    }

    private static DBTable instance = null;

    public static DBTable getInstance() {
        if (instance == null) {
            instance = new DBTable();
        }
        return instance;
    }
    public List<MdGmail> showTableGmail() {
        return currentSession.createCriteria(MdGmail.class).list();
    }
    public List<MdGmail> showTableOrder() {
        return currentSession.createCriteria(MdGmail.class).list();
    }
}

如果使用新的附加数据功能,将关闭连接并显示表明我的表将停止工作的命令数据.请帮助我

And if use new additional data function, the connection will be closed and command data showing that my table would stop working.Please help me

    public boolean saveOrUpdateGmail(MdGmail table) {
    try {
        currentSession.saveOrUpdate(table);
        currentSession.getTransaction().commit();
        return true;
    } catch (Exception e) {
        currentSession.getTransaction().rollback();
        e.printStackTrace();
        return false;
    }
}

推荐答案

使用Hibernate的最简单策略是打开一个会话(或获取当前会话),开始事务,执行请求,提交并关闭每个操作的会话(showTableGmail, saveOrUpdateGmail).因此,您需要从DBTable()构造函数中删除所有代码,并执行类似

The simplest strategy of using Hibernate is open a session (or get current one), begin a transaction, do request, commit, and close a session for every operation (showTableGmail, saveOrUpdateGmail). So you need to delete all code from DBTable() constructor and do something like this.

这篇关于Java Hibernate声明连接有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆