|
| 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.ops; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.hibernate.annotations.OnDelete; |
| 12 | +import org.hibernate.annotations.OnDeleteAction; |
| 13 | +import org.hibernate.query.Query; |
| 14 | + |
| 15 | +import org.hibernate.testing.TestForIssue; |
| 16 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import jakarta.persistence.Column; |
| 24 | +import jakarta.persistence.Entity; |
| 25 | +import jakarta.persistence.EnumType; |
| 26 | +import jakarta.persistence.Enumerated; |
| 27 | +import jakarta.persistence.FetchType; |
| 28 | +import jakarta.persistence.Id; |
| 29 | +import jakarta.persistence.JoinColumn; |
| 30 | +import jakarta.persistence.ManyToOne; |
| 31 | +import jakarta.persistence.Table; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | + |
| 35 | +@DomainModel( |
| 36 | + annotatedClasses = { |
| 37 | + EnumsParameterTest.Event.class, |
| 38 | + EnumsParameterTest.Organizer.class, |
| 39 | + EnumsParameterTest.Customer.class, |
| 40 | + } |
| 41 | +) |
| 42 | +@SessionFactory |
| 43 | +@TestForIssue(jiraKey = "HHH-15498") |
| 44 | +public class EnumsParameterTest { |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + public void setUp(SessionFactoryScope scope) { |
| 48 | + scope.inTransaction( |
| 49 | + session -> { |
| 50 | + Organizer organizer = new Organizer( 1L, "Test Organizer" ); |
| 51 | + Event event = new Event( 1L, "Test Event", organizer, Type.INDOOR ); |
| 52 | + Event nullEvent = new Event( 2L, "Null Event", null, Type.OUTDOOR ); |
| 53 | + session.persist( organizer ); |
| 54 | + session.persist( event ); |
| 55 | + session.persist( nullEvent ); |
| 56 | + } |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + @AfterEach |
| 61 | + public void tearDown(SessionFactoryScope scope) { |
| 62 | + scope.inTransaction( |
| 63 | + session -> { |
| 64 | + session.createMutationQuery( "delete from Event" ).executeUpdate(); |
| 65 | + session.createMutationQuery( "delete from Organizer" ).executeUpdate(); |
| 66 | + } |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testDeleteByEventType(SessionFactoryScope scope) { |
| 72 | + scope.inTransaction( |
| 73 | + session -> { |
| 74 | + session.createMutationQuery( "DELETE FROM Event WHERE (:type IS NULL OR type = :type)" ) |
| 75 | + .setParameter( "type", Type.INDOOR ) |
| 76 | + .executeUpdate(); |
| 77 | + } |
| 78 | + ); |
| 79 | + |
| 80 | + scope.inTransaction( |
| 81 | + session -> { |
| 82 | + List<Event> events = session.createQuery( "select e FROM Event e", Event.class ) |
| 83 | + .list(); |
| 84 | + assertThat( events.size() ).isEqualTo( 1 ); |
| 85 | + } |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testDelete(SessionFactoryScope scope) { |
| 91 | + scope.inTransaction( |
| 92 | + session -> { |
| 93 | + session.createMutationQuery( |
| 94 | + "DELETE FROM Event WHERE (cast(:type as String) IS NULL OR type = :type)" ) |
| 95 | + .setParameter( "type", null ) |
| 96 | + .executeUpdate(); |
| 97 | + } |
| 98 | + ); |
| 99 | + |
| 100 | + scope.inTransaction( |
| 101 | + session -> { |
| 102 | + List<Event> events = session.createQuery( "select e FROM Event e", Event.class ) |
| 103 | + .list(); |
| 104 | + assertThat( events.size() ).isEqualTo( 0 ); |
| 105 | + } |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testSelect(SessionFactoryScope scope) { |
| 111 | + scope.inTransaction( |
| 112 | + session -> { |
| 113 | + Query query = session.createQuery( |
| 114 | + "SELECT c FROM Customer c WHERE (:phoneType IS NULL OR c.type = :phoneType)", |
| 115 | + Customer.class |
| 116 | + ); |
| 117 | + query.setParameter( "phoneType", Customer.PhoneType.LAND_LINE ); |
| 118 | + List<Customer> customerList = query.getResultList(); |
| 119 | + } |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + @Entity(name = "Organizer") |
| 124 | + @Table(name = "ORGANIZER_TABLE") |
| 125 | + public static class Organizer { |
| 126 | + @Id |
| 127 | + private Long id; |
| 128 | + |
| 129 | + private String name; |
| 130 | + |
| 131 | + public Organizer() { |
| 132 | + } |
| 133 | + |
| 134 | + public Organizer(Long id, String name) { |
| 135 | + this.id = id; |
| 136 | + this.name = name; |
| 137 | + } |
| 138 | + |
| 139 | + public Long getId() { |
| 140 | + return id; |
| 141 | + } |
| 142 | + |
| 143 | + public void setId(Long id) { |
| 144 | + this.id = id; |
| 145 | + } |
| 146 | + |
| 147 | + public String getName() { |
| 148 | + return name; |
| 149 | + } |
| 150 | + |
| 151 | + public void setName(String name) { |
| 152 | + this.name = name; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Entity(name = "Event") |
| 157 | + @Table(name = "EVENT_CLASS") |
| 158 | + public static class Event { |
| 159 | + @Id |
| 160 | + private Long id; |
| 161 | + |
| 162 | + private String name; |
| 163 | + |
| 164 | + @Column(name = "TYPE_COLUMN", nullable = false) |
| 165 | + @Enumerated(EnumType.STRING) |
| 166 | + private Type type; |
| 167 | + |
| 168 | + @ManyToOne(fetch = FetchType.LAZY) |
| 169 | + @OnDelete(action = OnDeleteAction.CASCADE) |
| 170 | + @JoinColumn(name = "OrganizerId", referencedColumnName = "Id") |
| 171 | + private Organizer organizer; |
| 172 | + |
| 173 | + public Event() { |
| 174 | + } |
| 175 | + |
| 176 | + public Event(Long id, String name, Organizer organizer, Type type) { |
| 177 | + this.id = id; |
| 178 | + this.name = name; |
| 179 | + this.organizer = organizer; |
| 180 | + this.type = type; |
| 181 | + } |
| 182 | + |
| 183 | + public Long getId() { |
| 184 | + return id; |
| 185 | + } |
| 186 | + |
| 187 | + public void setId(Long id) { |
| 188 | + this.id = id; |
| 189 | + } |
| 190 | + |
| 191 | + public String getName() { |
| 192 | + return name; |
| 193 | + } |
| 194 | + |
| 195 | + public void setName(String name) { |
| 196 | + this.name = name; |
| 197 | + } |
| 198 | + |
| 199 | + public Organizer getOrganizer() { |
| 200 | + return organizer; |
| 201 | + } |
| 202 | + |
| 203 | + public void setOrganizer(Organizer organizer) { |
| 204 | + this.organizer = organizer; |
| 205 | + } |
| 206 | + |
| 207 | + public Type getType() { |
| 208 | + return type; |
| 209 | + } |
| 210 | + |
| 211 | + public Event setType(Type type) { |
| 212 | + this.type = type; |
| 213 | + return this; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + enum Type { |
| 218 | + INDOOR, OUTDOOR |
| 219 | + } |
| 220 | + |
| 221 | + @Entity(name = "Customer") |
| 222 | + @Table(name = "CUSTOMER") |
| 223 | + public static class Customer { |
| 224 | + |
| 225 | + @Id |
| 226 | + @Column(name = "id") |
| 227 | + int id; |
| 228 | + |
| 229 | + @Enumerated(EnumType.STRING) |
| 230 | + @Column(name = "type") |
| 231 | + private PhoneType type; |
| 232 | + |
| 233 | + public int getId() { |
| 234 | + return id; |
| 235 | + } |
| 236 | + |
| 237 | + public void setId(int id) { |
| 238 | + this.id = id; |
| 239 | + } |
| 240 | + |
| 241 | + public PhoneType getType() { |
| 242 | + return type; |
| 243 | + } |
| 244 | + |
| 245 | + public void setType(PhoneType type) { |
| 246 | + this.type = type; |
| 247 | + } |
| 248 | + |
| 249 | + public enum PhoneType { |
| 250 | + MOBILE, |
| 251 | + LAND_LINE; |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | +} |
0 commit comments