|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.query.hql; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.Tuple; |
| 10 | +import org.hibernate.cfg.QuerySettings; |
| 11 | +import org.hibernate.query.criteria.HibernateCriteriaBuilder; |
| 12 | +import org.hibernate.query.criteria.JpaCriteriaInsertSelect; |
| 13 | +import org.hibernate.query.criteria.JpaCriteriaInsertValues; |
| 14 | +import org.hibernate.query.criteria.JpaCriteriaQuery; |
| 15 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 16 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 17 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 19 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 20 | +import org.hibernate.testing.orm.junit.Setting; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import java.util.UUID; |
| 24 | + |
| 25 | +@DomainModel( |
| 26 | + annotatedClasses = {InsertConflictWithCriteriayCopyTreeEnabledTests.ItemEntity.class} |
| 27 | +) |
| 28 | +@ServiceRegistry( |
| 29 | + settings = {@Setting(name = QuerySettings.CRITERIA_COPY_TREE, value = "true")} |
| 30 | +) |
| 31 | +@SessionFactory |
| 32 | +@JiraKey("HHH-19314") |
| 33 | +public class InsertConflictWithCriteriayCopyTreeEnabledTests { |
| 34 | + |
| 35 | + @Test |
| 36 | + void createCriteriaInsertValuesTest(SessionFactoryScope scope) { |
| 37 | + scope.inTransaction( |
| 38 | + session -> { |
| 39 | + HibernateCriteriaBuilder cb = session.getCriteriaBuilder(); |
| 40 | + |
| 41 | + JpaCriteriaInsertValues<ItemEntity> insertIntoItem = cb.createCriteriaInsertValues( |
| 42 | + ItemEntity.class ); |
| 43 | + insertIntoItem.setInsertionTargetPaths( insertIntoItem.getTarget().get( "id" ) ); |
| 44 | + insertIntoItem.values( cb.values( cb.value( UUID.randomUUID() ) ) ); |
| 45 | + insertIntoItem.onConflict().onConflictDoNothing(); |
| 46 | + |
| 47 | + session.createMutationQuery( insertIntoItem ).executeUpdate(); |
| 48 | + } |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void createCriteriaInsertSelectTest(SessionFactoryScope scope) { |
| 54 | + scope.inTransaction( |
| 55 | + session -> { |
| 56 | + HibernateCriteriaBuilder cb = session.getCriteriaBuilder(); |
| 57 | + |
| 58 | + JpaCriteriaInsertSelect<ItemEntity> insertIntoItem = cb.createCriteriaInsertSelect( |
| 59 | + ItemEntity.class ); |
| 60 | + insertIntoItem.setInsertionTargetPaths( insertIntoItem.getTarget().get( "id" ) ); |
| 61 | + JpaCriteriaQuery<Tuple> cq = cb.createQuery( Tuple.class ); |
| 62 | + |
| 63 | + cq.multiselect( cb.literal( UUID.randomUUID() ) ); |
| 64 | + insertIntoItem.select( cq ); |
| 65 | + insertIntoItem.onConflict().onConflictDoNothing(); |
| 66 | + session.createMutationQuery( insertIntoItem ).executeUpdate(); |
| 67 | + } |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + @Entity(name = "ItemEntity") |
| 72 | + public static class ItemEntity { |
| 73 | + @Id |
| 74 | + private UUID id; |
| 75 | + |
| 76 | + } |
| 77 | +} |
0 commit comments