Я использую neo4j-spring-boot в своем проекте и заметил, что только некоторые объекты привязаны к базе данных neo4j. Например, у меня есть строка и класс сайта:
@NodeEntity(label = "Line")
public class Line {
@Id
@GeneratedValue
private Long lineid;
private String lineserial;
private float linelength;
private String linename;
public Line() {
}
public Line(String lineserial, String linename, float linelength) {
this.lineserial = lineserial;
this.linelength = linelength;
this.linename = linename;
}
public Long getLineId() {
return lineid;
}
public void setLineId(Long lineid) {
this.lineid = lineid;
}
public String getLineSerial() {
return lineserial;
}
public void setLineSerial(String lineserial) {
this.lineserial = lineserial;
}
public float getLength() {
return linelength;
}
public void setLineLength(int linelength) {
this.linelength = linelength;
}
public String getLineName() {
return linename;
}
public void setLineName(String linename) {
this.linename = linename;
}
@Relationship(type = "Connect_To", direction = Relationship.OUTGOING)
private List<Site> cSites;
public List<Site> getSites() {
return cSites;
}
public void addConnectSite(Site site) {
if (this.cSites == null) {
this.cSites = new ArrayList<>();
}
this.cSites.add(site);
}
@Relationship(type = "Cross_With", direction = Relationship.UNDIRECTED)
private List<Line> cLines;
public List<Line> getLines() {
return cLines;
}
public void addCrossLine(Line line) {
if (this.cLines == null) {
this.cLines = new ArrayList<>();
}
this.cLines.add(line);
}
}
@NodeEntity(label = "Site")
public class Site {
@Id
@GeneratedValue
private long siteid;
private String sitename;
public Site() {
}
public Site(String sitename) {
this.sitename = sitename;
}
public long getSiteId() {
return siteid;
}
public void setSiteId(Long siteid) {
this.siteid = siteid;
}
public String getSiteName() {
return sitename;
}
public void setSiteName(String name) {
this.sitename = name;
}
}
и репозитории
@Repository
public interface LineRepository extends Neo4jRepository<Line, Long> {
Line findByLineserial(@Param("lineserial") String lineserial);
Collection<Line> findByLinenameLike(@Param("linename") String linename);
@Query("MATCH (l:Line)-[r:Connect_To]->(S:Site) RETURN l,r,s LIMIT {limit}")
Collection<Line> graphConnectToSite(@Param("limit") int limit);
@Query("MATCH (11:Site)-[r:Cross_With]-(lr:Line) RETURN ll,r,lr LIMIT {limit}")
Collection<Line> graphCrossWithLine(@Param("limit") int limit);
}
@Repository
public interface SiteRepository extends Neo4jRepository<Site, Long> {
Site findBySitename(@Param("sitename") String sitename);
}
и сервисы
@Service
public class LineService {
private final static Logger LOG = LoggerFactory.getLogger(LineService.class);
@Autowired
private LineRepository lineRepository;
@Transactional(readOnly = true)
public Line findByTitle(String serial_number) {
Line result = lineRepository.findByLineserial(serial_number);
return result;
}
@Transactional(readOnly = true)
public Collection<Line> findByLineNameLike(String name) {
Collection<Line> result = lineRepository.findByLinenameLike(name);
return result;
}
@Transactional
public Line SaveLine(String serial_number, String line_name, float length) {
if (lineRepository.findByLineserial(serial_number) != null) {
return null;
}
Line line = new Line(serial_number, line_name, length);
return lineRepository.save(line);
}
}
@Service
public class SiteService {
private final static Logger LOG = LoggerFactory.getLogger(SiteService.class);
@Autowired
private SiteRepository siteRepository;
@Transactional(readOnly = true)
public Site findBySiteName(String site_name) {
Site result = siteRepository.findBySitename(site_name);
return result;
}
@Transactional
public Site SaveSite(String site_name) {
if (siteRepository.findBySitename(site_name) != null) {
System.out.print(site_name + "is already exist");
return null;
}
Site site = new Site(site_name);
//System.out.print(site_name+ siteRepository.save(site).getSiteId() + "is added successfully");
Site result = siteRepository.save(site);
return result;
}
}
Я использовал RESTful для добавления узлов, потом обнаружил, что в базу данных добавляются только строки, а сайтов нет. И если я создаю узел сайта вБаза данных neo4j, я не могу найти ее с помощью функции поиска по сайту. Вот журнал.
2019-11-06 17:19:33.147 INFO 2848 --- [nio-8080-exec-3] o.n.o.drivers.bolt.request.BoltRequest : Request: MATCH (n:`Site`) WHERE n.`sitename` = { `sitename_0` } WITH n RETURN n, ID(n) with params {sitename_0=site1}
2019-11-06 17:19:33.159 INFO 2848 --- [nio-8080-exec-3] o.n.o.drivers.bolt.request.BoltRequest : Request: UNWIND {rows} as row MATCH (n) WHERE ID(n)=row.nodeId SET n:`Site` SET n += row.props RETURN row.nodeId as ref, ID(n) as id, {type} as type with params {type=node, rows=[{nodeId=0, props={sitename=site1}}]}
2019-11-06 17:24:10.949 INFO 2848 --- [nio-8080-exec-9] o.n.o.drivers.bolt.request.BoltRequest : Request: MATCH (n:`Line`) WHERE n.`lineserial` = { `lineserial_0` } WITH n RETURN n,[ [ (n)-[r_c1:`Connect_To`]->(s1:`Site`) | [ r_c1, s1 ] ], [ (n)-[r_c1:`Cross_With`]-(l1:`Line`) | [ r_c1, l1 ] ] ], ID(n) with params {lineserial_0=1254}
2019-11-06 17:24:10.954 INFO 2848 --- [nio-8080-exec-9] o.n.o.drivers.bolt.request.BoltRequest : Request: UNWIND {rows} as row CREATE (n:`Line`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, {type} as type with params {type=node, rows=[{nodeRef=-11, props={linelength=11.2, linename=ABAB, lineserial=1254}}]}
Я заметил, что для линий пружина использовала CREATE, в то время как для сайтов использовали MATCH. Однако они оба использовали функцию save из Neo4jRepository.
Будет очень полезно, если кто-нибудь скажет мне, почему.