Java 如果记录不存在,则休眠插入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18558618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:02:30  来源:igfitidea点击:

Hibernate Insert if record does not exist

javahibernatehql

提问by cloudviz

How can I do this with Hibernate - If record does not exists insert it.

如何使用 Hibernate 执行此操作 - 如果记录不存在,请插入它。

I have the following columns

我有以下几列

Id (Primary Key), Ticker Symbol, Ticker Name, Industry, Sector, LastUpdate

Id(主键)、股票代码、股票名称、行业、部门、LastUpdate

I would like to check the records at Ticker Symbol Column if the string exist don't do anything if it does not exist then insert a new row with Id, Ticker Symbol, Ticker Name, Industry, Sector and Update Date (today's date).

如果字符串存在,我想检查代码符号列中的记录,如果它不存在,则不执行任何操作,然后插入一个包含 Id、代码符号、代码名称、行业、部门和更新日期(今天的日期)的新行。

I have gone as far as creating a new Table with the code below.

我已经使用下面的代码创建了一个新表。

//Hibernate Create a Session Factory    
 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();         
 for (int i = 0; i < Bloomberg.getTickerSymbol().size(); i++){

            //Hibernate to store Stock Tickers Data     
        tickerInfo.setTickerSymbol(Bloomberg.getTickerSymbol().get(i)); //Symbol
        tickerInfo.setTickerName(Bloomberg.getTickerName().get(i)); //Name
        tickerInfo.setTickerSector(Bloomberg.getTickerSector().get(i)); //Sector
        tickerInfo.setTickerIndustry(Bloomberg.getTickerIndustry().get(i)); //Industry
        tickerInfo.setTickerLastUpdate(Calendar.getInstance().getTime()); //Update Date

        org.hibernate.Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.saveOrUpdate(tickerInfo);
        session.getTransaction().commit();
        session.close();
}

采纳答案by Prabhakaran Ramaswamy

 //Hibernate Create a Session Factory    
 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();         
for (int i = 0; i < Bloomberg.getTickerSymbol().size(); i++){

        //Hibernate to store Stock Tickers Data     
    tickerInfo.setTickerSymbol(Bloomberg.getTickerSymbol().get(i)); //Symbol
    tickerInfo.setTickerName(Bloomberg.getTickerName().get(i)); //Name
    tickerInfo.setTickerSector(Bloomberg.getTickerSector().get(i)); //Sector
    tickerInfo.setTickerIndustry(Bloomberg.getTickerIndustry().get(i)); //Industry
    tickerInfo.setTickerLastUpdate(Calendar.getInstance().getTime()); //Update Date

    org.hibernate.Session session = sessionFactory.openSession();
    List tickerInfos = session.createCriteria(TickerInfo.class).add(Restrictions.eq("tickerSymbol", Bloomberg.getTickerSymbol().get(i))).list();
   if(tickerInfos.size()<1){
         session.beginTransaction(); 
         session.saveOrUpdate(tickerInfo);
         session.getTransaction().commit();
   }
   session.close();
 }

回答by samblake

An option could be to create a unique constraint in your database, attempt the update, and catch the exception if the constraint is violated.

一个选项可能是在您的数据库中创建一个唯一约束,尝试更新,并在违反约束时捕获异常。

A better approach might be a mix this with a local cache of known added ticker symbols (assuming symbols are never removed), which should cut down to the amount of database calls being made.

更好的方法可能是将其与已知添加的股票代码的本地缓存混合(假设永远不会删除符号),这应该减少正在执行的数据库调用量。