|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.mapping.generated.sql; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import org.hibernate.annotations.Generated; |
| 10 | +import org.hibernate.annotations.Immutable; |
| 11 | +import org.hibernate.dialect.SybaseASEDialect; |
| 12 | +import org.hibernate.generator.EventType; |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.hibernate.testing.orm.junit.SkipForDialect; |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import java.math.BigDecimal; |
| 21 | +import java.time.LocalDateTime; |
| 22 | + |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | +import static org.junit.Assert.assertFalse; |
| 25 | +import static org.junit.Assert.assertNotNull; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Gavin King |
| 29 | + */ |
| 30 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 31 | +@DomainModel(annotatedClasses = ImmutableSqlGeneratedTest.OrderLine.class) |
| 32 | +@SessionFactory |
| 33 | +@SkipForDialect(dialectClass = SybaseASEDialect.class, |
| 34 | + reason = "The name 'current_timestamp' is illegal in this context. Only constants, constant expressions, or variables allowed here.") |
| 35 | +public class ImmutableSqlGeneratedTest { |
| 36 | + |
| 37 | + @Test |
| 38 | + public void test(SessionFactoryScope scope) { |
| 39 | + BigDecimal unitPrice = new BigDecimal("12.99"); |
| 40 | + scope.inTransaction( session -> { |
| 41 | + OrderLine entity = new OrderLine( unitPrice, 5 ); |
| 42 | + session.persist(entity); |
| 43 | + session.flush(); |
| 44 | + assertEquals( "new", entity.status ); |
| 45 | + assertEquals( unitPrice, entity.unitPrice ); |
| 46 | + assertEquals( 5, entity.quantity ); |
| 47 | + assertNotNull( entity.updated ); |
| 48 | + } ); |
| 49 | + scope.inTransaction( session -> { |
| 50 | + OrderLine entity = session.createQuery("from WithDefault", OrderLine.class ).getSingleResult(); |
| 51 | + assertEquals( unitPrice, entity.unitPrice ); |
| 52 | + assertEquals( 5, entity.quantity ); |
| 53 | + assertEquals( "new", entity.status ); |
| 54 | + assertNotNull( entity.updated ); |
| 55 | + entity.status = "old"; |
| 56 | + } ); |
| 57 | + scope.inTransaction( session -> { |
| 58 | + OrderLine entity = session.createQuery("from WithDefault", OrderLine.class ).getSingleResult(); |
| 59 | + assertEquals( unitPrice, entity.unitPrice ); |
| 60 | + assertEquals( 5, entity.quantity ); |
| 61 | + assertEquals( "new", entity.status ); |
| 62 | + assertNotNull( entity.updated ); |
| 63 | + LocalDateTime previous = entity.updated; |
| 64 | + entity.quantity = 10; |
| 65 | + entity.status = "old"; |
| 66 | + session.flush(); |
| 67 | + assertNotNull( entity.updated ); |
| 68 | + assertFalse( previous == entity.updated ); |
| 69 | + } ); |
| 70 | + scope.inTransaction( session -> { |
| 71 | + OrderLine entity = session.createQuery("from WithDefault", OrderLine.class ).getSingleResult(); |
| 72 | + assertEquals( unitPrice, entity.unitPrice ); |
| 73 | + assertEquals( 10, entity.quantity ); |
| 74 | + assertEquals( "new", entity.status ); |
| 75 | + assertNotNull( entity.updated ); |
| 76 | + } ); |
| 77 | + } |
| 78 | + |
| 79 | + @AfterEach |
| 80 | + public void dropTestData(SessionFactoryScope scope) { |
| 81 | + scope.inTransaction( session -> session.createQuery( "delete WithDefault" ).executeUpdate() ); |
| 82 | + } |
| 83 | + |
| 84 | + @Entity(name="WithDefault") |
| 85 | + public static class OrderLine { |
| 86 | + @Id |
| 87 | + private long id; |
| 88 | + private BigDecimal unitPrice; |
| 89 | + private int quantity = 1; |
| 90 | + @Immutable |
| 91 | + @Generated(sql = "'new'") |
| 92 | + private String status; |
| 93 | + @Generated(event = {EventType.INSERT, EventType.UPDATE}, |
| 94 | + sql = "current_timestamp") |
| 95 | + private LocalDateTime updated; |
| 96 | + |
| 97 | + |
| 98 | + public OrderLine() {} |
| 99 | + public OrderLine(BigDecimal unitPrice, int quantity) { |
| 100 | + this.unitPrice = unitPrice; |
| 101 | + this.quantity = quantity; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments