Skip to content

Commit

Permalink
GH-551 - Ensure that id fields are mapped correctly during adhoc mapp…
Browse files Browse the repository at this point in the history
…ing.

3.1.x doesnt return Id fields as properties (3.2.x onwards does), hence we must add an additional query to the single use entity mapper.
  • Loading branch information
michael-simons committed Dec 9, 2019
1 parent 52a5997 commit 8c47d6b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ private void writeProperty(ClassInfo classInfo, Object instance, Map.Entry<Strin
FieldInfo fieldInfo = classInfo.relationshipFieldByName(property.getKey());
if (fieldInfo != null) {
writer = fieldInfo;
} else if (property.getKey().equals("id")) {
writer = classInfo.identityField();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.neo4j.ogm.domain.gh391.SomeContainer;
import org.neo4j.ogm.domain.gh551.AnotherThing;
import org.neo4j.ogm.domain.gh551.ThingResult;
import org.neo4j.ogm.domain.gh551.ThingWIthId;
import org.neo4j.ogm.domain.gh552.Thing;
import org.neo4j.ogm.metadata.MetaData;
import org.neo4j.ogm.metadata.reflect.ReflectionEntityInstantiator;
Expand Down Expand Up @@ -76,13 +77,35 @@ public void singleUseEntityMapperShouldWorkWithNestedObjects() {
.allSatisfy(s -> ((String) s).startsWith("Thing"));
}

/**
* ID fields are treated differently in different versions of OGM. This tests assures that they work correctly.
*/
@Test // GH-551
public void shouldUseIdFields() {

SingleUseEntityMapper entityMapper =
new SingleUseEntityMapper(sessionFactory.metaData(),
new ReflectionEntityInstantiator(sessionFactory.metaData()));

Iterable<Map<String, Object>> results = sessionFactory.openSession()
.query("MATCH (t:ThingEntity) RETURN 4711 as id, 'a name' as name LIMIT 1", EMPTY_MAP)
.queryResults();

assertThat(results).hasSize(1);

ThingWIthId thingResult = entityMapper.map(ThingWIthId.class, results.iterator().next());
assertThat(thingResult.getName()).isEqualTo("a name");
assertThat(thingResult.getId()).isEqualTo(4711);
}

@Test // GH-552
public void shouldLookupCorrectRootClass() {
MetaData metaData = new MetaData("org.neo4j.ogm.domain.gh552");
String propertyKey = "notAName";
Map<String, Object> properties = Collections.singletonMap(propertyKey, "NOT A NAME!!!");

SingleUseEntityMapper entityMapper = new SingleUseEntityMapper(metaData, new ReflectionEntityInstantiator(metaData));
SingleUseEntityMapper entityMapper = new SingleUseEntityMapper(metaData,
new ReflectionEntityInstantiator(metaData));
Thing thing = entityMapper.map(Thing.class, properties);
assertThat(thing.getNotAName()).isEqualTo(properties.get(propertyKey));
}
Expand Down
45 changes: 45 additions & 0 deletions test/src/test/java/org/neo4j/ogm/domain/gh551/ThingWIthId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2002-2019 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.ogm.domain.gh551;

/**
* @author Michael J. Simons
*/
public class ThingWIthId {

private Long id;

private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

0 comments on commit 8c47d6b

Please sign in to comment.