neo4j ::获取java.lang.IllegalArgumentException:类类com.my.domain.Actor不是有效的实体类。请检查实体映射

时间:2018-02-14 06:42:50

标签: java neo4j neo4j-ogm

我正在努力学习Neo4j-OGM(版本:3.1.0)。但是我坚持这个Exception。甚至尝试使用旧版本但没有用。并用Google搜索帮助但找不到任何东西.Neo4j是唯一的我的项目的选择。我发现我的代码没有任何问题。谁能帮我这个?谢谢:)这是我的代码..抱歉这个冗长的问题。

@NodeEntity(label="Film")
public class Movie {
    @GraphId
    Long id;
    @Property(name="title")
    private String name;
    public Movie(String name){
        this.name=name;
    }
    public Movie(){}       
}

@NodeEntity
public class Actor  {
    @Id
    @GeneratedValue
    private Long id;
    @Property(name="name")
    private String fullName;

    @Relationship(type="ACTED_IN", direction=Relationship.OUTGOING)
    private List<Role> filmography;

    public Actor(String name){
        this.fullName=name;
        this.filmography=new ArrayList<>();
    }
    public Actor(){}
    public void addRole(Role r){
        this.filmography.add(r);
    }        
}

@RelationshipEntity(type="ACTED_IN")
public class Role {
    @Id @GeneratedValue  private Long relationshipId;
    @Property            private String title;
    @StartNode           private Actor actor;
    @EndNode             private Movie movie;
    public Role(){}
    public Role(String title,Actor actor,Movie movie){
        this.actor=actor;
        this.title=title;
        this.movie=movie;
    }
}
public class Main{
   public static void main(String[] a){
        Movie m1=new Movie("M1");
        Actor a1=new Actor("A1");
        Actor a2=new Actor("A2");
        Movie m2=new Movie("M2");
        Role r1=new Role("R1",a1,m1);
        Role r2=new Role("R2",a2,m1);
        Role r3=new Role("R3",a2,m2);
        a1.addRole(r1);
        a2.addRole(r2);
        a2.addRole(r3);
        Configuration configuration = new Configuration.Builder()
            .uri("bolt://localhost")
            .credentials("neo4j", "admin")
            .build();
        SessionFactory sessionFactory = new SessionFactory(configuration, "com.my.domain");
        Session session=sessionFactory.openSession();
        session.beginTransaction();
        session.save(a1);
        session.save(a2);
 }
}

2 个答案:

答案 0 :(得分:0)

尝试检查类角色的bean是否在spring上下文中正确加载。如果由于错误的配置(例如:@EntityScan未使用正确的路径定义)在运行时上下文中不存在该变量,则可能会发生此异常。

答案 1 :(得分:0)

在SessionFactory中检查软件包扫描,例如。新的SessionFactory(配置,“ com.my.domain”);

如果声明的包不是您的实体包,则也会发生此错误