Skip to content

Commit

Permalink
OGM-1375 Avoid use of partial entities
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD committed Mar 12, 2018
1 parent bea69a6 commit 58680cc
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/org/hibernate/ogm/util/impl/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,7 @@ public interface Log extends BasicLogger {

@Message(id = 89, value = "%1$s does not support queries on polymorphic entities using TABLE_PER_CLASS inheritance strategy. You should try using SINGLE_TABLE instead. Entities: %2$s")
HibernateException queriesOnPolymorphicEntitiesAreNotSupportedWithTablePerClass( String datastore, Collection<String> subclassEntityNames );

@Message(id = 91, value = "Projection and addEntity are not allowed in the same query on <%1$s> %2$s")
HibernateException addEntityNotAllowedInNativeQueriesUsingProjection( String table, String query );
}
Original file line number Diff line number Diff line change
Expand Up @@ -844,9 +844,14 @@ public ClosableIterator<Tuple> executeBackendQuery(BackendQuery<MongoDBQueryDesc

MongoDBQueryDescriptor queryDescriptor = backendQuery.getQuery();

EntityKeyMetadata entityKeyMetadata =
backendQuery.getSingleEntityMetadataInformationOrNull() == null ? null :
backendQuery.getSingleEntityMetadataInformationOrNull().getEntityKeyMetadata();
EntityKeyMetadata entityKeyMetadata = backendQuery.getSingleEntityMetadataInformationOrNull() == null
? null
: backendQuery.getSingleEntityMetadataInformationOrNull().getEntityKeyMetadata();

// Projections and addEntities are not allowed in the same query at the same time
if ( entityKeyMetadata != null && queryDescriptor.getProjection() != null ) {
throw log.addEntityNotAllowedInNativeQueriesUsingProjection( entityKeyMetadata.getTable(), backendQuery.toString() );
}

String collectionName = getCollectionName( backendQuery, queryDescriptor, entityKeyMetadata );
MongoCollection<Document> collection = provider.getDatabase().getCollection( collectionName );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@
import org.hibernate.ogm.model.spi.Tuple.SnapshotType;
import org.hibernate.ogm.model.spi.TupleOperation;
import org.hibernate.ogm.model.spi.TupleSnapshot;
import org.neo4j.driver.internal.types.InternalTypeSystem;
import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.Statement;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.v1.Transaction;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.summary.ResultSummary;
import org.neo4j.driver.v1.types.Node;
Expand Down Expand Up @@ -118,7 +120,14 @@ public ClosableIterator<Tuple> executeBackendQuery(BackendQuery<String> backendQ
List<EntityKey> entityKeys = new ArrayList<>();
while ( result.hasNext() ) {
Record record = result.next();
Map<String, Object> recordAsMap = record.get( 0 ).asMap();
Value value = record.get( 0 );

if ( isProjection( value ) ) {
// Projections and addEntities are not allowed in the same query at the same time
throw log.addEntityNotAllowedInNativeQueriesUsingProjection( entityKeyMetadata.getTable(), backendQuery.getQuery() );
}

Map<String, Object> recordAsMap = value.asMap();
Object[] columnValues = columnValues( recordAsMap, entityKeyMetadata );
entityKeys.add( new EntityKey( entityKeyMetadata, columnValues ) );
}
Expand All @@ -134,6 +143,10 @@ public ClosableIterator<Tuple> executeBackendQuery(BackendQuery<String> backendQ
}
}

private boolean isProjection(Value value) {
return !( InternalTypeSystem.TYPE_SYSTEM.NODE().equals( value.type() ) );
}

@Override
public int executeBackendUpdateQuery(BackendQuery<String> query, QueryParameters queryParameters, TupleContext tupleContext) {
Map<String, Object> parameters = getParameters( queryParameters );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ public void removeTuple(EntityKey key, TupleContext tupleContext) {
* <p>
* the first time with the information related to the owner of the association and the {@link RowKey},
* the second time using the same {@link RowKey} but with the {@link AssociationKey} referring to the other side of the association.
* @param associatedEntityKeyMetadata
* @param associationKey
* @param action
* @param associationContext
*/
private void putAssociationOperation(AssociationKey associationKey, AssociationOperation action, AssociationContext associationContext) {
switch ( associationKey.getMetadata().getAssociationKind() ) {
Expand Down Expand Up @@ -530,7 +531,14 @@ public ClosableIterator<Tuple> executeBackendQuery(BackendQuery<String> backendQ
List<Row> rows = results.get( 0 ).getData();
EntityKey[] keys = new EntityKey[ rows.size() ];
for ( int i = 0; i < rows.size(); i++ ) {
Node node = rows.get( i ).getGraph().getNodes().get( 0 );
List<Node> nodes = rows.get( i ).getGraph().getNodes();

if ( nodes.isEmpty() ) {
// Projections and addEntities are not allowed in the same query at the same time
throw log.addEntityNotAllowedInNativeQueriesUsingProjection( entityKeyMetadata.getTable(), backendQuery.getQuery() );
}

Node node = nodes.get( 0 );
Object[] values = columnValues( node, entityKeyMetadata );
keys[i] = new EntityKey( entityKeyMetadata, values );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
*/
package org.hibernate.ogm.datastore.neo4j.embedded.dialect.impl;

import java.lang.invoke.MethodHandles;
import java.util.Map;

import org.hibernate.ogm.datastore.map.impl.MapTupleSnapshot;
import org.hibernate.ogm.datastore.neo4j.logging.impl.Log;
import org.hibernate.ogm.datastore.neo4j.logging.impl.LoggerFactory;
import org.hibernate.ogm.dialect.spi.TupleContext;
import org.hibernate.ogm.dialect.spi.TupleTypeContext;
import org.hibernate.ogm.model.key.spi.EntityKeyMetadata;
Expand All @@ -28,6 +31,8 @@
*/
public class EmbeddedNeo4jBackendQueryResultIterator extends EmbeddedNeo4jTupleIterator<Map<String, Object>> {

private static final Log log = LoggerFactory.make( MethodHandles.lookup() );

private final EntityKeyMetadata entityKeyMetadata;
private final TupleTypeContext tupleTypeContext;

Expand All @@ -47,9 +52,14 @@ private TupleSnapshot createSnapshot(Map<String, Object> next) {
if ( entityKeyMetadata == null ) {
return mapSnapshot( next );
}
else {
return nodeSnapshot( (Node) next.values().iterator().next() );

Object value = next.values().iterator().next();
if ( value instanceof Node ) {
return nodeSnapshot( (Node) value );
}

// Projections and addEntities are not allowed in the same query at the same time
throw log.addEntityNotAllowedInNativeQueriesUsingProjection( entityKeyMetadata.getTable(), "" );
}

private TupleSnapshot mapSnapshot(Map<String, Object> next) {
Expand Down

0 comments on commit 58680cc

Please sign in to comment.