最近用上springboot3.X版本的gradle项目,然后引入了spring-data-neo4j,看了一下这个jpa的版本是7.x左右的,之前用的4.x版本,实体和关系类是分开查询的,现在的注解变化好大,有一个问题:我查询不到同标签之间实体的关系了,neo4j会报错,不知道是代码问题还是什么原因,上图:
pojo:
/**
* 人物实体类
*/
@Node(primaryLabel = "Person")
@Data
public class Person implements Serializable {
@Id
@GeneratedValue
private Long id;
@Property(name = "name")
private String name;
@Property(name = "age")
private Integer age;
@Relationship(type = "Relation",direction = Relationship.Direction.OUTGOING)
private List<Relation> relations;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
}
/**
* 关系类
*/
@RelationshipProperties
@Data
public class Relation implements Serializable {
@RelationshipId
Long id;
@TargetNode
private Person person;
@Property(name = "relationship")
private String relationShip;
public Relation(Person person, String relationShip) {
this.person = person;
this.relationShip = relationShip;
}
}
dao层:
@Repository
public interface PersonRepository extends Neo4jRepository<Person,Long> {
/**
* 根据name查询
* @param name
* @return
*/
Person searchByName(String name);
}
@Repository
public interface RelationRepository extends Neo4jRepository<Relation, Long> {
}
新增都还没问题:
//新增
@Test
public void save() {
Person p1 = new Person("吕布", 18);
Person p2 = new Person("貂蝉", 18);
p1.setRelations(List.of(new Relation(p2, "丈夫"), new Relation(p2, "蓝颜")));
p2.setRelations(List.of(new Relation(p1, "夫人"), new Relation(p1, "红颜")));
personRepository.save(p1);
personRepository.save(p2);
}
//查询
@Test
public void update() {
Person person = personRepository.searchByName("吕布");
System.out.println();
}
引用 皆我百晓生 小程序回复内容作答:
根据报错信息看,这是由于查询方法中使用的参数类型不匹配导致的。在方法参数中使用了一个包装类型,而实际传入的是一个数组。为了解决这个问题,你可以尝试将方法参数类型改为数组或集合类型。
具体来说,你可以将PersonRepository中的searchByName方法的参数类型从String改为String[]或List,然后将查询方法的参数改为一个包含需要查询的name的数组或集合。
例如:
@Repository
public interface PersonRepository extends Neo4jRepository<Person,Long> {
/**
* 根据name查询
* @param names
* @return
*/
Person searchByName(String[] names);
}
然后在查询方法中传入一个包含需要查询的name的数组即可:
@Test
public void update() {
Person person = personRepository.searchByName(new String[]{"吕布"});
System.out.println();
}
这样应该就能够成功查询到同标签之间实体的关系了。
【以下回答由 GPT 生成】
我想了解以下几个问题: 1. 报错信息是什么? 2. 在查询同标签的实体关系时,您是如何进行查询的? 3. 你的具体需求是什么?
请提供以上的信息,以便我能够更准确地帮助您解决问题。