有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在SpringMVC中使用子项的ID查询多个关系

请我想检索一个教室的通知列表,其中教室是通知实体中的列表项。请参见下面我的代码片段:

@Entity
@Table(name="ta_notice")
public class Notifications implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long noid;

    private String title;
    private String message;
    private String messagechannel;
    private String season;
    private Date dueon;
    private Date addedon;

    @ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.MERGE, CascadeType.REFRESH})
    @JsonManagedReference
    @JsonIgnore
    private List<Classroom> classes = new ArrayList();

请参见下面的“我的存储库”:

@Repository
public interface NotificationRepo extends JpaRepository<Notifications, Long> {

    @Query("Select n from Notifications n where n.classes.crid = :classid")
    List<Notifications> classno(@Param("classid") Long classid);

}

返回的错误如下所示:

org.springframework.beans.factory.BeanCreationException: Error creating bean  with name 'notificationCon': Injection of autowired dependencies failed; nested  exception is org.springframework.beans.factory.BeanCreationException: Could not  autowire field: private net.myeverlasting.sirius.service.NotificationServ  net.myeverlasting.sirius.controllers.NotificationCon.notificationServ; nested  exception is org.springframework.beans.factory.BeanCreationException: Error  creating bean with name 'notificationServ': Injection of autowired dependencies  failed; nested exception is  org.springframework.beans.factory.BeanCreationException: Could not autowire  field: private net.myeverlasting.sirius.repositories.NotificationRepo  net.myeverlasting.sirius.service.NotificationServ.notificationRepo; nested  exception is org.springframework.beans.factory.BeanCreationException: Error  creating bean with name 'notificationRepo': Invocation of init method failed;  nested exception is java.lang.IllegalArgumentException: Validation failed for  query for method public abstract java.util.List  net.myeverlasting.sirius.repositories.NotificationRepo.classno(java.lang.Long)!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4726) [catalina.jar:8.0.20]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5162) [catalina.jar:8.0.20]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:8.0.20]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) [catalina.jar:8.0.20]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399) [catalina.jar:8.0.20]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_25]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_25]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_25]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25]

请问我如何才能达到这个目的:获取一个特定类的所有通知

谢谢


共 (1) 个答案

  1. # 1 楼答案

    如堆栈跟踪所示,您的查询无效。您需要加入才能使其有效:

    select n from Notifications n join n.classes c 
    where c.crid = :classid   
    

    您还应该将类通知重命名为Notification:该类的一个实例代表一个通知,而不是多个通知