neo4j ogm使用接口

时间:2017-03-26 22:16:11

标签: neo4j-ogm

我正在使用neo4j ogm版本2.1.1,并且在建模两个相同类型的类之间的关系时收到以下错误:

AmbiguousBaseClassException:在类型层次结构中找到的多个类映射到:[Package,Namespace,TypedElement,PackageableElement,NamedElement,Element,NetworkNode]

我的方案是我有多个实现PackageableElement接口的类,例如包和类。 PackageableElement的关系可以有一个或多个PackageableElements出口。

EG。包中包含许多类。这种关系似乎仍然存在,但在检索时会抛出上面的错误。

阅读文档,如果我理解了“关系模糊”问题,我在这种情况下修改了与Package的关系,使用“UNDIRECTED”类型,以便关系可以在两个方向上导航。这还没有解决问题。我在下面粘贴了3个域名对象来显示我的关系。

该结构是一个继承自Namespace的Package,它实现了PackageableElement接口,该接口具有方法和注释: @ org.neo4j.ogm.annotation.Relationship(type =“HasPackageableElement”,direction = org.neo4j.ogm.annotation.Relationship.UNDIRECTED)     public Set getPackageableElements();

包实体

@NodeEntity
@JsonTypeName("Package")
public class Package extends Namespace {

public String getURI() {
    return URI;
}

public void setURI(String URI) {
    this.URI = URI;
}

String URI;

@Override
public Integer getSequence() {
    return sequence;
}

@Override
public void setSequence(Integer sequence) {
    this.sequence = sequence;
}

@Index
Integer sequence;

}

命名空间类

@NodeEntity
public class Namespace implements PackagebleElement {

@Override
public Long getId() {
    return id;
}

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

@GraphId
private Long id;

@Override
public Set<Comment> getComments() {
    return comments;
}

@Override
public void setComments(Set<Comment> comments) {
    this.comments = comments;
}

private Package owningPackage;

@Override
public Package getOwningPackage() {
    return owningPackage;
}

@org.neo4j.ogm.annotation.Relationship(type = "HAS_COMMENT", direction = org.neo4j.ogm.annotation.Relationship.OUTGOING)
Set<Comment> comments;

@Override
public String getName() {
    return name;
}

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

@Index
private String name;

@Override
public VisibilityKindEnum getVisibility() {
    return visibility;
}

@Override
public void setVisibility(VisibilityKindEnum visibility) {
    this.visibility = visibility;
}

private VisibilityKindEnum visibility;

Object type;

@Override
public Object getType() {
    return type;
}

@Override
public void setType(Object type) {
    this.type = type;
}

@Override
public Integer getSequence() {
    return sequence;
}

@Override
public void setSequence(Integer sequence) {
    this.sequence = sequence;
}

@Index
Integer sequence;

public static enum ElementTypeEnum {

    Class,
    Package;
}

@Override
public Set<PackageableElement> getPackageableElements() {
    if (packageableElements == null)
        packageableElements = new HashSet<PackageableElement>();
    return packageableElements;
}

@org.neo4j.ogm.annotation.Relationship(type = "HasPackageableElement", direction = Relationship.UNDIRECTED)
public void setPackageableElements(Set<PackageableElement> packageableElements) {
    this.packageableElements = packageableElements;
}

@org.neo4j.ogm.annotation.Relationship(type = "HasPackageableElement", direction = Relationship.UNDIRECTED)
Set<PackageableElement> packageableElements;
}

PackageableElement类

@NodeEntity
public interface PackageableElement extends NamedElement {

@org.neo4j.ogm.annotation.Relationship(type = "HasPackageableElement", direction = org.neo4j.ogm.annotation.Relationship.UNDIRECTED)
public Set<PackageableElement> getPackageableElements();

public Integer getSequence();

public void setSequence(Integer sequence);

}

我在ogm中调试了MetaData类的resolve方法,这对我来说似乎是个错误。阅读的行 if(taxonClassInfo.isInterface()){                     LOGGER.debug(“标签位于接口上。寻找单个实现类......”);                     taxonClassInfo = findSingleImplementor(taxon);

findSingleImplementor方法返回结果,其中我有一个扩展另一个接口的接口。在我的场景中,NamedElement接口扩展了Element接口。然后添加&gt; 1个已解析数组的条目,然后抛出错误。

这是正确的行为吗?我以为它应该寻找具体的实现。

1 个答案:

答案 0 :(得分:0)

在我的工作项目中进行一些重构之后,我使用neo4j-ogm版本2.1.2进行了类似的异常AmbiguousBaseClassException

我发现对于我的特定问题,扫描了具体的类,但没有扫描泛型类,即当我创建SessionFactory时,我列出的包不包含基类。

特别是我有一个名为BaseObject(基类)的类。它由具体的课程Person扩展。

添加包含BaseObject的包后,在创建SessionFactory时,异常消失了。