简体   繁体   中英

Merge multiple objects with Hibernate

I'm using JPA with Hibernate and spring-boot-starter-data-jpa. I want to merge/update a set of Items. If it's a new item, I want to persist, if it's an already existsing item i want to update it.

    @PersistenceContext(unitName = "itemEntityManager")
    private EntityManager em;

    @Transactional
    public void saveItems(Set<Item>> items) {
        items.forEach(em::merge);
    }

When I try it like this, every Item creates a new HQL statment and it's inperformant . So I'm looking for a way to save all Items in one time (if it's possible), to save calls and time.

I found this:

        EntityTransaction transaction = em.getTransaction();
        transaction.begin();
        items.forEach(em::merge);
        transaction.commit();

but i can't use this transaction because i use the @Transactional . Is there a way with native SQL?

I created a native SQL statement:

  1. I joined all items as a list of values in sql format
String sqlValues = String.join(",", items.stream().map(this::toSqlEntry).collect(Collectors.toSet()));
  1. Then i called a native query
    em.createNativeQuery("INSERT INTO config_item"
                + "( id, eaid, name, type, road, offs, created, deleted )"
                + " VALUES " + sqlValues
                + " ON CONFLICT (id) DO UPDATE "
                + " SET "
                    + "eaid=excluded.eaid,\n"
                    + "name=excluded.name,\n"
                    + "type=excluded.type,\n"
                    + "road=excluded.road,\n"
                    + "offs=excluded.offs,\n"
                    + "created=excluded.created,\n"
                    + "deleted=excluded.deleted;"
        ).executeUpdate();

That's a lot faster and works

You could use @SQLInsert for this purpose to use batch inserts. See Hibernate Transactions and Concurrency Using attachDirty (saveOrUpdate)

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