gpt4 book ai didi

java - JPA 请求问题 : Validation failed for query for method public abstract java. util.List

转载 作者:行者123 更新时间:2023-12-04 10:31:23 29 4
gpt4 key购买 nike

我的 JPA 请求有问题。
我在 Spring MVC 的 Spring 项目上工作,在 TomCat 服务器上使用 spring 数据。

我有这些表:

Tables

我需要从文档的 ID 开始,找到与之相关的问题和答案。
在 SQL 中是这样的:

Select * from quizz_question, quizz_answer
where quizz_question.id = ( Select question_id from document_question Where document_id = 4406585 )
and quizz_answer.question_ID = quizz_question.id

我的 Document 和 QuizzQuestion 实体使用 DOCUMENT_QUESTION 表连接。

文件实体:
@Entity
@Table(name = TAB_DOCUMENT)
public class Document implements Serializable, IEntity<Long> {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Long id;

@Column(name = "CONTENT")
@Lob
private String content;

@ManyToMany(fetch = FetchType.EAGER,cascade = {CascadeType.MERGE,CascadeType.REMOVE})
@JoinTable(name = SopraTableConstants.TAB_DOCUMENT_QUESTION,
uniqueConstraints = { @UniqueConstraint(name = SopraTableConstants.CONSTRAINT_UK_DOCUMENT_QUESTION, columnNames = {"DOCUMENT_ID","QUESTION_ID"}) },
joinColumns = { @JoinColumn(name = "DOCUMENT_ID", referencedColumnName = "ID") },
inverseJoinColumns = { @JoinColumn(name = "QUESTION_ID", referencedColumnName = "ID") }
)
@SortNatural
private SortedSet<QuizzQuestion> questions=new TreeSet<QuizzQuestion>();

/** @return the content */
public String getContent() {
return content;
}

/** @return the questions */
public SortedSet<QuizzQuestion> getQuestions() {
return questions;
}

/** @param questions
* the questions to set */
public void setQuestions(final SortedSet<QuizzQuestion> questions) {
this.questions=questions;
}
}

测验问题实体:
@Entity
@Table(name = TAB_QUIZZ_QUESTION)
public class QuizzQuestion implements Serializable, IEntity<Long>, Comparable<QuizzQuestion> {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String questionLabel;

@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinColumn(name = "QUESTION_ID", referencedColumnName = "id", foreignKey = @ForeignKey(name = SopraTableConstants.FK_QUIZZ_ANSWER_QUIZZ_DOCUMENT))
@SortNatural
private SortedSet<QuizzAnswer> answers;

@OneToOne
@JoinColumn(name = "QUIZZ_ANSWER_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = SopraTableConstants.FK_QUIZZ_RIGHT_ANSWER))
private QuizzAnswer rightAnswer;

public SortedSet<QuizzAnswer> getAnswers() {
return answers;
}

@Override
public Long getId() {
return id;
}

public String getQuestionLabel() {
return questionLabel;
}

public QuizzAnswer getRightAnswer() {
return rightAnswer;
}

public void setAnswers(SortedSet<QuizzAnswer> param) {
this.answers = param;
}

@Override
public void setId(Long id) {
this.id = id;
}

public void setQuestionLabel(String param) {
this.questionLabel = param;
}

public void setRightAnswer(QuizzAnswer param) {
this.rightAnswer = param;
}
}

QuizzAnswer 实体:
@Entity
@Table(name = TAB_QUIZZ_ANSWER)
public class QuizzAnswer implements Serializable, IEntity<Long>, Comparable<QuizzAnswer> {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Pattern(regexp = ".{1,255}", message = "pattern")
private String answerLabel;

public String getAnswerLabel() {
return answerLabel;
}

@Override
public Long getId() {
return id;
}

public void setAnswerLabel(String param) {
this.answerLabel = param;
}

@Override
public void setId(Long id) {
this.id = id;
}
}

所以我在 JPA 中做了这样的事情:
@Query(value = "select quest from Document doc"
+ " right join doc.questions quest"
+ " join quest.answers"
+ " where doc.id = :documentId"
)
List<QuizzQuestion> findQuestionsByDocumentId(@Param(value = "documentId") final Long documentId);

但是有几个问题。我在基本的 DOCUMENT 表上询问,但我返回了“QuizzQuestion”项目的列表。那么我是否必须在“DocumentRepository”或“QuizzQuestionRepository”存储库中执行此查询。

我已经尝试了这两种方法,但无论如何,都会向我返回以下错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'quizzQuestionRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.afklm.sopra.repository.QuizzQuestionRepository.findQuestionsByDocumentId(java.lang.Long)!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:187)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1213)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1053)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
... 52 more
Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.afklm.sopra.repository.QuizzQuestionRepository.findQuestionsByDocumentId(java.lang.Long)!
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:92)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:62)
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:72)
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:53)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:141)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:209)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:74)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:416)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:206)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 63 more
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=quest,role=com.afklm.sopra.entity.Document.questions,tableName=QUIZZ_QUESTION,tableAlias=quizzquest2_,origin=DOCUMENT document0_,columns={document0_.ID ,className=com.afklm.sopra.entity.QuizzQuestion}}] [select quest from com.afklm.sopra.entity.Document doc right join fetch doc.questions quest join fetch quest.answers where doc.id = :documentId]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:331)
at sun.reflect.GeneratedMethodAccessor60.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
at com.sun.proxy.$Proxy122.createQuery(Unknown Source)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86)
... 76 more
Caused by: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=quest,role=com.afklm.sopra.entity.Document.questions,tableName=QUIZZ_QUESTION,tableAlias=quizzquest2_,origin=DOCUMENT document0_,columns={document0_.ID ,className=com.afklm.sopra.entity.QuizzQuestion}}] [select quest from com.afklm.sopra.entity.Document doc right join fetch doc.questions quest join fetch quest.answers where doc.id = :documentId]
at org.hibernate.QueryException.generateQueryException(QueryException.java:137)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
... 82 more
Caused by: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=quest,role=com.afklm.sopra.entity.Document.questions,tableName=QUIZZ_QUESTION,tableAlias=quizzquest2_,origin=DOCUMENT document0_,columns={document0_.ID ,className=com.afklm.sopra.entity.QuizzQuestion}}]
at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:227)
at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:923)
at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:691)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:663)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
... 90 more

这可能很容易解决,但我有点迷茫。如果有人可以启发我,在此先感谢他! :D

最佳答案

对于查询问题,缺少空格:

作为一个很好的做法,将它们放在多行字符串的开头,以便您可以看到它。

然后你想返回 QuizzQuestion 所以返回这些而不是文档:

@Query(value = "select quest from QuizzQuestion quest"
+ " join fetch quest.answers"
+ " where quest.document.id = :documentId"
)
List<QuizzQuestion> findQuestionsByDocumentId(@Param(value = "documentId") final Long documentId);

关于java - JPA 请求问题 : Validation failed for query for method public abstract java. util.List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60413802/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com