|
| 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 | + |
| 24 | +@DomainModel( |
| 25 | + annotatedClasses = { |
| 26 | + InsertConflictWithCriteriaCopyTreeEnabledTests.TestEntity.class, |
| 27 | + InsertConflictWithCriteriaCopyTreeEnabledTests.AnotherTestEntity.class, |
| 28 | + } |
| 29 | +) |
| 30 | +@ServiceRegistry( |
| 31 | + settings = {@Setting(name = QuerySettings.CRITERIA_COPY_TREE, value = "true")} |
| 32 | +) |
| 33 | +@SessionFactory |
| 34 | +@JiraKey("HHH-19314") |
| 35 | +public class InsertConflictWithCriteriaCopyTreeEnabledTests { |
| 36 | + |
| 37 | + @Test |
| 38 | + void createCriteriaInsertValuesTest(SessionFactoryScope scope) { |
| 39 | + scope.inTransaction( |
| 40 | + session -> { |
| 41 | + HibernateCriteriaBuilder cb = session.getCriteriaBuilder(); |
| 42 | + |
| 43 | + JpaCriteriaInsertValues<TestEntity> insertIntoItem = cb |
| 44 | + .createCriteriaInsertValues( TestEntity.class ); |
| 45 | + insertIntoItem.setInsertionTargetPaths( insertIntoItem.getTarget().get( "id" ) ); |
| 46 | + insertIntoItem.values( cb.values( cb.value( 1L ) ) ); |
| 47 | + insertIntoItem.onConflict().onConflictDoNothing(); |
| 48 | + |
| 49 | + session.createMutationQuery( insertIntoItem ).executeUpdate(); |
| 50 | + } |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void createCriteriaInsertSelectTest(SessionFactoryScope scope) { |
| 56 | + scope.inTransaction( |
| 57 | + session -> { |
| 58 | + HibernateCriteriaBuilder cb = session.getCriteriaBuilder(); |
| 59 | + |
| 60 | + JpaCriteriaInsertSelect<TestEntity> insertIntoItem = cb |
| 61 | + .createCriteriaInsertSelect( TestEntity.class ); |
| 62 | + insertIntoItem.setInsertionTargetPaths( insertIntoItem.getTarget().get( "id" ) ); |
| 63 | + |
| 64 | + JpaCriteriaQuery<Tuple> cq = cb.createQuery( Tuple.class ); |
| 65 | + cq.select( cb.tuple( cb.literal( 1 ) ) ); |
| 66 | + cq.fetch( 1 ); |
| 67 | + insertIntoItem.select( cq ); |
| 68 | + insertIntoItem.onConflict().onConflictDoNothing(); |
| 69 | + |
| 70 | + session.createMutationQuery( insertIntoItem ).executeUpdate(); |
| 71 | + } |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + @Entity(name = "TestEntity") |
| 76 | + public static class TestEntity { |
| 77 | + @Id |
| 78 | + private Long id; |
| 79 | + |
| 80 | + private String name; |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + @Entity(name = "AnotherTestEntity") |
| 85 | + public static class AnotherTestEntity { |
| 86 | + @Id |
| 87 | + private Long id; |
| 88 | + |
| 89 | + private String name; |
| 90 | + |
| 91 | + } |
| 92 | +} |
0 commit comments