简体   繁体   中英

Hibernate Insert if record does not exist

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

I have the following columns

Id (Primary Key), Ticker Symbol, Ticker Name, Industry, Sector, 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).

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();
}
 //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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM