Skip to content

Commit ec49acf

Browse files
committed
use typesafe logging in EnhancementHelper
1 parent b2d3606 commit ec49acf

File tree

2 files changed

+69
-22
lines changed

2 files changed

+69
-22
lines changed

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/BytecodeInterceptorLogging.java

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.lang.invoke.MethodHandles;
1818

1919
import static org.jboss.logging.Logger.Level.TRACE;
20+
import static org.jboss.logging.Logger.Level.DEBUG;
2021
import static org.jboss.logging.Logger.Level.WARN;
2122

2223
/**
@@ -38,13 +39,66 @@ public interface BytecodeInterceptorLogging extends BasicLogger {
3839
@LogMessage(level = WARN)
3940
@Message(
4041
id = 90005901,
41-
value = "'%s.%s' was mapped with explicit lazy group '%s'. Hibernate will ignore the lazy group - this is generally " +
42-
"not a good idea for to-one associations as it leads to two separate SQL selects to initialize the association. " +
43-
"This is expected to be improved in future versions of Hibernate"
42+
value = "Ignoring explicit lazy group '%s' specified for association '%s.%s'"
43+
+ " (a lazy group for a to-one association would lead to two separate SELECTs to initialize the association)"
4444
)
45-
void lazyGroupIgnoredForToOne(String ownerName, String attributeName, String requestedLazyGroup);
45+
void lazyGroupIgnoredForToOne(String requestedLazyGroup, String ownerName, String attributeName);
4646

4747
@LogMessage(level = TRACE)
4848
@Message(id = 90005902, value = "Forcing initialization: %s.%s -> %s")
4949
void enhancementAsProxyLazinessForceInitialize(String entityName, Object identifier, String attributeName);
50+
51+
@LogMessage(level = WARN)
52+
@Message(
53+
id = 90005903,
54+
value = "Unable to commit JDBC transaction on temporary session used to load lazy collection associated to no session"
55+
)
56+
void unableToCommitTransactionOnTemporarySession();
57+
58+
@LogMessage(level = WARN)
59+
@Message(
60+
id = 90005904,
61+
value = "Unable to close temporary session used to load lazy collection associated to no session"
62+
)
63+
void unableToCloseTemporarySession();
64+
65+
// DEBUG messages (type-safe)
66+
67+
@LogMessage(level = DEBUG)
68+
@Message(
69+
id = 90005905,
70+
value = "To-one property '%s.%s' was mapped with LAZY + NO_PROXY but the class was not enhanced"
71+
)
72+
void toOneLazyNoProxyButNotEnhanced(String ownerName, String attributeName);
73+
74+
@LogMessage(level = DEBUG)
75+
@Message(
76+
id = 90005906,
77+
value = "'%s.%s' was mapped with LAZY and explicit NO_PROXY but the associated entity ('%s') has subclasses"
78+
)
79+
void lazyNoProxyButAssociatedHasSubclasses(String ownerName, String attributeName, String associatedEntityName);
80+
81+
@LogMessage(level = DEBUG)
82+
@Message(
83+
id = 90005907,
84+
value = "'%s.%s' specified NotFoundAction.IGNORE & LazyToOneOption.NO_PROXY;"
85+
+ " skipping foreign key selection to more efficiently handle NotFoundAction.IGNORE"
86+
)
87+
void notFoundIgnoreWithNoProxySkippingFkSelection(String ownerName, String attributeName);
88+
89+
@LogMessage(level = DEBUG)
90+
@Message(id = 90005908, value = "Enhancement interception started temporary Session")
91+
void enhancementHelperStartedTemporarySession();
92+
93+
@LogMessage(level = DEBUG)
94+
@Message(id = 90005909, value = "Enhancement interception starting transaction on temporary Session")
95+
void enhancementHelperStartingTransactionOnTemporarySession();
96+
97+
@LogMessage(level = DEBUG)
98+
@Message(id = 90005910, value = "Enhancement interception committing transaction on temporary Session")
99+
void enhancementHelperCommittingTransactionOnTemporarySession();
100+
101+
@LogMessage(level = DEBUG)
102+
@Message(id = 90005911, value = "Enhancement interception closing temporary Session")
103+
void enhancementHelperClosingTemporarySession();
50104
}

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/EnhancementHelper.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public static boolean includeInBaseFetchGroup(
4141
if ( ! isEnhanced ) {
4242
if ( value instanceof ToOne toOne ) {
4343
if ( toOne.isUnwrapProxy() ) {
44-
BYTECODE_INTERCEPTOR_LOGGER.debugf(
45-
"To-one property `%s#%s` was mapped with LAZY + NO_PROXY but the class was not enhanced",
44+
BYTECODE_INTERCEPTOR_LOGGER.toOneLazyNoProxyButNotEnhanced(
4645
bootMapping.getPersistentClass().getEntityName(),
4746
bootMapping.getName()
4847
);
@@ -73,9 +72,9 @@ public static boolean includeInBaseFetchGroup(
7372
// we simply log a message that we are ignoring the `@LazyGroup` for to-ones
7473

7574
BYTECODE_INTERCEPTOR_LOGGER.lazyGroupIgnoredForToOne(
75+
bootMapping.getLazyGroup(),
7676
bootMapping.getPersistentClass().getEntityName(),
77-
bootMapping.getName(),
78-
bootMapping.getLazyGroup()
77+
bootMapping.getName()
7978
);
8079

8180
// at a later time - for example 6.0 when we can implement the join solution
@@ -105,8 +104,7 @@ public static boolean includeInBaseFetchGroup(
105104
// the associated type has subclasses - we cannot use the enhanced proxy and will generate a HibernateProxy
106105
if ( unwrapExplicitlyRequested ) {
107106
// NO_PROXY was explicitly requested
108-
BytecodeInterceptorLogging.LOGGER.debugf(
109-
"`%s#%s` was mapped with LAZY and explicit NO_PROXY but the associated entity (`%s`) has subclasses",
107+
BYTECODE_INTERCEPTOR_LOGGER.lazyNoProxyButAssociatedHasSubclasses(
110108
bootMapping.getPersistentClass().getEntityName(),
111109
bootMapping.getName(),
112110
toOne.getReferencedEntityName()
@@ -118,9 +116,7 @@ public static boolean includeInBaseFetchGroup(
118116

119117
if ( toOne instanceof ManyToOne manyToOne && manyToOne.isIgnoreNotFound() ) {
120118
if ( unwrapExplicitlyRequested ) {
121-
BytecodeInterceptorLogging.LOGGER.debugf(
122-
"%s#%s specified NotFoundAction.IGNORE & LazyToOneOption.NO_PROXY; " +
123-
"skipping FK selection to more efficiently handle NotFoundAction.IGNORE",
119+
BYTECODE_INTERCEPTOR_LOGGER.notFoundIgnoreWithNoProxySkippingFkSelection(
124120
bootMapping.getPersistentClass().getEntityName(),
125121
bootMapping.getName()
126122
);
@@ -186,7 +182,7 @@ else if ( !session.isConnected() ) {
186182

187183
// If we are using a temporary Session, begin a transaction if necessary
188184
if ( isTempSession ) {
189-
BytecodeInterceptorLogging.LOGGER.debug( "Enhancement interception Helper#performWork started temporary Session" );
185+
BYTECODE_INTERCEPTOR_LOGGER.enhancementHelperStartedTemporarySession();
190186

191187
isJta = session.getTransactionCoordinator().getTransactionCoordinatorBuilder().isJta();
192188

@@ -196,7 +192,7 @@ else if ( !session.isConnected() ) {
196192
// be created even if a current session and transaction are
197193
// open (ex: session.clear() was used). We must prevent
198194
// multiple transactions.
199-
BytecodeInterceptorLogging.LOGGER.debug( "Enhancement interception Helper#performWork starting transaction on temporary Session" );
195+
BYTECODE_INTERCEPTOR_LOGGER.enhancementHelperStartingTransactionOnTemporarySession();
200196
session.beginTransaction();
201197
}
202198
}
@@ -213,24 +209,21 @@ else if ( !session.isConnected() ) {
213209
try {
214210
// Commit the JDBC transaction if we started one.
215211
if ( !isJta ) {
216-
BytecodeInterceptorLogging.LOGGER.debug( "Enhancement interception Helper#performWork committing transaction on temporary Session" );
212+
BYTECODE_INTERCEPTOR_LOGGER.enhancementHelperCommittingTransactionOnTemporarySession();
217213
session.getTransaction().commit();
218214
}
219215
}
220216
catch (Exception e) {
221-
BytecodeInterceptorLogging.LOGGER.warn(
222-
"Unable to commit JDBC transaction on temporary session used to load lazy " +
223-
"collection associated to no session"
224-
);
217+
BYTECODE_INTERCEPTOR_LOGGER.unableToCommitTransactionOnTemporarySession();
225218
}
226219

227220
// Close the just opened temp Session
228221
try {
229-
BytecodeInterceptorLogging.LOGGER.debug( "Enhancement interception Helper#performWork closing temporary Session" );
222+
BYTECODE_INTERCEPTOR_LOGGER.enhancementHelperClosingTemporarySession();
230223
session.close();
231224
}
232225
catch (Exception e) {
233-
BytecodeInterceptorLogging.LOGGER.warn( "Unable to close temporary session used to load lazy collection associated to no session" );
226+
BYTECODE_INTERCEPTOR_LOGGER.unableToCloseTemporarySession();
234227
}
235228
}
236229
}

0 commit comments

Comments
 (0)