Skip to content

Commit 731a5db

Browse files
committed
HHH-18815 clarify and test semantics of @generated and @immutable
Signed-off-by: Gavin King <[email protected]>
1 parent 00f6115 commit 731a5db

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

hibernate-core/src/main/java/org/hibernate/annotations/Generated.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,25 @@
4848
* {@link GeneratedColumn} annotation, so that Hibernate automatically
4949
* generates the correct DDL.
5050
* </ul>
51+
* <p>
52+
* A {@code @Generated} field may be generated on
53+
* {@linkplain EventType#INSERT inserts}, on
54+
* {@linkplain EventType#UPDATE updates}, or on both inserts and updates,
55+
* as specified by the {@link #event} member.
56+
* By default, {@code @Generated} fields are not immutable, and so a field
57+
* which is generated on insert may later be explicitly assigned a new value
58+
* by the application program, resulting in its value being updated in the
59+
* database. If this is not desired, the {@link Immutable @Immutable}
60+
* annotation may be used in conjunction with {@code @Generated} to specify
61+
* that the field may never be updated after initial generation of its value.
5162
*
5263
* @author Emmanuel Bernard
5364
*
5465
* @see jakarta.persistence.GeneratedValue
5566
* @see ColumnDefault
5667
* @see GeneratedColumn
68+
* @see Formula
69+
* @see Immutable
5770
*/
5871
@ValueGenerationType( generatedBy = GeneratedGeneration.class )
5972
@IdGeneratorType( GeneratedGeneration.class )
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/sql/SqlGeneratedTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public void test(SessionFactoryScope scope) {
5252
assertEquals( "new", entity.status );
5353
assertNotNull( entity.updated );
5454
entity.status = "old";
55-
session.flush();
5655
} );
5756
scope.inTransaction( session -> {
5857
OrderLine entity = session.createQuery("from WithDefault", OrderLine.class ).getSingleResult();

0 commit comments

Comments
 (0)