簡體   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