希望大家都做得好,
我遇到了一些我找不到解决方法的问题 或如何解决
如果我想进行查询是可能的,但我想尝试这种方式,如果不可能,请在下面显示,也请让我知道 我正在使用 JPARepository
public Posts findByIdAndCommonFieldActive(int id, boolean active);
我的发布实体课程
@Entity
@Table(name="post")
public class Posts implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String title;
private String description;
private String content;
@Embedded
private CommonField commonField;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="post")
private List<Comments> comments = new ArrayList<Comments>();
//getter and setter
}
我的共同领域CLass
@Embeddable
public class CommonField {
private boolean isActive;
private Date createdDate;
private Date modifiedDate;
private int createdBy;
private int modifiedBy;
public CommonField() {
// TODO Auto-generated constructor stub
}
public CommonField(boolean isActive, Date createdDate, Date modifiedDate, int createdBy, int modifiedBy) {
super();
this.isActive = isActive;
this.createdDate = createdDate;
this.modifiedDate = modifiedDate;
this.createdBy = createdBy;
this.modifiedBy = modifiedBy;
}
/// Getter and Setter
}
我得到的错误
Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract com.harkesh.model.Posts com.harkesh.repo.PostRepo.findByIdAndCommonFieldActive(int,boolean)!
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:93) ~[spring-data-jpa-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:63) ~[spring-data-jpa-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:76) ~[spring-data-jpa-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:56) ~[spring-data-jpa-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:139) ~[spring-data-jpa-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:206) ~[spring-data-jpa-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:79) ~[spring-data-jpa-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lookupQuery(RepositoryFactorySupport.java:566) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(RepositoryFactorySupport.java:559) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_192]
at java.util.Iterator.forEachRemaining(Iterator.java:116) ~[na:1.8.0_192]
at java.util.Collections$UnmodifiableCollection$1.forEachRemaining(Collections.java:1049) ~[na:1.8.0_192]
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:1.8.0_192]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_192]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_192]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_192]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_192]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_192]
但是最好的方式是让我知道 谢谢
答案 0 :(得分:0)
更改
public Posts findByIdAndCommonFieldActive(int id, boolean active);
到
public Posts findByIdAndCommonFieldIsActive(int id, boolean isActive);
应该可以解决您的问题。