使用 spring 创建自定义查找器
Create custom finder with spring
我试图实现一个多对多的数据库 relationship.The 数据库已经建立,现在我们需要查询它。我以这篇文章为例:https://www.bezkoder.com/jpa-many-to-many/。当我在存储库中创建自定义查找器时 - 服务器在 运行.
崩溃
我有两个实体:MassageEntity 和 RstringsEntity。我正在尝试创建一个方法 List<MasageEntity> findMasageEntitiesByRstringsEntitiesId(Long string_id);
。如果没有该方法,服务器将启动。
这是错误的样子。
2022-04-09 22:08:39.091 ERROR 9672 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'massegeController': Unsatisfied dependency expressed through field 'Massage_repo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'massage_repo' defined in Dyplom.rString.repository.massage_repo defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! Reason: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=11=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.17.jar:5.3.17]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.17.jar:5.3.17]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.5.jar:2.6.5]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) ~[spring-boot-2.6.5.jar:2.6.5]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) ~[spring-boot-2.6.5.jar:2.6.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-2.6.5.jar:2.6.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) ~[spring-boot-2.6.5.jar:2.6.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.5.jar:2.6.5]
at Dyplom.rString.RStringApplication.main(RStringApplication.java:10) ~[main/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.6.5.jar:2.6.5]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'massage_repo' defined in Dyplom.rString.repository.massage_repo defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! Reason: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=11=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1389) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:656) ~[spring-beans-5.3.17.jar:5.3.17]
... 25 common frames omitted
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! Reason: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!
at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:106) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery(QueryExecutorMethodInterceptor.java:94) ~[spring-data-commons-2.6.3.jar:2.6.3]
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:197) ~[na:na]
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) ~[na:na]
at java.base/java.util.Collections$UnmodifiableCollection.forEachRemaining(Collections.java:1056) ~[na:na]
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.mapMethodsToQuery(QueryExecutorMethodInterceptor.java:96) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new[=11=](QueryExecutorMethodInterceptor.java:86) ~[spring-data-commons-2.6.3.jar:2.6.3]
at java.base/java.util.Optional.map(Optional.java:260) ~[na:na]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.<init>(QueryExecutorMethodInterceptor.java:86) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:364) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet(RepositoryFactoryBeanSupport.java:322) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.util.Lazy.getNullable(Lazy.java:230) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:328) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144) ~[spring-data-jpa-2.6.3.jar:2.6.3]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.17.jar:5.3.17]
... 35 common frames omitted
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:96) ~[spring-data-jpa-2.6.3.jar:2.6.3]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:113) ~[spring-data-jpa-2.6.3.jar:2.6.3]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:254) ~[spring-data-jpa-2.6.3.jar:2.6.3]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:87) ~[spring-data-jpa-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:102) ~[spring-data-commons-2.6.3.jar:2.6.3]
... 57 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property 'rstringsentsId' found for type 'Masageent'!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:90) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:437) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:413) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.mapping.PropertyPath.lambda$from[=11=](PropertyPath.java:366) ~[spring-data-commons-2.6.3.jar:2.6.3]
at java.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330) ~[na:na]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:348) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:331) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:81) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new[=11=](PartTree.java:249) ~[spring-data-commons-2.6.3.jar:2.6.3]
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:197) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:179) ~[na:na]
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:250) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new[=11=](PartTree.java:383) ~[spring-data-commons-2.6.3.jar:2.6.3]
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:197) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:179) ~[na:na]
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:384) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:95) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:89) ~[spring-data-jpa-2.6.3.jar:2.6.3]
... 61 common frames omitted
现在我展示一些代码。
RstringEntity.java
package Dyplom.rString.entity;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "string")
public class RstringsEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name")
private String name;
@Column(name="code")
private String code;
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
@JoinTable(name = "string_massage",
joinColumns = { @JoinColumn(name = "string_id",referencedColumnName = "id"), },
inverseJoinColumns = { @JoinColumn(name = "massage_id",referencedColumnName = "id") })
private Set<MasageEntity> massagess = new HashSet<>();
public RstringsEntity(String name, String description) {
this.name = name;
this.code = code;
}
public RstringsEntity() {
}
// getters and setters
public void addMassage(MasageEntity massage) {
this.massagess.add(massage);
massage.getMassages().add(this);
}
public void removeMassage(long mId) {
MasageEntity massage = this.massagess.stream().filter(t -> t.getId() == mId).findFirst().orElse(null);
if (massage != null) this.massagess.remove(massage);
massage.getMassages().remove(this);
}
/*
@ManyToMany()
private List<rStringsMassageEntity> massages;
*/
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
MassageEntity.java
package Dyplom.rString.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "massages")
public class MasageEntity {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "string_text")
private String string_text;
@Column(name = "string_speed")
private Long string_speed;
@Column(name = "string_color_type")
private Long string_color_type;
@Column(name = "string_color")
private String string_color;
@Column(name = "string_timing_type")
private String string_timing_type;
@Column(name = "string_timing")
private String string_timing;
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
},
mappedBy = "massagess")
@JsonIgnore
private Set<RstringsEntity> strings = new HashSet<>();
public MasageEntity() {
}
public long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getString_text() {
return string_text;
}
public void setString_text(String string_text) {
this.string_text = string_text;
}
public Long getString_speed() {
return string_speed;
}
public void setString_speed(Long string_speed) {
this.string_speed = string_speed;
}
public Long getString_color_type() {
return string_color_type;
}
public void setString_color_type(Long string_color_type) {
this.string_color_type = string_color_type;
}
public String getString_color() {
return string_color;
}
public void setString_color(String string_color) {
this.string_color = string_color;
}
public String getString_timing_type() {
return string_timing_type;
}
public void setString_timing_type(String string_timing_type) {
this.string_timing_type = string_timing_type;
}
public String getString_timing() {
return string_timing;
}
public void setString_timing(String string_timing) {
this.string_timing = string_timing;
}
public Set<RstringsEntity> getMassages() {
return strings;
}
public void setTutorials(Set<RstringsEntity> strings) {
this.strings = strings;
}
}
strings_repo.java
package Dyplom.rString.repository;
import Dyplom.rString.entity.RstringsEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface strings_repo extends JpaRepository<RstringsEntity, Long> {
RstringsEntity findByName(String name);
RstringsEntity findRstringsEntitiesById(Long string_id);
List<RstringsEntity> findRstringsEntitiesByMasageEntitiesId(Long massage_id);
}
massage_repo.java
package Dyplom.rString.repository;
import Dyplom.rString.entity.MasageEntity;
import Dyplom.rString.entity.RstringsEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface massage_repo extends JpaRepository <MasageEntity, Long> {
MasageEntity findMasageEntitiesById(Long massage_id);
List<MasageEntity> findMasageEntitiesByRstringsEntitiesId(Long string_id);
List<MasageEntity> findMasageEntityByRstringsEntityId(Long string_id);
}
我看不出显示控制器有什么意义,因为错误发生在其他地方。
我做错了什么?也许我需要一些设置或依赖项?或者我可以用不同的方式解决问题?
您的问题是查询方法的名称:findMasageEntitiesByRstringsEntitiesId(Long
。您需要使用实体 class 中的字段名称,而不是 class 名称。
我会尝试 findMasageEntitiesByStringsId(Long...
。 (您给变量一个名称而给 getter 方法另一个名称也有点令人困惑,不确定 JPA 是否会喜欢。)
returns列表的其他方法也遇到同样的问题。
我试图实现一个多对多的数据库 relationship.The 数据库已经建立,现在我们需要查询它。我以这篇文章为例:https://www.bezkoder.com/jpa-many-to-many/。当我在存储库中创建自定义查找器时 - 服务器在 运行.
崩溃我有两个实体:MassageEntity 和 RstringsEntity。我正在尝试创建一个方法 List<MasageEntity> findMasageEntitiesByRstringsEntitiesId(Long string_id);
。如果没有该方法,服务器将启动。
这是错误的样子。
2022-04-09 22:08:39.091 ERROR 9672 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'massegeController': Unsatisfied dependency expressed through field 'Massage_repo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'massage_repo' defined in Dyplom.rString.repository.massage_repo defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! Reason: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=11=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.17.jar:5.3.17]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.17.jar:5.3.17]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.5.jar:2.6.5]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) ~[spring-boot-2.6.5.jar:2.6.5]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) ~[spring-boot-2.6.5.jar:2.6.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-2.6.5.jar:2.6.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) ~[spring-boot-2.6.5.jar:2.6.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.5.jar:2.6.5]
at Dyplom.rString.RStringApplication.main(RStringApplication.java:10) ~[main/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.6.5.jar:2.6.5]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'massage_repo' defined in Dyplom.rString.repository.massage_repo defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! Reason: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=11=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1389) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:656) ~[spring-beans-5.3.17.jar:5.3.17]
... 25 common frames omitted
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! Reason: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!
at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:106) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery(QueryExecutorMethodInterceptor.java:94) ~[spring-data-commons-2.6.3.jar:2.6.3]
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:197) ~[na:na]
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) ~[na:na]
at java.base/java.util.Collections$UnmodifiableCollection.forEachRemaining(Collections.java:1056) ~[na:na]
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.mapMethodsToQuery(QueryExecutorMethodInterceptor.java:96) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new[=11=](QueryExecutorMethodInterceptor.java:86) ~[spring-data-commons-2.6.3.jar:2.6.3]
at java.base/java.util.Optional.map(Optional.java:260) ~[na:na]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.<init>(QueryExecutorMethodInterceptor.java:86) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:364) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet(RepositoryFactoryBeanSupport.java:322) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.util.Lazy.getNullable(Lazy.java:230) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:328) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144) ~[spring-data-jpa-2.6.3.jar:2.6.3]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.17.jar:5.3.17]
... 35 common frames omitted
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List Dyplom.rString.repository.massage_repo.findMassageentByRstringsentsId(java.lang.Long)! No property 'rstringsentsId' found for type 'Masageent'!
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:96) ~[spring-data-jpa-2.6.3.jar:2.6.3]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:113) ~[spring-data-jpa-2.6.3.jar:2.6.3]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:254) ~[spring-data-jpa-2.6.3.jar:2.6.3]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:87) ~[spring-data-jpa-2.6.3.jar:2.6.3]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:102) ~[spring-data-commons-2.6.3.jar:2.6.3]
... 57 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property 'rstringsentsId' found for type 'Masageent'!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:90) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:437) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:413) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.mapping.PropertyPath.lambda$from[=11=](PropertyPath.java:366) ~[spring-data-commons-2.6.3.jar:2.6.3]
at java.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330) ~[na:na]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:348) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:331) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:81) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new[=11=](PartTree.java:249) ~[spring-data-commons-2.6.3.jar:2.6.3]
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:197) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:179) ~[na:na]
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:250) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new[=11=](PartTree.java:383) ~[spring-data-commons-2.6.3.jar:2.6.3]
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:197) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:179) ~[na:na]
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) ~[na:na]
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:384) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:95) ~[spring-data-commons-2.6.3.jar:2.6.3]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:89) ~[spring-data-jpa-2.6.3.jar:2.6.3]
... 61 common frames omitted
现在我展示一些代码。
RstringEntity.java
package Dyplom.rString.entity;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "string")
public class RstringsEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name")
private String name;
@Column(name="code")
private String code;
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
@JoinTable(name = "string_massage",
joinColumns = { @JoinColumn(name = "string_id",referencedColumnName = "id"), },
inverseJoinColumns = { @JoinColumn(name = "massage_id",referencedColumnName = "id") })
private Set<MasageEntity> massagess = new HashSet<>();
public RstringsEntity(String name, String description) {
this.name = name;
this.code = code;
}
public RstringsEntity() {
}
// getters and setters
public void addMassage(MasageEntity massage) {
this.massagess.add(massage);
massage.getMassages().add(this);
}
public void removeMassage(long mId) {
MasageEntity massage = this.massagess.stream().filter(t -> t.getId() == mId).findFirst().orElse(null);
if (massage != null) this.massagess.remove(massage);
massage.getMassages().remove(this);
}
/*
@ManyToMany()
private List<rStringsMassageEntity> massages;
*/
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
MassageEntity.java
package Dyplom.rString.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "massages")
public class MasageEntity {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "string_text")
private String string_text;
@Column(name = "string_speed")
private Long string_speed;
@Column(name = "string_color_type")
private Long string_color_type;
@Column(name = "string_color")
private String string_color;
@Column(name = "string_timing_type")
private String string_timing_type;
@Column(name = "string_timing")
private String string_timing;
@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
},
mappedBy = "massagess")
@JsonIgnore
private Set<RstringsEntity> strings = new HashSet<>();
public MasageEntity() {
}
public long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getString_text() {
return string_text;
}
public void setString_text(String string_text) {
this.string_text = string_text;
}
public Long getString_speed() {
return string_speed;
}
public void setString_speed(Long string_speed) {
this.string_speed = string_speed;
}
public Long getString_color_type() {
return string_color_type;
}
public void setString_color_type(Long string_color_type) {
this.string_color_type = string_color_type;
}
public String getString_color() {
return string_color;
}
public void setString_color(String string_color) {
this.string_color = string_color;
}
public String getString_timing_type() {
return string_timing_type;
}
public void setString_timing_type(String string_timing_type) {
this.string_timing_type = string_timing_type;
}
public String getString_timing() {
return string_timing;
}
public void setString_timing(String string_timing) {
this.string_timing = string_timing;
}
public Set<RstringsEntity> getMassages() {
return strings;
}
public void setTutorials(Set<RstringsEntity> strings) {
this.strings = strings;
}
}
strings_repo.java
package Dyplom.rString.repository;
import Dyplom.rString.entity.RstringsEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface strings_repo extends JpaRepository<RstringsEntity, Long> {
RstringsEntity findByName(String name);
RstringsEntity findRstringsEntitiesById(Long string_id);
List<RstringsEntity> findRstringsEntitiesByMasageEntitiesId(Long massage_id);
}
massage_repo.java
package Dyplom.rString.repository;
import Dyplom.rString.entity.MasageEntity;
import Dyplom.rString.entity.RstringsEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface massage_repo extends JpaRepository <MasageEntity, Long> {
MasageEntity findMasageEntitiesById(Long massage_id);
List<MasageEntity> findMasageEntitiesByRstringsEntitiesId(Long string_id);
List<MasageEntity> findMasageEntityByRstringsEntityId(Long string_id);
}
我看不出显示控制器有什么意义,因为错误发生在其他地方。
我做错了什么?也许我需要一些设置或依赖项?或者我可以用不同的方式解决问题?
您的问题是查询方法的名称:findMasageEntitiesByRstringsEntitiesId(Long
。您需要使用实体 class 中的字段名称,而不是 class 名称。
我会尝试 findMasageEntitiesByStringsId(Long...
。 (您给变量一个名称而给 getter 方法另一个名称也有点令人困惑,不确定 JPA 是否会喜欢。)
returns列表的其他方法也遇到同样的问题。