@@ -0,0 +1,3048 @@
+package com.vladmihalcea.book.hpjp.hibernate.query.upsert;%0A%0Aimport com.vladmihalcea.book.hpjp.hibernate.identifier.composite.CompositeIdIdentityGeneratedTest;%0Aimport com.vladmihalcea.book.hpjp.util.AbstractMySQLIntegrationTest;%0Aimport com.vladmihalcea.book.hpjp.util.AbstractTest;%0Aimport com.vladmihalcea.book.hpjp.util.exception.ExceptionUtil;%0Aimport org.hibernate.Session;%0Aimport org.hibernate.annotations.NaturalId;%0Aimport org.hibernate.annotations.SQLInsert;%0Aimport org.junit.Test;%0A%0Aimport javax.persistence.*;%0Aimport java.io.Serializable;%0Aimport java.util.List;%0Aimport java.util.concurrent.CountDownLatch;%0Aimport java.util.concurrent.atomic.AtomicBoolean;%0A%0Aimport static org.junit.Assert.assertTrue;%0A%0A/**%0A * @author Vlad Mihalcea%0A */%0Apublic class MySQLUpsertUniqueColumnTest extends AbstractMySQLIntegrationTest %7B%0A%0A    @Override%0A    protected Class%3C?%3E%5B%5D entities() %7B%0A        return new Class%3C?%3E%5B%5D %7B%0A            Book.class,%0A        %7D;%0A    %7D%0A%0A    private final CountDownLatch aliceLatch = new CountDownLatch(1);%0A%0A    @Test%0A    public void test() %7B%0A        doInJPA(entityManager -%3E %7B%0A            Book book = new Book();%0A            book.setTitle(%22High-Performance Java Persistence%22);%0A            book.setIsbn(%22978-9730228236%22);%0A            entityManager.persist(book);%0A%0A            final AtomicBoolean preventedByLocking = new AtomicBoolean();%0A%0A            executeAsync(() -%3E %7B%0A                try %7B%0A                    doInJPA(_entityManager -%3E %7B%0A                        _entityManager.unwrap(Session.class).doWork(this::setJdbcTimeout);%0A                        _entityManager.createNativeQuery(%0A                            %22INSERT IGNORE %22 +%0A                            %22INTO book (title, isbn) %22 +%0A                            %22VALUES (:title, :isbn)%22)%0A                        .setParameter(%22title%22, %22High-Performance Hibernate%22)%0A                        .setParameter(%22isbn%22, %22978-9730228236%22)%0A                        .executeUpdate();%0A                    %7D);%0A                %7D catch (Exception e) %7B%0A                    if( ExceptionUtil.isLockTimeout( e )) %7B%0A                        preventedByLocking.set( true );%0A                    %7D%0A                %7D%0A%0A                aliceLatch.countDown();%0A            %7D);%0A%0A            awaitOnLatch(aliceLatch);%0A%0A            assertTrue(preventedByLocking.get());%0A        %7D);%0A    %7D%0A%0A    @Entity(name = %22Book%22)%0A    @Table(name = %22book%22)%0A    public static class Book %7B%0A%0A        @Id%0A        @GeneratedValue(strategy = GenerationType.IDENTITY)%0A        private Long id;%0A%0A        private String title;%0A%0A        @NaturalId%0A        private String isbn;%0A%0A        public Long getId() %7B%0A            return id;%0A        %7D%0A%0A        public void setId(Long id) %7B%0A            this.id = id;%0A        %7D%0A%0A        public String getTitle() %7B%0A            return title;%0A        %7D%0A%0A        public void setTitle(String title) %7B%0A            this.title = title;%0A        %7D%0A%0A        public String getIsbn() %7B%0A            return isbn;%0A        %7D%0A%0A        public void setIsbn(String isbn) %7B%0A            this.isbn = isbn;%0A        %7D%0A    %7D%0A%7D%0A

Add new test for Upsert by non-PK