Skip to content

Commit 73f9df1

Browse files
committed
HHH-7736 - simple improvement and test fixing
1 parent 46a34a4 commit 73f9df1

File tree

7 files changed

+297
-166
lines changed

7 files changed

+297
-166
lines changed

hibernate-core/src/main/java/org/hibernate/LockOptions.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class LockOptions implements Serializable {
6565

6666
private LockMode lockMode = LockMode.NONE;
6767
private int timeout = WAIT_FOREVER;
68-
private Map aliasSpecificLockModes = null; //initialize lazily as LockOptions is frequently created without needing this
68+
private Map<String, LockMode> aliasSpecificLockModes = null; //initialize lazily as LockOptions is frequently created without needing this
6969

7070
public LockOptions() {
7171
}
@@ -114,7 +114,7 @@ public LockOptions setLockMode(LockMode lockMode) {
114114
*/
115115
public LockOptions setAliasSpecificLockMode(String alias, LockMode lockMode) {
116116
if ( aliasSpecificLockModes == null ) {
117-
aliasSpecificLockModes = new HashMap();
117+
aliasSpecificLockModes = new HashMap<String, LockMode>();
118118
}
119119
aliasSpecificLockModes.put( alias, lockMode );
120120
return this;
@@ -135,7 +135,7 @@ public LockMode getAliasSpecificLockMode(String alias) {
135135
if ( aliasSpecificLockModes == null ) {
136136
return null;
137137
}
138-
return (LockMode) aliasSpecificLockModes.get( alias );
138+
return aliasSpecificLockModes.get( alias );
139139
}
140140

141141
/**
@@ -176,9 +176,9 @@ public int getAliasLockCount() {
176176
*
177177
* @return Iterator for accessing the Map.Entry's
178178
*/
179-
public Iterator getAliasLockIterator() {
179+
public Iterator<Map.Entry<String, LockMode>> getAliasLockIterator() {
180180
if ( aliasSpecificLockModes == null ) {
181-
return Collections.emptyList().iterator();
181+
return Collections.<String, LockMode>emptyMap().entrySet().iterator();
182182
}
183183
return aliasSpecificLockModes.entrySet().iterator();
184184
}
@@ -256,7 +256,7 @@ public static LockOptions copy(LockOptions from, LockOptions dest) {
256256
dest.setScope(from.getScope());
257257
dest.setTimeOut(from.getTimeOut());
258258
if ( from.aliasSpecificLockModes != null ) {
259-
dest.aliasSpecificLockModes = new HashMap( from.aliasSpecificLockModes );
259+
dest.aliasSpecificLockModes = new HashMap<String, LockMode>( from.aliasSpecificLockModes );
260260
}
261261
return dest;
262262
}

hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/entity/EntityClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ private void processHibernateEntitySpecificAnnotations() {
388388
// Custom persister
389389
String entityPersisterClass = null;
390390
final AnnotationInstance persisterAnnotation = JandexHelper.getSingleAnnotation(
391-
getClassInfo(), HibernateDotNames.PERSISTER
391+
getClassInfo(), HibernateDotNames.PERSISTER, ClassInfo.class
392392
);
393393
if ( persisterAnnotation != null && persisterAnnotation.value( "impl" ) != null ) {
394394
entityPersisterClass = persisterAnnotation.value( "impl" ).asString();

hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java

Lines changed: 37 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
import org.hibernate.internal.FilterConfiguration;
8888
import org.hibernate.internal.FilterHelper;
8989
import org.hibernate.internal.util.StringHelper;
90+
import org.hibernate.internal.util.ValueHolder;
9091
import org.hibernate.internal.util.collections.ArrayHelper;
9192
import org.hibernate.jdbc.Expectation;
9293
import org.hibernate.jdbc.Expectations;
@@ -199,6 +200,7 @@ public abstract class AbstractEntityPersister
199200
//information about lazy properties of this class
200201
private final String[] lazyPropertyNames;
201202
private final int[] lazyPropertyNumbers;
203+
202204
private final Type[] lazyPropertyTypes;
203205
private final String[][] lazyPropertyColumnAliases;
204206

@@ -266,7 +268,7 @@ public abstract class AbstractEntityPersister
266268
protected ExecuteUpdateResultCheckStyle[] deleteResultCheckStyles;
267269

268270
private InsertGeneratedIdentifierDelegate identityDelegate;
269-
271+
private LazyPropertyInitializer lazyPropertyInitializerDelegater = new LazyPropertyInitializerImpl( this );
270272
private boolean[] tableHasColumns;
271273

272274
private final String loaderName;
@@ -1204,149 +1206,13 @@ protected String generateLazySelectString() {
12041206
ArrayHelper.toIntArray( formulaNumbers ) );
12051207

12061208
}
1207-
1209+
@Override
12081210
public Object initializeLazyProperty(String fieldName, Object entity, SessionImplementor session)
12091211
throws HibernateException {
1210-
1211-
final Serializable id = session.getContextEntityIdentifier( entity );
1212-
1213-
final EntityEntry entry = session.getPersistenceContext().getEntry( entity );
1214-
if ( entry == null ) {
1215-
throw new HibernateException( "entity is not associated with the session: " + id );
1216-
}
1217-
1218-
if ( LOG.isTraceEnabled() ) {
1219-
LOG.tracev( "Initializing lazy properties of: {0}, field access: {1}", MessageHelper.infoString( this, id, getFactory() ), fieldName );
1220-
}
1221-
1222-
if ( hasCache() ) {
1223-
CacheKey cacheKey = session.generateCacheKey( id, getIdentifierType(), getEntityName() );
1224-
Object ce = getCacheAccessStrategy().get( cacheKey, session.getTimestamp() );
1225-
if (ce!=null) {
1226-
CacheEntry cacheEntry = (CacheEntry) getCacheEntryStructure().destructure(ce, factory);
1227-
if ( !cacheEntry.areLazyPropertiesUnfetched() ) {
1228-
//note early exit here:
1229-
return initializeLazyPropertiesFromCache( fieldName, entity, session, entry, cacheEntry );
1230-
}
1231-
}
1232-
}
1233-
1234-
return initializeLazyPropertiesFromDatastore( fieldName, entity, session, id, entry );
1212+
return lazyPropertyInitializerDelegater.initializeLazyProperty( fieldName, entity, session );
12351213

12361214
}
12371215

1238-
private Object initializeLazyPropertiesFromDatastore(
1239-
final String fieldName,
1240-
final Object entity,
1241-
final SessionImplementor session,
1242-
final Serializable id,
1243-
final EntityEntry entry) {
1244-
1245-
if ( !hasLazyProperties() ) {
1246-
throw new AssertionFailure( "no lazy properties" );
1247-
}
1248-
1249-
LOG.trace( "Initializing lazy properties from datastore" );
1250-
1251-
try {
1252-
1253-
Object result = null;
1254-
PreparedStatement ps = null;
1255-
try {
1256-
final String lazySelect = getSQLLazySelectString();
1257-
ResultSet rs = null;
1258-
try {
1259-
if ( lazySelect != null ) {
1260-
// null sql means that the only lazy properties
1261-
// are shared PK one-to-one associations which are
1262-
// handled differently in the Type#nullSafeGet code...
1263-
ps = session.getTransactionCoordinator()
1264-
.getJdbcCoordinator()
1265-
.getStatementPreparer()
1266-
.prepareStatement( lazySelect );
1267-
getIdentifierType().nullSafeSet( ps, id, 1, session );
1268-
rs = ps.executeQuery();
1269-
rs.next();
1270-
}
1271-
final Object[] snapshot = entry.getLoadedState();
1272-
for ( int j = 0; j < lazyPropertyNames.length; j++ ) {
1273-
Object propValue = lazyPropertyTypes[j].nullSafeGet( rs, lazyPropertyColumnAliases[j], session, entity );
1274-
if ( initializeLazyProperty( fieldName, entity, session, snapshot, j, propValue ) ) {
1275-
result = propValue;
1276-
}
1277-
}
1278-
}
1279-
finally {
1280-
if ( rs != null ) {
1281-
rs.close();
1282-
}
1283-
}
1284-
}
1285-
finally {
1286-
if ( ps != null ) {
1287-
ps.close();
1288-
}
1289-
}
1290-
1291-
LOG.trace( "Done initializing lazy properties" );
1292-
1293-
return result;
1294-
1295-
}
1296-
catch ( SQLException sqle ) {
1297-
throw getFactory().getSQLExceptionHelper().convert(
1298-
sqle,
1299-
"could not initialize lazy properties: " +
1300-
MessageHelper.infoString( this, id, getFactory() ),
1301-
getSQLLazySelectString()
1302-
);
1303-
}
1304-
}
1305-
1306-
private Object initializeLazyPropertiesFromCache(
1307-
final String fieldName,
1308-
final Object entity,
1309-
final SessionImplementor session,
1310-
final EntityEntry entry,
1311-
final CacheEntry cacheEntry
1312-
) {
1313-
1314-
LOG.trace( "Initializing lazy properties from second-level cache" );
1315-
1316-
Object result = null;
1317-
Serializable[] disassembledValues = cacheEntry.getDisassembledState();
1318-
final Object[] snapshot = entry.getLoadedState();
1319-
for ( int j = 0; j < lazyPropertyNames.length; j++ ) {
1320-
final Object propValue = lazyPropertyTypes[j].assemble(
1321-
disassembledValues[ lazyPropertyNumbers[j] ],
1322-
session,
1323-
entity
1324-
);
1325-
if ( initializeLazyProperty( fieldName, entity, session, snapshot, j, propValue ) ) {
1326-
result = propValue;
1327-
}
1328-
}
1329-
1330-
LOG.trace( "Done initializing lazy properties" );
1331-
1332-
return result;
1333-
}
1334-
1335-
private boolean initializeLazyProperty(
1336-
final String fieldName,
1337-
final Object entity,
1338-
final SessionImplementor session,
1339-
final Object[] snapshot,
1340-
final int j,
1341-
final Object propValue) {
1342-
setPropertyValue( entity, lazyPropertyNumbers[j], propValue );
1343-
if ( snapshot != null ) {
1344-
// object have been loaded with setReadOnly(true); HHH-2236
1345-
snapshot[ lazyPropertyNumbers[j] ] = lazyPropertyTypes[j].deepCopy( propValue, factory );
1346-
}
1347-
return fieldName.equals( lazyPropertyNames[j] );
1348-
}
1349-
13501216
public boolean isBatchable() {
13511217
return optimisticLockStyle() == OptimisticLockStyle.NONE
13521218
|| ( !isVersioned() && optimisticLockStyle() == OptimisticLockStyle.VERSION )
@@ -3014,15 +2880,23 @@ public Object getEntity() {
30142880
return identityDelegate.performInsert( sql, session, binder );
30152881
}
30162882

2883+
private ValueHolder<String> identitySelectStringValue = new ValueHolder<String>(
2884+
new ValueHolder.DeferredInitializer<String>() {
2885+
@Override
2886+
public String initialize() {
2887+
return getFactory().getDialect().getIdentitySelectString(
2888+
getTableName( 0 ),
2889+
getKeyColumns( 0 )[0],
2890+
getIdentifierType().sqlTypes( getFactory() )[0]
2891+
);
2892+
}
2893+
}
2894+
);
2895+
@Override
30172896
public String getIdentitySelectString() {
3018-
//TODO: cache this in an instvar
3019-
return getFactory().getDialect().getIdentitySelectString(
3020-
getTableName(0),
3021-
getKeyColumns(0)[0],
3022-
getIdentifierType().sqlTypes( getFactory() )[0]
3023-
);
2897+
return identitySelectStringValue.getValue();
30242898
}
3025-
2899+
@Override
30262900
public String getSelectByUniqueKeyString(String propertyName) {
30272901
return new SimpleSelect( getFactory().getDialect() )
30282902
.setTableName( getTableName(0) )
@@ -5024,5 +4898,21 @@ public String getTableAliasForColumn(String columnName, String rootAlias) {
50244898
public int determineTableNumberForColumn(String columnName) {
50254899
return 0;
50264900
}
5027-
4901+
4902+
4903+
Type[] getLazyPropertyTypes() {
4904+
return lazyPropertyTypes;
4905+
}
4906+
4907+
int[] getLazyPropertyNumbers() {
4908+
return lazyPropertyNumbers;
4909+
}
4910+
4911+
String[] getLazyPropertyNames() {
4912+
return lazyPropertyNames;
4913+
}
4914+
4915+
String[][] getLazyPropertyColumnAliases() {
4916+
return lazyPropertyColumnAliases;
4917+
}
50284918
}

0 commit comments

Comments
 (0)