|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.batch; |
| 8 | + |
| 9 | +import org.hibernate.cfg.AvailableSettings; |
| 10 | +import org.hibernate.engine.jdbc.JdbcLogging; |
| 11 | + |
| 12 | +import org.hibernate.testing.logger.LogInspectionHelper; |
| 13 | +import org.hibernate.testing.logger.TriggerOnPrefixLogListener; |
| 14 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 15 | +import org.hibernate.testing.orm.junit.Jira; |
| 16 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 19 | +import org.hibernate.testing.orm.junit.Setting; |
| 20 | +import org.junit.jupiter.api.AfterAll; |
| 21 | +import org.junit.jupiter.api.BeforeAll; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import jakarta.persistence.CascadeType; |
| 26 | +import jakarta.persistence.Entity; |
| 27 | +import jakarta.persistence.GeneratedValue; |
| 28 | +import jakarta.persistence.Id; |
| 29 | +import jakarta.persistence.Inheritance; |
| 30 | +import jakarta.persistence.InheritanceType; |
| 31 | +import jakarta.persistence.JoinColumn; |
| 32 | +import jakarta.persistence.JoinTable; |
| 33 | +import jakarta.persistence.ManyToOne; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Marco Belladelli |
| 39 | + */ |
| 40 | +@SessionFactory |
| 41 | +@DomainModel( annotatedClasses = { |
| 42 | + BatchRowCountWarningTest.BaseEntity.class, |
| 43 | + BatchRowCountWarningTest.SubEntity.class, |
| 44 | + BatchRowCountWarningTest.MyEntity.class, |
| 45 | + BatchRowCountWarningTest.SpamEntity.class, |
| 46 | + BatchRowCountWarningTest.JoinTableEntity.class, |
| 47 | +} ) |
| 48 | +@ServiceRegistry( settings = @Setting( name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "3" ) ) |
| 49 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-16713" ) |
| 50 | +public class BatchRowCountWarningTest { |
| 51 | + private TriggerOnPrefixLogListener trigger; |
| 52 | + |
| 53 | + @BeforeAll |
| 54 | + public void setUp(SessionFactoryScope scope) { |
| 55 | + trigger = new TriggerOnPrefixLogListener( "HHH100001" ); |
| 56 | + LogInspectionHelper.registerListener( trigger, JdbcLogging.JDBC_MESSAGE_LOGGER ); |
| 57 | + scope.inTransaction( session -> { |
| 58 | + session.persist( new MyEntity( 1L, "Nicola", null ) ); |
| 59 | + session.persist( new MyEntity( 2L, "Stefano", "Ste" ) ); |
| 60 | + session.persist( new JoinTableEntity( 1L, new SpamEntity() ) ); |
| 61 | + } ); |
| 62 | + } |
| 63 | + |
| 64 | + @BeforeEach |
| 65 | + public void reset() { |
| 66 | + trigger.reset(); |
| 67 | + } |
| 68 | + |
| 69 | + @AfterAll |
| 70 | + public void tearDown(SessionFactoryScope scope) { |
| 71 | + LogInspectionHelper.clearAllListeners( JdbcLogging.JDBC_MESSAGE_LOGGER ); |
| 72 | + scope.inTransaction( session -> { |
| 73 | + session.createMutationQuery( "delete from BaseEntity" ).executeUpdate(); |
| 74 | + session.createQuery( "from JoinTableEntity", JoinTableEntity.class ) |
| 75 | + .getResultList() |
| 76 | + .forEach( session::remove ); |
| 77 | + } ); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testPersistBase(SessionFactoryScope scope) { |
| 82 | + scope.inTransaction( session -> session.persist( new BaseEntity( 10L ) ) ); |
| 83 | + scope.inTransaction( session -> assertThat( session.find( BaseEntity.class, 10L ) ).isNotNull() ); |
| 84 | + assertThat( trigger.wasTriggered() ).as( "Warning message was triggered" ).isFalse(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testPersistJoined(SessionFactoryScope scope) { |
| 89 | + scope.inTransaction( session -> session.persist( new MyEntity( 3L, "Vittorio", "Vitto" ) ) ); |
| 90 | + scope.inTransaction( session -> assertThat( session.find( MyEntity.class, 3L ) |
| 91 | + .getName() ).isEqualTo( "Vittorio" ) ); |
| 92 | + assertThat( trigger.wasTriggered() ).as( "Warning message was triggered" ).isFalse(); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testPersistJoinTable(SessionFactoryScope scope) { |
| 97 | + scope.inTransaction( session -> session.persist( new JoinTableEntity( 2L, new SpamEntity() ) ) ); |
| 98 | + scope.inTransaction( session -> assertThat( session.find( JoinTableEntity.class, 2L ) |
| 99 | + .getSpamEntity() ).isNotNull() ); |
| 100 | + assertThat( trigger.wasTriggered() ).as( "Warning message was triggered" ).isFalse(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testPersistMultipleJoined(SessionFactoryScope scope) { |
| 105 | + scope.inTransaction( session -> { |
| 106 | + session.persist( new MyEntity( 96L, "multi_1", null ) ); |
| 107 | + session.persist( new MyEntity( 97L, "multi_2", null ) ); |
| 108 | + session.persist( new MyEntity( 98L, "multi_3", null ) ); |
| 109 | + session.persist( new MyEntity( 99L, "multi_4", null ) ); |
| 110 | + } ); |
| 111 | + assertThat( trigger.wasTriggered() ).as( "Warning message was triggered" ).isFalse(); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testUpdateJoined(SessionFactoryScope scope) { |
| 116 | + scope.inTransaction( session -> { |
| 117 | + final MyEntity entity = session.find( MyEntity.class, 1L ); |
| 118 | + entity.setNickname( "Nico" ); |
| 119 | + } ); |
| 120 | + scope.inTransaction( session -> assertThat( session.find( MyEntity.class, 1L ) |
| 121 | + .getNickname() ).isEqualTo( "Nico" ) ); |
| 122 | + assertThat( trigger.wasTriggered() ).as( "Warning message was triggered" ).isFalse(); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testDeleteJoined(SessionFactoryScope scope) { |
| 127 | + scope.inTransaction( session -> { |
| 128 | + final MyEntity entity = session.find( MyEntity.class, 2L ); |
| 129 | + session.remove( entity ); |
| 130 | + } ); |
| 131 | + scope.inTransaction( session -> assertThat( session.find( MyEntity.class, 2L ) ).isNull() ); |
| 132 | + assertThat( trigger.wasTriggered() ).as( "Warning message was triggered" ).isFalse(); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testDeleteJoinTable(SessionFactoryScope scope) { |
| 137 | + scope.inTransaction( session -> { |
| 138 | + final JoinTableEntity entity = session.find( JoinTableEntity.class, 1L ); |
| 139 | + session.remove( entity ); |
| 140 | + } ); |
| 141 | + scope.inTransaction( session -> assertThat( session.find( JoinTableEntity.class, 1L ) ).isNull() ); |
| 142 | + assertThat( trigger.wasTriggered() ).as( "Warning message was triggered" ).isFalse(); |
| 143 | + } |
| 144 | + |
| 145 | + @Entity( name = "BaseEntity" ) |
| 146 | + @Inheritance( strategy = InheritanceType.JOINED ) |
| 147 | + public static class BaseEntity { |
| 148 | + @Id |
| 149 | + private Long id; |
| 150 | + |
| 151 | + public BaseEntity() { |
| 152 | + } |
| 153 | + |
| 154 | + public BaseEntity(Long id) { |
| 155 | + this.id = id; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Entity( name = "SubEntity" ) |
| 160 | + public static class SubEntity extends BaseEntity { |
| 161 | + private String name; |
| 162 | + |
| 163 | + public SubEntity() { |
| 164 | + } |
| 165 | + |
| 166 | + public SubEntity(Long id, String name) { |
| 167 | + super( id ); |
| 168 | + this.name = name; |
| 169 | + } |
| 170 | + |
| 171 | + public String getName() { |
| 172 | + return name; |
| 173 | + } |
| 174 | + |
| 175 | + public void setName(String name) { |
| 176 | + this.name = name; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + @Entity( name = "MyEntity" ) |
| 181 | + public static class MyEntity extends SubEntity { |
| 182 | + private String nickname; |
| 183 | + |
| 184 | + public MyEntity() { |
| 185 | + } |
| 186 | + |
| 187 | + public MyEntity(Long id, String name, String nickname) { |
| 188 | + super( id, name ); |
| 189 | + this.nickname = nickname; |
| 190 | + } |
| 191 | + |
| 192 | + public String getNickname() { |
| 193 | + return nickname; |
| 194 | + } |
| 195 | + |
| 196 | + public void setNickname(String nickname) { |
| 197 | + this.nickname = nickname; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @Entity( name = "SpamEntity" ) |
| 202 | + public static class SpamEntity { |
| 203 | + @Id |
| 204 | + @GeneratedValue |
| 205 | + private Long id; |
| 206 | + } |
| 207 | + |
| 208 | + @Entity( name = "JoinTableEntity" ) |
| 209 | + public static class JoinTableEntity { |
| 210 | + @Id |
| 211 | + private Long id; |
| 212 | + |
| 213 | + @ManyToOne( cascade = CascadeType.ALL ) |
| 214 | + @JoinTable( name = "foo_spam", joinColumns = @JoinColumn( name = "foo_id" ), inverseJoinColumns = @JoinColumn( name = "spam_id" ) ) |
| 215 | + private SpamEntity spamEntity; |
| 216 | + |
| 217 | + public JoinTableEntity() { |
| 218 | + |
| 219 | + } |
| 220 | + |
| 221 | + public JoinTableEntity(Long id, SpamEntity spam) { |
| 222 | + this.id = id; |
| 223 | + this.spamEntity = spam; |
| 224 | + } |
| 225 | + |
| 226 | + public SpamEntity getSpamEntity() { |
| 227 | + return spamEntity; |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments