Skip to content

Commit 9874ccd

Browse files
marko-bekhtayrodiere
authored andcommitted
HHH-19495 Remove the default "true" from optimistic-lock
so that no extra annotations are added unless explicitly defined.
1 parent 283955a commit 9874ccd

File tree

7 files changed

+218
-17
lines changed

7 files changed

+218
-17
lines changed

hibernate-core/src/main/java/org/hibernate/boot/jaxb/mapping/spi/JaxbLockableAttribute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
* @author Steve Ebersole
1111
*/
1212
public interface JaxbLockableAttribute extends JaxbPersistentAttribute {
13-
boolean isOptimisticLock();
13+
Boolean isOptimisticLock();
1414
void setOptimisticLock(Boolean value);
1515
}

hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/attr/CommonAttributeProcessing.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,15 @@ public static void applyOptimisticLock(
8484
JaxbLockableAttribute jaxbAttribute,
8585
MutableMemberDetails memberDetails,
8686
XmlDocumentContext xmlDocumentContext) {
87-
final boolean includeInOptimisticLock = jaxbAttribute.isOptimisticLock();
88-
final OptimisticLockAnnotation optLockAnn = (OptimisticLockAnnotation) memberDetails.applyAnnotationUsage(
89-
HibernateAnnotations.OPTIMISTIC_LOCK,
90-
xmlDocumentContext.getModelBuildingContext()
91-
);
92-
optLockAnn.excluded( !includeInOptimisticLock );
87+
final Boolean includeInOptimisticLock = jaxbAttribute.isOptimisticLock();
88+
89+
if ( includeInOptimisticLock != null ) {
90+
final OptimisticLockAnnotation optLockAnn = (OptimisticLockAnnotation) memberDetails.applyAnnotationUsage(
91+
HibernateAnnotations.OPTIMISTIC_LOCK,
92+
xmlDocumentContext.getModelBuildingContext()
93+
);
94+
optLockAnn.excluded( !includeInOptimisticLock );
95+
}
9396
}
9497

9598
public static void applyFetching(

hibernate-core/src/main/resources/org/hibernate/xsd/mapping/mapping-7.0.xsd

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@
601601
<xsd:attribute name="access" type="orm:access-type"/>
602602
<!-- hbm: Hibernate's pluggable accessor spi -->
603603
<xsd:attribute name="attribute-accessor" type="xsd:string" />
604-
<xsd:attribute name="optimistic-lock" type="xsd:boolean" default="true" />
604+
<xsd:attribute name="optimistic-lock" type="xsd:boolean" />
605605
</xsd:complexType>
606606

607607
<xsd:group name="basic-type-group">
@@ -1027,7 +1027,7 @@
10271027
<xsd:attribute name="fetch-mode" type="orm:plural-fetch-mode"/>
10281028
<xsd:attribute name="access" type="orm:access-type"/>
10291029
<xsd:attribute name="attribute-accessor" type="xsd:string" />
1030-
<xsd:attribute name="optimistic-lock" type="xsd:boolean" default="true" />
1030+
<xsd:attribute name="optimistic-lock" type="xsd:boolean" />
10311031
<xsd:attribute name="classification" type="orm:limited-collection-classification-enum" />
10321032
</xsd:complexType>
10331033

@@ -1109,7 +1109,7 @@
11091109
<xsd:attribute name="name" type="xsd:string" use="required"/>
11101110
<xsd:attribute name="access" type="orm:access-type"/>
11111111
<xsd:attribute name="attribute-accessor" type="xsd:string" />
1112-
<xsd:attribute name="optimistic-lock" type="xsd:boolean" default="true" />
1112+
<xsd:attribute name="optimistic-lock" type="xsd:boolean" />
11131113
</xsd:complexType>
11141114

11151115
<!-- **************************************************** -->
@@ -1614,7 +1614,7 @@
16141614
<xsd:attribute name="orphan-removal" type="xsd:boolean"/>
16151615
<xsd:attribute name="classification" type="orm:limited-collection-classification-enum" />
16161616
<xsd:attribute name="not-found" type="orm:not-found-enum"/>
1617-
<xsd:attribute name="optimistic-lock" type="xsd:boolean" default="true" />
1617+
<xsd:attribute name="optimistic-lock" type="xsd:boolean" />
16181618
</xsd:complexType>
16191619

16201620
<!-- **************************************************** -->
@@ -1661,7 +1661,7 @@
16611661
<xsd:attribute name="optional" type="xsd:boolean"/>
16621662
<xsd:attribute name="access" type="orm:access-type"/>
16631663
<xsd:attribute name="attribute-accessor" type="xsd:string" />
1664-
<xsd:attribute name="optimistic-lock" type="xsd:boolean" default="true" />
1664+
<xsd:attribute name="optimistic-lock" type="xsd:boolean" />
16651665
<xsd:attribute name="maps-id" type="xsd:string"/>
16661666
<xsd:attribute name="id" type="xsd:boolean"/>
16671667
<xsd:attribute name="not-found" type="orm:not-found-enum"/>
@@ -2057,7 +2057,7 @@
20572057
<xsd:attribute name="orphan-removal" type="xsd:boolean"/>
20582058
<xsd:attribute name="classification" type="orm:limited-collection-classification-enum" />
20592059
<xsd:attribute name="not-found" type="orm:not-found-enum"/>
2060-
<xsd:attribute name="optimistic-lock" type="xsd:boolean" default="true" />
2060+
<xsd:attribute name="optimistic-lock" type="xsd:boolean" />
20612061
</xsd:complexType>
20622062

20632063
<!-- **************************************************** -->
@@ -2113,7 +2113,7 @@
21132113
<xsd:attribute name="optional" type="xsd:boolean"/>
21142114
<xsd:attribute name="access" type="orm:access-type"/>
21152115
<xsd:attribute name="attribute-accessor" type="xsd:string" />
2116-
<xsd:attribute name="optimistic-lock" type="xsd:boolean" default="true" />
2116+
<xsd:attribute name="optimistic-lock" type="xsd:boolean" />
21172117
<xsd:attribute name="mapped-by" type="xsd:string"/>
21182118
<xsd:attribute name="orphan-removal" type="xsd:boolean"/>
21192119
<xsd:attribute name="maps-id" type="xsd:string"/>
@@ -3139,7 +3139,7 @@
31393139
<xsd:attribute name="fetch" type="orm:fetch-type"/>
31403140
<xsd:attribute name="fetch-mode" type="orm:singular-fetch-mode"/>
31413141
<xsd:attribute name="optional" type="xsd:boolean"/>
3142-
<xsd:attribute name="optimistic-lock" type="xsd:boolean" default="true" />
3142+
<xsd:attribute name="optimistic-lock" type="xsd:boolean" />
31433143
</xsd:complexType>
31443144

31453145
<xsd:complexType name="any-discriminator">
@@ -3208,7 +3208,7 @@
32083208
<xsd:attribute name="fetch-mode" type="orm:plural-fetch-mode"/>
32093209
<xsd:attribute name="access" type="orm:access-type"/>
32103210
<xsd:attribute name="attribute-accessor" type="xsd:string" />
3211-
<xsd:attribute name="optimistic-lock" type="xsd:boolean" default="true" />
3211+
<xsd:attribute name="optimistic-lock" type="xsd:boolean" />
32123212
<xsd:attribute name="classification" type="orm:limited-collection-classification-enum" />
32133213
</xsd:complexType>
32143214

@@ -3532,4 +3532,4 @@
35323532
<xsd:attribute name="type" use="required" type="xsd:string"/>
35333533
</xsd:complexType>
35343534

3535-
</xsd:schema>
3535+
</xsd:schema>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.jpa.xml;
6+
7+
import jakarta.persistence.Id;
8+
import jakarta.persistence.Version;
9+
10+
import java.util.List;
11+
12+
public class Consumer {
13+
14+
@Id
15+
private int id;
16+
17+
private List<ConsumerItem> consumerItems;
18+
19+
@Version
20+
private int version;
21+
22+
public int getId() {
23+
return id;
24+
}
25+
26+
public void setId(int id) {
27+
this.id = id;
28+
}
29+
30+
public List<ConsumerItem> getConsumerItems() {
31+
return consumerItems;
32+
}
33+
34+
public void setConsumerItems(List<ConsumerItem> consumerItems) {
35+
this.consumerItems = consumerItems;
36+
}
37+
38+
public int getVersion() {
39+
return version;
40+
}
41+
42+
public void setVersion(int version) {
43+
this.version = version;
44+
}
45+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.jpa.xml;
6+
7+
import jakarta.persistence.Version;
8+
9+
public class ConsumerItem {
10+
11+
private int id;
12+
13+
private Consumer consumer;
14+
15+
@Version
16+
private int version;
17+
18+
public int getId() {
19+
return id;
20+
}
21+
22+
public void setId(int id) {
23+
this.id = id;
24+
}
25+
26+
public Consumer getConsumer() {
27+
return consumer;
28+
}
29+
30+
public void setConsumer(Consumer consumer) {
31+
this.consumer = consumer;
32+
}
33+
34+
public int getVersion() {
35+
return version;
36+
}
37+
38+
public void setVersion(int version) {
39+
this.version = version;
40+
}
41+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.jpa.xml;
6+
7+
import org.hibernate.testing.jdbc.SQLStatementInspector;
8+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
9+
import org.hibernate.testing.orm.junit.JiraKey;
10+
import org.hibernate.testing.orm.junit.Jpa;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
15+
@JiraKey(value = "HHH-19495")
16+
@Jpa(
17+
xmlMappings = {"org/hibernate/orm/test/jpa/xml/NoDefaultOptimisticLockAnnotationTest.xml"},
18+
annotatedClasses = {Consumer.class, ConsumerItem.class},
19+
useCollectingStatementInspector = true
20+
)
21+
class NoDefaultOptimisticLockAnnotationTest {
22+
23+
private static SQLStatementInspector statementInspector;
24+
private static int consumerId;
25+
26+
27+
@BeforeEach
28+
public void setUp(EntityManagerFactoryScope scope) {
29+
statementInspector = scope.getCollectingStatementInspector();
30+
31+
scope.inTransaction( em -> {
32+
Consumer consumer = new Consumer();
33+
em.persist( consumer );
34+
consumerId = consumer.getId();
35+
36+
ConsumerItem item1 = new ConsumerItem();
37+
item1.setConsumer( consumer );
38+
em.persist( item1 );
39+
40+
ConsumerItem item2 = new ConsumerItem();
41+
item2.setConsumer( consumer );
42+
em.persist( item2 );
43+
} );
44+
}
45+
46+
@Test
47+
void test(EntityManagerFactoryScope scope) {
48+
statementInspector.clear();
49+
50+
scope.inTransaction( em -> {
51+
Consumer consumer = em.find( Consumer.class, consumerId );
52+
ConsumerItem inventory = new ConsumerItem();
53+
inventory.setConsumer( consumer );
54+
consumer.getConsumerItems().add( inventory );
55+
} );
56+
57+
statementInspector.assertIsInsert( 1 );
58+
statementInspector.assertNoUpdate();
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ SPDX-License-Identifier: Apache-2.0
4+
~ Copyright Red Hat Inc. and Hibernate Authors
5+
-->
6+
<entity-mappings xmlns="https://jakarta.ee/xml/ns/persistence/orm"
7+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
8+
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence/orm https://jakarta.ee/xml/ns/persistence/orm/orm_3_2.xsd"
9+
version="3.2">
10+
11+
<package>org.hibernate.orm.test.jpa.xml</package>
12+
13+
<entity class="Consumer" name="Consumer">
14+
<table name="consumer"/>
15+
<attributes>
16+
<id name="id">
17+
<column name="id_entity"/>
18+
<generated-value strategy="AUTO"/>
19+
</id>
20+
<version name="version">
21+
<column name="version"/>
22+
</version>
23+
<one-to-many name="consumerItems" mapped-by="consumer" fetch="LAZY">
24+
<cascade>
25+
<cascade-all/>
26+
</cascade>
27+
</one-to-many>
28+
</attributes>
29+
</entity>
30+
31+
<entity class="ConsumerItem" name="ConsumerInventory">
32+
<table name="consumer_item"/>
33+
<attributes>
34+
<id name="id">
35+
<column name="id_entity"/>
36+
<generated-value strategy="AUTO"/>
37+
</id>
38+
<version name="version">
39+
<column name="version"/>
40+
</version>
41+
<many-to-one name="consumer" target-entity="Consumer">
42+
<join-column name="consumer_id"/>
43+
<cascade>
44+
<cascade-merge/>
45+
</cascade>
46+
</many-to-one>
47+
</attributes>
48+
</entity>
49+
50+
</entity-mappings>
51+
52+

0 commit comments

Comments
 (0)