Hibernate saveorUpdate Method Problem

I'm trying to work with Hibernate (the database for the first time), and some how to choose the best way to use saveOrUpdate or Save.Update. I have a POJO destination class and its other attributes that need to be updated with the Destination entity. I get the XML file to import, and I use the following code to update / save the destination object along with my attribute classes.

try{
        getSessionFactory().getCurrentSession().beginTransaction();
        getSessionFactory().getCurrentSession().saveOrUpdate(entity);
        getSessionFactory().getCurrentSession().getTransaction().commit();
        }
        catch(Exception e){
            getSessionFactory().getCurrentSession().getTransaction().rollback();
        }
        finally{
            getSessionFactory().close();
        }

everything works fine until I use the exact same instance of the session. But later, when I use the same XML file to update the target software for certain attributes, it gives me the following error.

SEVERE: Duplicate entry 'MNLI' for key 'DESTINATIONID'
9 Jan, 2011 4:58:11 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
    at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2242)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2678)
    at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)

UUID Destination, , . , secodn hibernate , insert, .

- userid, , , . , .?

+3
1

saveOrUpdate() (. 11.7. ):

  • ,
  • , , ,
  • , save() it
  • , , save() it
  • a , version - , , save() it
  • ()

, , XML .

+2

Source: https://habr.com/ru/post/1784366/


All Articles