Я следую "Spring Data Neo4j", но у меня возникла проблема. Когда я добавляю свойство отношений в класс User.class, выдается ошибка Caused by: org.neo4j.ogm.exception.core.MappingException: Error mapping GraphModel to instance of paper.neo4j.entities.User
. Если я его уронил, программа работает нормально.
package paper.neo4j.entities;
import java.util.Set;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;
import org.neo4j.ogm.annotation.Relationship;
@NodeEntity
public class User {
public User() {}
@Id
@GeneratedValue
private Long userId;
@Property
private String name;
@Relationship(type = "RATED")
private Set<Rating> ratings;
@Relationship(type = "TAGED")
private Set<Taged> tags;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Rating> getRatings() {
return ratings;
}
public void setRatings(Set<Rating> ratings) {
this.ratings = ratings;
}
public Set<Taged> getTags() {
return tags;
}
public void setTags(Set<Taged> tags) {
this.tags = tags;
}
}
package paper.neo4j.repositories;
import java.util.Map;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import paper.neo4j.entities.User;
public interface UserRepository extends Neo4jRepository<User, Long> {
@Query("MATCH (u1:User {userId:{0}})-[r:RATED]->(m:Movie) \r\n"
+ " WITH u1, avg(r.rating) AS u1_mean \r\n"
+ "MATCH (u1)-[r1:RATED]->(m:Movie)<-[r2:RATED]-(u2) \r\n"
+ " WITH u1, u1_mean, u2, COLLECT({r1: r1, r2: r2}) AS ratings \r\n"
+ " WHERE size(ratings) > 10 \r\n"
+ "MATCH (u2)-[r:RATED]->(m:Movie) \r\n"
+ " WITH u1, u1_mean, u2, avg(r.rating) AS u2_mean, ratings \r\n"
+ "UNWIND ratings AS r \r\n"
+ "WITH \r\n"
+ " sum( (r.r1.rating-u1_mean) * (r.r2.rating-u2_mean) ) AS nom, \r\n"
+ " sqrt( \r\n"
+ " sum( (r.r1.rating - u1_mean)^2) * sum( (r.r2.rating - u2_mean) ^2)\r\n"
+ " ) AS denom, \r\n"
+ " u1, u2 \r\n"
+ "WHERE denom <> 0 \r\n"
+ "RETURN u2, nom/denom AS pearson \r\n"
+ "ORDER BY pearson DESC \r\n"
+ "LIMIT {1}")
Map<User, Double> findTopNPearsonByUserIdAndN(Long userId,Integer n);
@Query("MATCH (u:User {userId:{0}}) \r\n" +
"MATCH (u)-[r:RATED]->(m:Movie) \r\n" +
"RETURN avg(r.rating) AS average \r\n" )
Double findAvgByUserId(Long userId);
}
package paper.neo4j.controllers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import paper.neo4j.repositories.UserRepository;
@Controller
public class UserController {
@Autowired
private UserRepository userRepository;
private static final Logger logger=LoggerFactory.getLogger(UserController.class);
@RequestMapping("/user/view")
public String index(Model model,@PageableDefault(value=1,sort= {"userId"},direction=Sort.Direction.ASC) Pageable pageable) {
logger.info("user/view index comming...");
model.addAttribute("users", userRepository.findAll());
return "user/view";
}
@RequestMapping("/user/all")
public String getAll(Model model) {
logger.info("user/view getAll() comming...");
model.addAttribute("users", userRepository.findAll());
return "user/all";
}
}
package paper.neo4j.entities;
import java.util.Set;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;
import org.neo4j.ogm.annotation.Relationship;
@NodeEntity
public class Movie {
public Movie() {
}
@Id
@GeneratedValue
private Long movieId;
@Property
private String title;
@Property
private int runtime;
@Property
private String[] countries;
@Property
private int year;
@Relationship(type = "IN_GENRE")
private Set<Genre> inGenre;
@Relationship(type = "ACTED_IN", direction = Relationship.INCOMING)
private Set<Actor> actedIn;
@Relationship(type = "DIRECTED", direction = Relationship.INCOMING)
private Set<Director> directed;
@Relationship(type = "RATED", direction = Relationship.INCOMING)
private Set<Rating> rated;
@Relationship(type = "TAGED", direction = Relationship.INCOMING)
private Set<Taged> taged;
}
package paper.neo4j.entities;
import org.neo4j.ogm.annotation.EndNode;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.Property;
import org.neo4j.ogm.annotation.RelationshipEntity;
import org.neo4j.ogm.annotation.StartNode;
@RelationshipEntity("RATED")
public class Rating {
public Rating() {
}
@Id
@GeneratedValue
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Double getRating() {
return rating;
}
public void setRating(Double rating) {
this.rating = rating;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Movie getMovie() {
return movie;
}
public void setMovie(Movie movie) {
this.movie = movie;
}
@Property
private Double rating;
@Property
private Long timestamp;
@StartNode
private User user;
@EndNode
private Movie movie;
}
package paper.neo4j.entities;
import org.neo4j.ogm.annotation.EndNode;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.Property;
import org.neo4j.ogm.annotation.RelationshipEntity;
import org.neo4j.ogm.annotation.StartNode;
@RelationshipEntity("TAGED")
public class Taged {
public Taged() {
}
@Id
@GeneratedValue
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Movie getMovie() {
return movie;
}
public void setMovie(Movie movie) {
this.movie = movie;
}
@Property
private String tag;
@Property
private Long timestamp;
@StartNode
private User user;
@EndNode
private Movie movie;
}
2019-05-23 17:21:03.834 ERROR 12124 --- [nio-8086-exec-1] p.neo4j.controllers.ExceptionController : Error mapping GraphModel to instance of paper.neo4j.entities.User; nested exception is org.neo4j.ogm.exception.core.MappingException: Error mapping GraphModel to instance of paper.neo4j.entities.User
org.springframework.dao.InvalidDataAccessApiUsageException: Error mapping GraphModel to instance of paper.neo4j.entities.User; nested exception is org.neo4j.ogm.exception.core.MappingException: Error mapping GraphModel to instance of paper.neo4j.entities.User
at org.springframework.data.neo4j.transaction.SessionFactoryUtils.convertOgmAccessException(SessionFactoryUtils.java:126)
at org.springframework.data.neo4j.repository.support.Neo4jPersistenceExceptionTranslator.translateExceptionIfPossible(Neo4jPersistenceExceptionTranslator.java:31)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy421.findAll(Unknown Source)
at paper.neo4j.controllers.UserController.getAll(UserController.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.neo4j.ogm.exception.core.MappingException: Error mapping GraphModel to instance of paper.neo4j.entities.User
at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:217)
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:138)
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:94)
at org.neo4j.ogm.session.delegates.LoadByTypeDelegate.lambda$loadAll$0(LoadByTypeDelegate.java:107)
at org.neo4j.ogm.session.Neo4jSession.doInTransaction(Neo4jSession.java:574)
at org.neo4j.ogm.session.Neo4jSession.doInTransaction(Neo4jSession.java:558)
at org.neo4j.ogm.session.delegates.LoadByTypeDelegate.loadAll(LoadByTypeDelegate.java:94)
at org.neo4j.ogm.session.delegates.LoadByTypeDelegate.loadAll(LoadByTypeDelegate.java:118)
at org.neo4j.ogm.session.Neo4jSession.loadAll(Neo4jSession.java:192)
at sun.reflect.GeneratedMethodAccessor356.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:282)
at org.springframework.data.neo4j.transaction.SharedSessionCreator$SharedSessionInvocationHandler.lambda$invoke$0(SharedSessionCreator.java:108)
at org.springframework.data.neo4j.transaction.SharedSessionCreator$SharedSessionInvocationHandler.invokeInTransaction(SharedSessionCreator.java:139)
at org.springframework.data.neo4j.transaction.SharedSessionCreator$SharedSessionInvocationHandler.invoke(SharedSessionCreator.java:110)
at com.sun.proxy.$Proxy76.loadAll(Unknown Source)
at org.springframework.data.neo4j.repository.support.SimpleNeo4jRepository.findAll(SimpleNeo4jRepository.java:154)
at org.springframework.data.neo4j.repository.support.SimpleNeo4jRepository.findAll(SimpleNeo4jRepository.java:149)
at sun.reflect.GeneratedMethodAccessor355.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
... 62 more
Caused by: java.lang.IllegalArgumentException: array element type mismatch
at java.lang.reflect.Array.set(Native Method)
at org.neo4j.ogm.metadata.reflect.EntityAccessManager.merge(EntityAccessManager.java:92)
at org.neo4j.ogm.metadata.reflect.EntityAccessManager.merge(EntityAccessManager.java:56)
at org.neo4j.ogm.context.GraphEntityMapper.writeProperty(GraphEntityMapper.java:311)
at org.neo4j.ogm.context.GraphEntityMapper.setProperties(GraphEntityMapper.java:277)
at org.neo4j.ogm.context.GraphEntityMapper.mapNodes(GraphEntityMapper.java:239)
at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:212)
... 97 more
Я посетил этот URL 'http://localhost:8086/user/all',but фактический вывод - ошибка.