Skip to content

Commit

Permalink
HHH-8741 - More checkstyle cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Nov 25, 2013
1 parent 8fe5460 commit 783831f
Show file tree
Hide file tree
Showing 51 changed files with 1,114 additions and 906 deletions.
Expand Up @@ -22,7 +22,6 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.persistence.AttributeNode;
Expand All @@ -44,28 +43,26 @@
* Encapsulates a JPA EntityGraph provided through a JPQL query hint. Converts the fetches into a list of AST
* FromElements. The logic is kept here as much as possible in order to make it easy to remove this in the future,
* once our AST is improved and this "hack" is no longer needed.
*
*
* @author Brett Meyer
*/
public class EntityGraphQueryHint {

private final EntityGraph<?> originEntityGraph;
public EntityGraphQueryHint( EntityGraph<?> originEntityGraph ) {

public EntityGraphQueryHint(EntityGraph<?> originEntityGraph) {
this.originEntityGraph = originEntityGraph;
}

public List<FromElement> toFromElements(FromClause fromClause, HqlSqlWalker walker) {
// If a role already has an explicit fetch in the query, skip it in the graph.
Map<String, FromElement> explicitFetches = new HashMap<String, FromElement>();
Iterator iter = fromClause.getFromElements().iterator();
while ( iter.hasNext() ) {
final FromElement fromElement = ( FromElement ) iter.next();
if (fromElement.getRole() != null) {
for ( Object o : fromClause.getFromElements() ) {
final FromElement fromElement = (FromElement) o;
if ( fromElement.getRole() != null ) {
explicitFetches.put( fromElement.getRole(), fromElement );
}
}

return getFromElements(
originEntityGraph.getAttributeNodes(),
fromClause.getFromElement(),
Expand All @@ -74,42 +71,43 @@ public List<FromElement> toFromElements(FromClause fromClause, HqlSqlWalker walk
explicitFetches
);
}

private List<FromElement> getFromElements(
List attributeNodes,
FromElement origin,
FromClause fromClause,
HqlSqlWalker walker,
Map<String, FromElement> explicitFetches) {
final List<FromElement> fromElements = new ArrayList<FromElement>();
for (Object obj : attributeNodes) {

for ( Object obj : attributeNodes ) {
final AttributeNode<?> attributeNode = (AttributeNode<?>) obj;

final String attributeName = attributeNode.getAttributeName();
final String className = origin.getClassName();
final String role = className + "." + attributeName;

final String classAlias = origin.getClassAlias();

final String originTableAlias = origin.getTableAlias();

Type propertyType = origin.getPropertyType( attributeName, attributeName );

final Type propertyType = origin.getPropertyType( attributeName, attributeName );

try {
FromElement fromElement = null;
if (!explicitFetches.containsKey( role )) {
if ( !explicitFetches.containsKey( role ) ) {
if ( propertyType.isEntityType() ) {
final EntityType entityType = (EntityType) propertyType;

final String[] columns = origin.toColumns( originTableAlias, attributeName, false );
final String tableAlias = walker.getAliasGenerator().createName(
entityType.getAssociatedEntityName() );

final FromElementFactory fromElementFactory = new FromElementFactory( fromClause, origin,
attributeName, classAlias, columns, false);
entityType.getAssociatedEntityName()
);

final FromElementFactory fromElementFactory = new FromElementFactory(
fromClause, origin,
attributeName, classAlias, columns, false
);
final JoinSequence joinSequence = walker.getSessionFactoryHelper().createJoinSequence(
false, entityType, tableAlias, JoinType.LEFT_OUTER_JOIN, columns );
false, entityType, tableAlias, JoinType.LEFT_OUTER_JOIN, columns
);
fromElement = fromElementFactory.createEntityJoin(
entityType.getAssociatedEntityName(),
tableAlias,
Expand All @@ -121,33 +119,40 @@ private List<FromElement> getFromElements(
null
);
}
else if ( propertyType.isCollectionType() ) {
final String[] columns = origin.toColumns( originTableAlias, attributeName, false );

final FromElementFactory fromElementFactory = new FromElementFactory( fromClause, origin,
attributeName, classAlias, columns, false);
else if ( propertyType.isCollectionType() ) {
final String[] columns = origin.toColumns( originTableAlias, attributeName, false );

final FromElementFactory fromElementFactory = new FromElementFactory(
fromClause, origin,
attributeName, classAlias, columns, false
);
final QueryableCollection queryableCollection = walker.getSessionFactoryHelper()
.requireQueryableCollection( role );
fromElement = fromElementFactory.createCollection(
queryableCollection, role, JoinType.LEFT_OUTER_JOIN, true, false ) ;
queryableCollection, role, JoinType.LEFT_OUTER_JOIN, true, false
);
}
}
if (fromElement != null) {

if ( fromElement != null ) {
fromElements.add( fromElement );

// recurse into subgraphs
for (Subgraph<?> subgraph : attributeNode.getSubgraphs().values()) {
fromElements.addAll( getFromElements( subgraph.getAttributeNodes(), fromElement,
fromClause, walker, explicitFetches ) );
for ( Subgraph<?> subgraph : attributeNode.getSubgraphs().values() ) {
fromElements.addAll(
getFromElements(
subgraph.getAttributeNodes(), fromElement,
fromClause, walker, explicitFetches
)
);
}
}
}
catch (Exception e) {
throw new QueryException( "Could not apply the EntityGraph to the Query!", e );
}
}

return fromElements;
}
}
@@ -1,10 +1,10 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* Copyright (c) 2008, 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
Expand All @@ -20,16 +20,16 @@
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.hibernate.engine.query.spi.sql;

import java.util.Map;

import org.hibernate.LockMode;

/**
* Represents a return defined as part of a native sql query which
* names a collection role in the form {classname}.{collectionrole}; it
* names a collection role in the form {className}.{collectionRole}; it
* is used in defining a custom sql query for loading an entity's
* collection in non-fetching scenarios (i.e., loading the collection
* itself as the "root" of the result).
Expand All @@ -52,6 +52,7 @@ public class NativeSQLQueryCollectionReturn extends NativeSQLQueryNonScalarRetur
* @param propertyResults Any user-supplied column->property mappings
* @param lockMode The lock mode to apply to the collection.
*/
@SuppressWarnings("unchecked")
public NativeSQLQueryCollectionReturn(
String alias,
String ownerEntityName,
Expand All @@ -64,6 +65,13 @@ public NativeSQLQueryCollectionReturn(
this.hashCode = determineHashCode();
}

private int determineHashCode() {
int result = super.hashCode();
result = 31 * result + ( ownerEntityName != null ? ownerEntityName.hashCode() : 0 );
result = 31 * result + ( ownerProperty != null ? ownerProperty.hashCode() : 0 );
return result;
}

/**
* Returns the class owning the collection.
*
Expand All @@ -82,6 +90,8 @@ public String getOwnerProperty() {
return ownerProperty;
}

@Override
@SuppressWarnings("RedundantIfStatement")
public boolean equals(Object o) {
if ( this == o ) {
return true;
Expand All @@ -93,7 +103,7 @@ public boolean equals(Object o) {
return false;
}

NativeSQLQueryCollectionReturn that = ( NativeSQLQueryCollectionReturn ) o;
final NativeSQLQueryCollectionReturn that = (NativeSQLQueryCollectionReturn) o;

if ( ownerEntityName != null ? !ownerEntityName.equals( that.ownerEntityName ) : that.ownerEntityName != null ) {
return false;
Expand All @@ -105,14 +115,8 @@ public boolean equals(Object o) {
return true;
}

@Override
public int hashCode() {
return hashCode;
}

private int determineHashCode() {
int result = super.hashCode();
result = 31 * result + ( ownerEntityName != null ? ownerEntityName.hashCode() : 0 );
result = 31 * result + ( ownerProperty != null ? ownerProperty.hashCode() : 0 );
return result;
}
}
@@ -1,10 +1,10 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* Copyright (c) 2008, 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
Expand All @@ -20,9 +20,9 @@
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.hibernate.engine.query.spi.sql;

import java.util.Map;

import org.hibernate.LockMode;
Expand All @@ -46,6 +46,7 @@ public class NativeSQLQueryJoinReturn extends NativeSQLQueryNonScalarReturn {
* @param propertyResults Any user-supplied column->property mappings
* @param lockMode The lock mode to apply
*/
@SuppressWarnings("unchecked")
public NativeSQLQueryJoinReturn(
String alias,
String ownerAlias,
Expand All @@ -58,6 +59,13 @@ public NativeSQLQueryJoinReturn(
this.hashCode = determineHashCode();
}

private int determineHashCode() {
int result = super.hashCode();
result = 31 * result + ( ownerAlias != null ? ownerAlias.hashCode() : 0 );
result = 31 * result + ( ownerProperty != null ? ownerProperty.hashCode() : 0 );
return result;
}

/**
* Retrieve the alias of the owner of this fetched association.
*
Expand All @@ -77,6 +85,8 @@ public String getOwnerProperty() {
return ownerProperty;
}

@Override
@SuppressWarnings("RedundantIfStatement")
public boolean equals(Object o) {
if ( this == o ) {
return true;
Expand All @@ -88,7 +98,7 @@ public boolean equals(Object o) {
return false;
}

NativeSQLQueryJoinReturn that = ( NativeSQLQueryJoinReturn ) o;
final NativeSQLQueryJoinReturn that = (NativeSQLQueryJoinReturn) o;

if ( ownerAlias != null ? !ownerAlias.equals( that.ownerAlias ) : that.ownerAlias != null ) {
return false;
Expand All @@ -100,14 +110,8 @@ public boolean equals(Object o) {
return true;
}

@Override
public int hashCode() {
return hashCode;
}

private int determineHashCode() {
int result = super.hashCode();
result = 31 * result + ( ownerAlias != null ? ownerAlias.hashCode() : 0 );
result = 31 * result + ( ownerProperty != null ? ownerProperty.hashCode() : 0 );
return result;
}
}

0 comments on commit 783831f

Please sign in to comment.