我在项目中使用Spring数据JPA。当我要求一份特定导演制作的电影列表时,我得到:
javax.persistence.NonUniqueResultException: query did not return a unique result: 11 at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:128) ~[hibernate-core-5.6.1.Final.jar:5.6.1.Final] at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1672) ~[hibernate-core-5.6.1.Final.jar:5.6.1.Final] at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getSingleResult(CriteriaQueryTypeQueryAdapter.java:111) ~[hibernate-core-5.6.1.Final.jar:5.6.1.Final] at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:198) ~[spring-data-jpa-2.6.0.jar:2.6.0] at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:90) ~[spring-data-jpa-2.6.0.jar:2.6.0] at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:155) ~[spring-data-jpa-2.6.0.jar:2.6.0] at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:143) ~[spring-data-jpa-2.6.0.jar:2.6.0] at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) ~[spring-data-commons-2.6.0.jar:2.6.0] at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) ~[spring-data-commons-2.6.0.jar:2.6.0] at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:159) ~[spring-data-commons-2.6.0.jar:2.6.0] at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:138) ~[spring-data-commons-2.6.0.jar:2.6.0] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.13.jar:5.3.13] at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) ~[spring-data-commons-2.6.0.jar:2.6.0] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.13.jar:5.3.13] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.13.jar:5.3.13] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.13.jar:5.3.13] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.13.jar:5.3.13] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.13.jar:5.3.13] at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ~[spring-tx-5.3.13.jar:5.3.13] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.13.jar:5.3.13] at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:145) ~[spring-data-jpa-2.6.0.jar:2.6.0] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.13.jar:5.3.13] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.13.jar:5.3.13] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.13.jar:5.3.13] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.13.jar:5.3.13] at com.sun.proxy.$Proxy113.findDirectorsByDirectorId(Unknown Source) ~[na:na] at net.yukitteru.what_to_watch.controller.MovieController.showMovies(MovieController.java:33) ~[main/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.13.jar:5.3.13] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.13.jar:5.3.13] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.13.jar:5.3.13] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.13.jar:5.3.13] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.13.jar:5.3.13] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.13.jar:5.3.13] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.13.jar:5.3.13] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.13.jar:5.3.13] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.13.jar:5.3.13] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.13.jar:5.3.13] at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) ~[tomcat-embed-core-9.0.55.jar:4.0.FR] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.13.jar:5.3.13] at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.55.jar:4.0.FR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.55.jar:9.0.55] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.13.jar:5.3.13] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.13.jar:5.3.13] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.13.jar:5.3.13] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.13.jar:5.3.13] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.13.jar:5.3.13] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.13.jar:5.3.13] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:895) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1722) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.55.jar:9.0.55] at java.base/java.lang.Thread.run(Thread.java:829) ~[na:na]
电影拍摄地点:
@Repository
public interface MovieRepository extends JpaRepository<Movies, Integer> {
List<Movies> findAllByDirectorsById(Directors director);
}
董事报告人:
@Repository
public interface DirectorRepository extends JpaRepository<Directors, Integer> {
Directors findDirectorsByDirectorId(Persons directorId);
}
人员存储库:
@Repository
public interface PersonRepository extends JpaRepository<Persons, Integer> {
Persons findPersonsByName(String name);
}
电影控制器:
@Controller
@RequestMapping("/movies")
public class MovieController {
private final DirectorRepository directorRepository;
private final MovieRepository movieRepository;
private final PersonRepository personRepository;
@Autowired
public MovieController(DirectorRepository directorRepository, MovieRepository movieRepository, PersonRepository personRepository) {
this.directorRepository = directorRepository;
this.movieRepository = movieRepository;
this.personRepository = personRepository;
}
@GetMapping("")
public List<Movies> showMovies() {
List<Movies> m = movieRepository.findAllByDirectorsById(
directorRepository.findDirectorsByDirectorId(
personRepository.findPersonsByName("Mel Brooks")));
return m;
}
}
我不明白为什么当sql查询一切正常工作时,但是当使用表达式搜索存储库时,它却不能正常工作。在阅读了类似的问题后,我没有找到任何解决我问题的东西。
董事实体:
@Entity
@NoArgsConstructor
@Setter
@EqualsAndHashCode
public class Directors {
private Integer id;
private Movies movieId;
private Persons directorId;
@Id
@Column(name = "id", nullable = false)
public Integer getId() {
return id;
}
@ManyToOne
@JsonIgnore
@JoinColumn(name = "movie_id", referencedColumnName = "id")
public Movies getMovieId() {
return movieId;
}
@ManyToOne
@JsonIgnore
@JoinColumn(name = "director_id", referencedColumnName = "id")
public Persons getDirectorId() {
return directorId;
}
}
个人实体:
@Entity
@NoArgsConstructor
@Setter
@EqualsAndHashCode
public class Persons {
private Integer id;
private String name;
private Set<Directors> directorsById;
private Set<Actors> actorsById;
@Id
@Column(name = "id", nullable = false)
public Integer getId() {
return id;
}
@Basic
@Column(name = "name", nullable = true, length = -1)
public String getName() {
return name;
}
public void Name(String name) {
this.name = name;
}
@OneToMany(mappedBy = "directorId")
public Set<Directors> getDirectorsById() {
return directorsById;
}
@OneToMany(mappedBy = "personId")
public Set<Actors> getActorsById() {
return actorsById;
}
}
电影实体:
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Setter
@EqualsAndHashCode
public class Movies {
private Integer id;
private String title;
private String releaseDate;
private Long budget;
private Long revenue;
private Double popularity;
private Integer runtime;
private Double rating;
private String overview;
private Languages originalLanguage;
private Collections belongsToCollection;
private Set<MoviesGenres> moviesGenresById;
private Set<Directors> directorsById;
private Set<Actors> actorsById;
private Set<MoviesKeywords> moviesKeywordsById;
private Set<SpokenLanguages> spokenLanguagesById;
private Set<ProductionCountries> productionCountriesById;
private Set<MoviesProductionCompanies> moviesProductionCompaniesById;
@Id
@Column(name = "id", nullable = false)
public Integer getId() {
return id;
}
@Basic
@Column(name = "title", nullable = true, length = -1)
public String getTitle() {
return title;
}
@Basic
@Column(name = "release_date", nullable = true, length = -1)
public String getReleaseDate() {
return releaseDate;
}
@Basic
@Column(name = "budget", nullable = true)
public Long getBudget() {
return budget;
}
@Basic
@Column(name = "revenue", nullable = true)
public Long getRevenue() {
return revenue;
}
@Basic
@Column(name = "popularity", nullable = true, precision = 0)
public Double getPopularity() {
return popularity;
}
@Basic
@Column(name = "runtime", nullable = true)
public Integer getRuntime() {
return runtime;
}
@Basic
@Column(name = "rating", nullable = true, precision = 0)
public Double getRating() {
return rating;
}
@Basic
@Column(name = "overview", nullable = true, length = -1)
public String getOverview() {
return overview;
}
@ManyToOne
@JsonIgnore
@JoinColumn(name = "original_language", referencedColumnName = "id")
public Languages getOriginalLanguage() {
return originalLanguage;
}
@ManyToOne
@JsonIgnore
@JoinColumn(name = "belongs_to_collection", referencedColumnName = "id")
public Collections getBelongsToCollection() {
return belongsToCollection;
}
@OneToMany(mappedBy = "movieId")
public Set<MoviesGenres> getMoviesGenresById() {
return moviesGenresById;
}
@OneToMany(mappedBy = "movieId")
public Set<Directors> getDirectorsById() {
return directorsById;
}
@OneToMany(mappedBy = "movieId")
public Set<Actors> getActorsById() {
return actorsById;
}
@OneToMany(mappedBy = "movieId")
public Set<MoviesKeywords> getMoviesKeywordsById() {
return moviesKeywordsById;
}
@OneToMany(mappedBy = "movieId")
public Set<SpokenLanguages> getSpokenLanguagesById() {
return spokenLanguagesById;
}
@OneToMany(mappedBy = "movieId")
public Set<ProductionCountries> getProductionCountriesById() {
return productionCountriesById;
}
@OneToMany(mappedBy = "movieId")
public Set<MoviesProductionCompanies> getMoviesProductionCompaniesById() {
return moviesProductionCompaniesById;
}
}
问题是一个人
可以是多个导演
(也就是说,导演了很多电影)。
因此,DirectorRepository
是错误的:
@Repository
public interface DirectorRepository extends JpaRepository<Directors, Integer> {
Directors findDirectorsByDirectorId(Persons directorId);
}
这意味着一个人
最多只能是一部电影的导演
。
如果人
是不止一部电影的导演
,该方法应该返回什么?它应该随机选择一个吗?那不是很有用!这会让你想知道:为什么MoviRepository.findAllByDirectorsById()
只返回一部电影?
这就是为什么决定找到比它可以返回的结果更多的查询方法会抛出NonUniqueResultException
。
如何修复它?您需要声明findDirectorsByDirectorId()
方法,以便它可以返回多个导演
:
@Repository
public interface DirectorRepository extends JpaRepository<Directors, Integer> {
List<Directors> findDirectorsByDirectorId(Persons directorId);
}
反过来,您需要声明方法MovieRepository。findAllByDirectorsById()
,以便它可以接受多个控制器
:
@Repository
public interface MovieRepository extends JpaRepository<Movies, Integer> {
List<Movies> findAllByDirectorsIn(List<Directors> director);
}
PERERSRespository.findByPERosnName()有多个结果,即一个列表,因为该方法的返回类型不是列表,这就是导致问题的原因。
检查数据库,Persons表中必须有更多同名数据。
尝试创建一个方法人员列表查找人员ByNameList(字符串名称)。检查这是否实际上返回一个只有1个值的具有相同参数的列表。
我正在对一个小表执行一个简单的查询 系统表只有三列(Id、Name、Progress)和1300行。 我获取数据的代码是: 这段代码在JTable中显示日期大约需要15秒,而如果在phpmyadmin中执行查询,则需要不到1秒。
问题内容: 我正在尝试向表中插入一些行…我正在使用 postgressql-7.2.jar。 我得到以下异常 org.postgresql.util.PSQLException:查询未返回任何结果。 在org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:255) 我已经用Googl
这里有一个名为的小本体,由Protégé创建,其中我有类、、、和个体()、()和(也是)。在本体论中,我只声明。 当我在Protégé的DL Query选项卡中请求的实例时,我得到的是(它是,因此是,因此是)。 但当我编写以下SPARQ查询时: 我没有任何例子。当我将替换为时,结果是相同的。只有当我用替换它时,我才会得到所需的结果。 为什么DL查询做推断(允许我获得作为类的实例),而不是SPARQ
Spring Data repository中的查询可以异步执行,参考Spring执行异步方法。这意味着方法可以在被调用时立刻返回,而真正的查询执行会被当做一个任务提交到Spring的TaskExecutor。 @Async Future<User> findByFirstname(String firstname); //1 @Async CompletableFu
要运行查询,在工具栏中点击 运行 。如果查询语句是正确的,该查询将被运行,如果该查询应该返回数据,结果 选项卡会打开查询返回的数据。如果运行查询时发生错误,运行停止,显示相应的错误信息。 结果 选项卡以网格显示查询返回的结果数据。数据可以用两种模式显示:网格查看 和 表单查看。详细信息请看表查看 器。 提示:Navicat 支持返回 10 个结果集。 注意:你可以通过选择 查看 -> 显示结果 -
你可以在任何的服务器运行查询。在工具栏的下拉式列表中选择目标服务器和数据库。然后点击 “运行”。如果该查询语句是正确的,它将被运行。如果该查询应该返回数据,“结果”选项卡会打开并显示返回的数据。如果运行查询时发生错误,运行会停止并显示相应的错误信息。 “结果”选项卡以网格显示查询返回的结果数据。数据可以用三种模式显示:网格视图、树视图和 JSON 视图。详细信息请参阅数据查看器。 【注意】Navi