繁体   English   中英

在Spring MVC中使用子项的ID查询多条关系

[英]Query a manytomany relationship using ID of child item in Spring MVC

请我想检索用于教室的通知列表,其中教室是通知实体中的列表项目。 请参阅下面的我的片段:

@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]

请我如何实现此目的:获取特定课程的所有通知。

谢谢

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM