8000 Very minor things by gavinking · Pull Request #10188 · hibernate/hibernate-orm · GitHub
[go: up one dir, main page]

Skip to content

Very minor things #10188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@
package org.hibernate;

/**
* Thrown when the user passes a transient instance to a {@link Session}
* method that expects a persistent instance.
* Thrown if a transient instance of an entity class is passed to
* a {@link Session} method that expects a persistent instance,
* or if the state of an entity instance cannot be made persistent
* because the instance holds a reference to a transient entity.
* <p>
* An entity is considered <em>transient</em> if it is:
* <ul>
* <li>a newly-instantiated instance of an entity class which has
* never been {@linkplain Session#persist made persistent} in
* the database, or
* <li>an entity instance previously associated with a persistence
* context which has been {@linkplain Session#remove removed}
* from the database.
* </ul>
*
* @author Gavin King
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@
import static org.hibernate.internal.util.StringHelper.qualify;

/**
* Thrown when a property cannot be persisted because it is an association
* with a transient unsaved entity instance.
* Thrown when the state of an entity cannot be made persistent
* because the entity holds a reference to a transient entity.
* <p>
* An entity is considered <em>transient</em> if it is:
* <ul>
* <li>a newly-instantiated instance of an entity class which has
* never been {@linkplain Session#persist made persistent} in
* the database, or
* <li>an entity instance previously associated with a persistence
* context which has been {@linkplain Session#remove removed}
* from the database.
* </ul>
*
* @author Gail Badner
*/
Expand Down Expand Up @@ -68,6 +78,7 @@ public String getPropertyName() {
@Override
public String getMessage() {
return super.getMessage() + " for entity "
+ qualify( propertyOwnerEntityName, propertyName ) + " -> " + transientEntityName;
+ qualify( propertyOwnerEntityName, propertyName )
+ " -> " + transientEntityName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,30 @@ default String getPartName() {
/**
* Extract the identifier from an instance of the entity
*
* It's supposed to be use during the merging process
* @apiNote Intended for use during the merging process
*/
default Object getIdentifier(Object entity, MergeContext mergeContext){
return getIdentifier( entity );
}

/**
* Return the identifier of the persistent or transient object, or throw
* an exception if the instance is "unsaved"
* <p>
* Used by OneToOneType and ManyToOneType to determine what id value should
* be used for an object that may or may not be associated with the session.
* This does a "best guess" using any/all info available to use (not just the
* EntityEntry).
* Return the identifier of the persistent or transient object, or throw an
* exception if the instance is "unsaved"
*
* @apiNote This method is called by {@link org.hibernate.type.OneToOneType}
* and {@link org.hibernate.type.ManyToOneType} to determine the id value
* which should be used for an object that may or may not be associated with
* the session. This does a "best guess" using any/all info available to use
* (not just the {@link org.hibernate.engine.spi.EntityEntry}).
*
* @param entity The entity instance
* @param session The session
*
* @return The identifier
*
* @throws TransientObjectException if the entity is transient (does not yet have an identifier)
* @see org.hibernate.engine.internal.ForeignKeys#getEntityIdentifierIfNotUnsaved(String, Object, SharedSessionContractImplementor)
* @throws TransientObjectException if the entity is transient
* (does not yet have an identifier)
* @see org.hibernate.engine.internal.ForeignKeys#getEntityIdentifierIfNotUnsaved
* @since 6.1.1
*/
default Object getIdentifierIfNotUnsaved(Object entity, SharedSessionContractImplementor session) {
Expand All @@ -103,20 +105,23 @@ else if ( session == null ) {
// If we have no session available, just return the identifier
return getIdentifier( entity );
}
Object id = session.getContextEntityIdentifier( entity );
final Object id = session.getContextEntityIdentifier( entity );
if ( id == null ) {
// context-entity-identifier returns null explicitly if the entity
// is not associated with the persistence context; so make some
// deeper checks...
// getContextEntityIdentifier() returned null, indicating that
// the entity is not associated with the persistence context,
// so look deeper for its id
final String entityName = findContainingEntityMapping().getEntityName();
if ( ForeignKeys.isTransient( entityName, entity, Boolean.FALSE, session ) ) {
// TODO should be a TransientPropertyValueException
throw new TransientObjectException( "object references an unsaved transient instance of '"
+ (entityName == null ? session.guessEntityName( entity ) : entityName)
+ "' save the transient instance before flushing" );
}
id = getIdentifier( entity );
return getIdentifier( entity );
}
else {
return id;
}
return id;
}

/**
Expand Down
< 6D40 /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public class NativeQueryImpl<R>
private Callback callback;

/**
* Constructs a NativeQueryImpl given a sql query defined in the mappings.
* Constructs a {@code NativeQueryImpl} given a SQL query defined in the mappings.
* Used by Hibernate Reactive.
*/
@SuppressWarnings("unused")
Expand Down 8000 Expand Up @@ -191,7 +191,7 @@ public NativeQueryImpl(NamedNativeQueryMemento<?> memento, SharedSessionContract
}

/**
* Constructs a NativeQueryImpl given a sql query defined in the mappings.
* Constructs a {@code NativeQueryImpl} given a SQL query defined in the mappings.
*/
public NativeQueryImpl(
NamedNativeQueryMemento<?> memento,
Expand Down Expand Up @@ -228,7 +228,7 @@ public NativeQueryImpl(
}

/**
* Constructs a NativeQueryImpl given a sql query defined in the mappings.
* Constructs a {@code NativeQueryImpl} given a SQL query defined in the mappings.
*/
public NativeQueryImpl(
NamedNativeQueryMemento<?> memento,
Expand Down Expand Up @@ -378,7 +378,7 @@ else if ( Map.class.equals( resultClass ) ) {
else if ( List.class.equals( resultClass ) ) {
return NativeQueryListTransformer.INSTANCE;
}
else if ( Object[].class.equals( resultClass )) {
else if ( Object[].class.equals( resultClass ) ) {
return NativeQueryArrayTransformer.INSTANCE;
}
else if ( resultClass != Object.class ) {
Expand Down Expand Up @@ -535,23 +535,26 @@ public Class<R> getResultType() {

@Override
public NamedNativeQueryMemento<R> toMemento(String name) {
final QueryOptions options = getQueryOptions();
return new NamedNativeQueryMementoImpl<>(
name,
resultType != null ? resultType : extractResultClass( resultSetMapping ),
resultType == null
? extractResultClass( resultSetMapping )
: resultType,
sqlString,
originalSqlString,
resultSetMapping.getMappingIdentifier(),
querySpaces,
isCacheable(),
getCacheRegion(),
getCacheMode(),
getQueryOptions().getFlushMode(),
options.getFlushMode(),
isReadOnly(),
getTimeout(),
getFetchSize(),
getComment(),
getQueryOptions().getLimit().getFirstRow(),
getQueryOptions().getLimit().getMaxRows(),
options.getLimit().getFirstRow(),
options.getLimit().getMaxRows(),
getHints()
);
}
Expand Down Expand Up @@ -729,16 +732,16 @@ public KeyedResultList<R> getKeyedResultList(KeyedPage<R> page) {
protected SelectQueryPlan<R> resolveSelectQueryPlan() {
final ResultSetMapping mapping;
if ( resultType != null && resultSetMapping.isDynamic() && resultSetMapping.getNumberOfResultBuilders() == 0 ) {
mapping = ResultSetMapping.resolveResultSetMapping( originalSqlString, true, getSessionFactory() );

if ( getSessionFactory().getMappingMetamodel().isEntityClass( resultType ) ) {
final SessionFactoryImplementor sessionFactory = getSessionFactory();
mapping = ResultSetMapping.resolveResultSetMapping( originalSqlString, true, sessionFactory );
if ( sessionFactory.getMappingMetamodel().isEntityClass( resultType ) ) {
mapping.addResultBuilder(
Builders.entityCalculated( unqualify( resultType.getName() ), resultType.getName(),
LockMode.READ, getSessionFactory() ) );
LockMode.READ, sessionFactory ) );
}
else if ( !isResultTypeAlwaysAllowed( resultType )
&& (!isClass( resultType ) || hasJavaTypeDescriptor( resultType )) ) {
mapping.addResultBuilder( Builders.resultClassBuilder( resultType, getSessionFactory().getMappingMetamodel() ) );
mapping.addResultBuilder( Builders.resultClassBuilder( resultType, sessionFactory.getMappingMetamodel() ) );
}
}
else {
Expand Down Expand Up @@ -960,12 +963,13 @@ public static int determineBindValueMaxCount(boolean paddingEnabled, int inExprL
}

private SelectInterpretationsKey selectInterpretationsKey(ResultSetMapping resultSetMapping) {
final QueryOptions options = getQueryOptions();
return new SelectInterpretationsKey(
getQueryString(),
resultSetMapping,
getSynchronizedQuerySpaces(),
getQueryOptions().getTupleTransformer(),
getQueryOptions().getResultListTransformer()
options.getTupleTransformer(),
options.getResultListTransformer()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.hibernate.query.Page;
import org.hibernate.query.QueryLogging;
import org.hibernate.query.SelectionQuery;
import org.hibernate.query.criteria.JpaSelection;
import org.hibernate.query.hql.internal.QuerySplitter;
import org.hibernate.query.spi.AbstractSelectionQuery;
import org.hibernate.query.spi.HqlInterpretation;
Expand All @@ -32,7 +31,6 @@
import org.hibernate.query.sqm.tree.expression.SqmJpaCriteriaParameterWrapper;
import org.hibernate.query.sqm.tree.expression.SqmParameter;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.query.sqm.tree.select.SqmSelectableNode;
import org.hibernate.query.sqm.tree.select.SqmSelection;
import org.hibernate.sql.results.internal.TupleMetadata;
import org.hibernate.type.BasicTypeRegistry;
Expand Down Expand Up @@ -318,23 +316,24 @@ private static boolean isInstantiableWithoutMetadata(Class<?> resultType) {
}

private TupleMetadata getTupleMetadata(List<SqmSelection<?>> selections) {
if ( getQueryOptions().getTupleTransformer() == null ) {
final var tupleTransformer = getQueryOptions().getTupleTransformer();
if ( tupleTransformer == null ) {
return new TupleMetadata( buildTupleElementArray( selections ), buildTupleAliasArray( selections ) );
}
else {
throw new IllegalArgumentException(
"Illegal combination of Tuple resultType and (non-JpaTupleBuilder) TupleTransformer: "
+ getQueryOptions().getTupleTransformer()
+ tupleTransformer
);
}
}

private static TupleElement<?>[] buildTupleElementArray(List<SqmSelection<?>> selections) {
if ( selections.size() == 1 ) {
final SqmSelectableNode<?> selectableNode = selections.get( 0).getSelectableNode();
final var selectableNode = selections.get( 0 ).getSelectableNode();
if ( selectableNode instanceof CompoundSelection<?> ) {
final List<? extends JpaSelection<?>> selectionItems = selectableNode.getSelectionItems();
final TupleElement<?>[] elements = new TupleElement<?>[ selectionItems.size() ];
final var selectionItems = selectableNode.getSelectionItems();
final var elements = new TupleElement<?>[ selectionItems.size() ];
for ( int i = 0; i < selectionItems.size(); i++ ) {
elements[i] = selectionItems.get( i );
}
Expand All @@ -345,7 +344,7 @@ private static TupleElement<?>[] buildTupleElementArray(List<SqmSelection<?>> se
}
}
else {
final TupleElement<?>[] elements = new TupleElement<?>[ selections.size() ];
final var elements = new TupleElement<?>[ selections.size() ];
for ( int i = 0; i < selections.size(); i++ ) {
elements[i] = selections.get( i ).getSelectableNode();
}
Expand All @@ -355,10 +354,10 @@ private static TupleElement<?>[] buildTupleElementArray(List<SqmSelection<?>> se

private static String[] buildTupleAliasArray(List<SqmSelection<?>> selections) {
if ( selections.size() == 1 ) {
final SqmSelectableNode<?> selectableNode = selections.get(0).getSelectableNode();
final var selectableNode = selections.get(0).getSelectableNode();
if ( selectableNode instanceof CompoundSelection<?> ) {
final List<? extends JpaSelection<?>> selectionItems = selectableNode.getSelectionItems();
final String[] elements = new String[ selectionItems.size() ];
final var selectionItems = selectableNode.getSelectionItems();
final String[] elements = new String[ selectionItems.size() ];
for ( int i = 0; i < selectionItems.size(); i++ ) {
elements[i] = selectionItems.get( i ).getAlias();
}
Expand Down
0