Skip to content

Commit 9cda810

Browse files
committed
HHH-19975 validate LoadEvent when it is reused
1 parent fee7ed7 commit 9cda810

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

hibernate-core/src/main/java/org/hibernate/event/spi/LoadEvent.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.event.spi;
66

7+
import org.hibernate.Internal;
78
import org.hibernate.LockMode;
89
import org.hibernate.LockOptions;
910
import org.hibernate.Locking;
@@ -66,32 +67,36 @@ private LoadEvent(
6667
boolean isAssociationFetch,
6768
EventSource source,
6869
Boolean readOnly) {
69-
7070
super( source );
71+
this.entityId = entityId;
72+
this.entityClassName = entityClassName;
73+
this.instanceToLoad = instanceToLoad;
74+
this.lockOptions = lockOptions;
75+
this.isAssociationFetch = isAssociationFetch;
76+
this.readOnly = readOnly;
77+
validate();
78+
}
7179

80+
@Internal
81+
public void validate() {
7282
if ( entityId == null ) {
73-
throw new IllegalArgumentException( "id to load is required for loading" );
83+
throw new IllegalArgumentException( "Identifier may not be null" );
7484
}
7585

76-
if ( lockOptions.getLockMode() == LockMode.WRITE ) {
77-
throw new IllegalArgumentException("Invalid lock mode for loading");
86+
final var lockMode = lockOptions.getLockMode();
87+
if ( lockMode == LockMode.WRITE ) {
88+
throw new IllegalArgumentException( "Invalid lock mode: " + LockMode.WRITE );
7889
}
79-
else if ( lockOptions.getLockMode() == null ) {
90+
else if ( lockMode == null ) {
8091
lockOptions.setLockMode( LockMode.NONE );
8192
}
82-
83-
this.entityId = entityId;
84-
this.entityClassName = entityClassName;
85-
this.instanceToLoad = instanceToLoad;
86-
this.lockOptions = lockOptions;
87-
this.isAssociationFetch = isAssociationFetch;
88-
this.readOnly = readOnly;
8993
}
9094

9195
public Object getEntityId() {
9296
return entityId;
9397
}
9498

99+
@Internal
95100
public void setEntityId(Object entityId) {
96101
this.entityId = entityId;
97102
}
@@ -100,6 +105,7 @@ public String getEntityClassName() {
100105
return entityClassName;
101106
}
102107

108+
@Internal
103109
public void setEntityClassName(String entityClassName) {
104110
this.entityClassName = entityClassName;
105111
}
@@ -108,6 +114,7 @@ public boolean isAssociationFetch() {
108114
return isAssociationFetch;
109115
}
110116

117+
@Internal
111118
public void setAssociationFetch(boolean associationFetch) {
112119
isAssociationFetch = associationFetch;
113120
}
@@ -116,6 +123,7 @@ public Object getInstanceToLoad() {
116123
return instanceToLoad;
117124
}
118125

126+
@Internal
119127
public void setInstanceToLoad(Object instanceToLoad) {
120128
this.instanceToLoad = instanceToLoad;
121129
}
@@ -124,6 +132,7 @@ public LockOptions getLockOptions() {
124132
return lockOptions;
125133
}
126134

135+
@Internal
127136
public void setLockOptions(LockOptions lockOptions) {
128137
this.lockOptions = lockOptions;
129138
}
@@ -140,12 +149,11 @@ public Boolean getReadOnly() {
140149
return readOnly;
141150
}
142151

152+
@Internal
143153
public void setReadOnly(Boolean readOnly) {
144154
this.readOnly = readOnly;
145155
}
146156

147-
148-
149157
/**
150158
* @deprecated Use {@linkplain #getLockOptions()} instead.
151159
*/

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,7 @@ protected LoadEvent makeLoadEvent(String entityName, Object id, Boolean readOnly
11281128
event.setReadOnly( readOnly );
11291129
event.setLockOptions( lockOptions );
11301130
event.setAssociationFetch( false );
1131+
event.validate();
11311132
return event;
11321133
}
11331134
}

0 commit comments

Comments
 (0)