diff --git a/core/src/main/java/org/neo4j/ogm/metadata/MetaData.java b/core/src/main/java/org/neo4j/ogm/metadata/MetaData.java index 6ccfffed1..6dfdf19d0 100644 --- a/core/src/main/java/org/neo4j/ogm/metadata/MetaData.java +++ b/core/src/main/java/org/neo4j/ogm/metadata/MetaData.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.neo4j.ogm.annotation.NodeEntity; import org.neo4j.ogm.annotation.RelationshipEntity; @@ -204,6 +205,13 @@ public ClassInfo resolve(String... taxa) { } } if (resolved.size() > 1) { + // if all resolved classes are found in the taxa, and only one of them is a concrete class, we can assume + // that one matches the thing to be loaded + var interfacesAndClasses = resolved.stream().collect(Collectors.partitioningBy(ClassInfo::isInterface)); + if (interfacesAndClasses.get(false).size() == 1) { + return interfacesAndClasses.get(false).get(0); + } + // Sort so we always get the same order String[] sorted = Arrays.copyOf(taxa, taxa.length); Arrays.sort(sorted); diff --git a/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Canvas.java b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Canvas.java new file mode 100644 index 000000000..23957fa9a --- /dev/null +++ b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Canvas.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2002-2024 "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.gh1156; + +import java.io.Serializable; +import java.util.Collection; +import org.neo4j.ogm.annotation.NodeEntity; + +@NodeEntity +public interface Canvas extends Serializable, UObject { + + public void setSuid(java.lang.String suid); + + public String getSuid(); + + public void setCanvasName(java.lang.String canvasName); + + public String getCanvasName(); + +} diff --git a/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/UObject.java b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/UObject.java new file mode 100644 index 000000000..5bfe41243 --- /dev/null +++ b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/UObject.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2002-2024 "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.gh1156; + +import java.io.Serializable; +import org.neo4j.ogm.annotation.NodeEntity; + +@NodeEntity +public interface UObject extends Serializable { + + public void setSuid(java.lang.String suid); + + public String getSuid(); + + public void setObjName(java.lang.String objName); + + public String getObjName(); +} diff --git a/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Window.java b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Window.java new file mode 100644 index 000000000..632f8621a --- /dev/null +++ b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1156/Window.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2002-2024 "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.gh1156; + +import java.io.Serializable; +import org.neo4j.ogm.annotation.Id; +import org.neo4j.ogm.annotation.GeneratedValue; +import org.neo4j.ogm.annotation.Version; +import org.neo4j.ogm.annotation.Property; +import org.neo4j.ogm.annotation.NodeEntity; + +@NodeEntity +public class Window implements Serializable, Canvas { + + private static final long serialVersionUID = 1L; + + private String suid_ = java.util.UUID.randomUUID().toString(); + + public Class getTypeClazz() { + return Window.class; + } + + @Id @GeneratedValue public Long __id; + + @Version + @Property(name = "V_WINDOW") + private long vWindow; + + public long getVwindow() { + return vWindow; + } + + public void setVwindow(long vWindow) { + this.vWindow = vWindow; + } + + @Property(name = "UID") + private String uid; + + public void setUid(java.lang.String uid) { + this.uid = uid; + } + + public String getUid() { + return uid; + } + + @Property(name = "NAME") + private String name; + + public void setName(java.lang.String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setSuidCanvas(java.lang.String suid) { + this.suid_ = suid; + } + + public String getSuidCanvas() { + return this.suid_; + } + + public void setSuid(java.lang.String suid) { + this.suid_ = suid; + } + + public String getSuid() { + return this.suid_; + } + + @Property(name = "CANVAS_NAME") + private String canvasName; + + public void setCanvasName(java.lang.String canvasName) { + this.canvasName = canvasName; + } + + public String getCanvasName() { + return canvasName; + } + + public void setSuidUObject(java.lang.String suid) { + this.suid_ = suid; + } + + public String getSuidUObject() { + return this.suid_; + } + + @Property(name = "OBJ_NAME") + private String objName; + + public void setObjName(java.lang.String objName) { + this.objName = objName; + } + + public String getObjName() { + return objName; + } +} diff --git a/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/persistence/examples/pizza/PizzaIntegrationTest.java b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/persistence/examples/pizza/PizzaIntegrationTest.java index b5212719f..1a2109a75 100644 --- a/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/persistence/examples/pizza/PizzaIntegrationTest.java +++ b/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/persistence/examples/pizza/PizzaIntegrationTest.java @@ -31,6 +31,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.neo4j.ogm.domain.gh1156.Window; import org.neo4j.ogm.domain.pizza.*; import org.neo4j.ogm.exception.core.MappingException; import org.neo4j.ogm.model.Result; @@ -359,6 +360,20 @@ void shouldRaiseExceptionWhenAmbiguousClassLabelApplied() { } } + @Test // GH-1156 + void multipleLabelsButOnlyOneConcreteClassShouldNotFail() { + Session sessionWithAmbiguousDomain = new SessionFactory(getDriver(), "org.neo4j.ogm.domain.gh1156") + .openSession(); + + Window window = new Window(); + + sessionWithAmbiguousDomain.save(window); + sessionWithAmbiguousDomain.clear(); + + var loaded = sessionWithAmbiguousDomain.load(Window.class, window.__id); + assertThat(loaded).isNotNull(); + } + @Test void shouldUpdateSessionContextAfterSaveForSingleAndMultiStatementCypherQueries() {