简体   繁体   中英

How ignore hibernate error and continue insert data?

I have some method in my DAO class:

public void insertAVAYAcmCDRs(List<AvayaCmCdr> cdrList) {
        AvayaCmCdr aCdrList1 = null;
        try {
            em.getTransaction().begin();
            for (AvayaCmCdr aCdrList : cdrList) {
                aCdrList1 = aCdrList;
                em.persist(aCdrList);
            }
            em.getTransaction().commit();
            em.clear();
        } catch (Exception e) {
            logger.log(Level.INFO, "Exception in  task time={0}. Exception message = {1}.", new Object[]{aCdrList1.getDate(), e.getMessage()});
        }
    }

I tried save all array entities to DB. But in DB i have uniqe index - it does not allow to insert duplicate rows. It work normaly on DB side but i have some error in java.

a different object with the same identifier value was already associated with the session: 

I get this error on 2 step of cycle. I print this object and found dublicate in DB.

I want ignore this error and continue insert data or somehow handle the error.

if this row already in the database i want ignore and skip it and continue insert

Why are you assigning this aCdrList1 = aCdrList ? Is there any specific reason? you can save aCdrList object. Use below one

em.saveOrUpdate(aCdrList);

or

em.merge(aCdrList);   

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