我尝试将Spring查询与命名实体图一起用于方法getAll,我收到以下错误:
2016-05-24T12:12:26,575 WARN [RMI TCP Connection(3)-127.0.0.1]: nnotationConfigWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rentedServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ro.ubb.stcatalog.core.repository.GraphRepo ro.ubb.stcatalog.core.service.RentedServiceImpl.rentedRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'graphRepo': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List ro.ubb.stcatalog.core.repository.GraphRepo.findAllClient(java.lang.Integer)!
2016-05-24T12:12:26,581 ERROR [RMI TCP Connection(3)-127.0.0.1]: ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rentedServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ro.ubb.stcatalog.core.repository.GraphRepo ro.ubb.stcatalog.core.service.RentedServiceImpl.rentedRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'graphRepo': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List ro.ubb.stcatalog.core.repository.GraphRepo.findAllClient(java.lang.Integer)!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) [spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) [spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4732) [catalina.jar:9.0.0.M4]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5194) [catalina.jar:9.0.0.M4]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152) [catalina.jar:9.0.0.M4]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726) [catalina.jar:9.0.0.M4]
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:702) [catalina.jar:9.0.0.M4]
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) [catalina.jar:9.0.0.M4]
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1702) [catalina.jar:9.0.0.M4]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_31]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_31]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_31]
at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_31]
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) [tomcat-coyote.jar:9.0.0.M4]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) [?:1.8.0_31]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) [?:1.8.0_31]
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482) [catalina.jar:9.0.0.M4]
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431) [catalina.jar:9.0.0.M4]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_31]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_31]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_31]
at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_31]
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) [tomcat-coyote.jar:9.0.0.M4]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) [?:1.8.0_31]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) [?:1.8.0_31]
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1466) [?:1.8.0_31]
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) [?:1.8.0_31]
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1307) [?:1.8.0_31]
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1399) [?:1.8.0_31]
我的课程是租来的,看起来像这样:
@Entity
@Table(name="rented")
@NamedEntityGraphs({
@NamedEntityGraph(name = "rentedMovieClient", attributeNodes = {
@NamedAttributeNode("client"),
@NamedAttributeNode("movie")
})
})
public class Rented implements Serializable{
@Id
@TableGenerator(name = "TABLE_GENERATOR", initialValue = 0, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.TABLE, generator = "TABLE_GENERATOR")
@Column(unique = true, nullable = false)
private Integer id;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "client_id")
private Client client;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "movie_id")
private Movie movie;
public Rented() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public Movie getMovie() {
return movie;
}
public void setMovie(Movie movie) {
this.movie = movie;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Rented rented = (Rented) o;
// if (!id.equals(rented.id)) return false;
if (!client.equals(rented.client)) return false;
return movie.equals(rented.movie);
}
@Override
public int hashCode() {
int result = id.hashCode();
// result = 31 * result + client.hashCode();
// result = 31 * result + movie.hashCode();
return result;
}
}
我的repo界面如下所示:
public interface GraphRepo extends JpaRepository<Rented, Integer>, CustomRepo{
@Query("select r from Rented r")
@EntityGraph(value = "rentedMovieClient", type = EntityGraph.EntityGraphType.LOAD)
List<Rented> findAllS();
@Query("select r from Rented r where r.movie.id = ?1")
@EntityGraph(value = "rentedMovieClient", type = EntityGraph.EntityGraphType.LOAD)
List<Rented> findAllMovie(Integer movieId);
@Query("select r from Rented r where r.client.cnp = ?1")
@EntityGraph(value = "rentedMovieClient", type = EntityGraph.EntityGraphType.LOAD)
List<Rented> findAllClient(Integer clientId);
}
我在网上看到查询或命名约定可能存在问题,但我发现它没有任何问题。有任何想法吗?我也有一个repo的实现,但它与其他东西,而不是相关。数据库有问题吗?