#java #spring #spring-boot
Вопрос:
У меня была эта проблема в моем коде с репозиториями и службами spring в моем проекте spring она не работала для пользователя и роли проблема с их сервисом и репозиторием:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2021-11-13 11:23:03.534 ERROR 1140 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl' defined in file [/Users/mj/Documents/Deploy/Github/Projs/Back/Gym/target/classes/app/gym/v1/Service/UserServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepo' defined in app.gym.v1.Repo.RoleRepo 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 app.gym.v1.Model.Domain.Role app.gym.v1.Repo.RoleRepo.findByRole(java.lang.String)! Reason: Failed to create query for method public abstract app.gym.v1.Model.Domain.Role app.gym.v1.Repo.RoleRepo.findByRole(java.lang.String)! No property role found for type Role!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract app.gym.v1.Model.Domain.Role app.gym.v1.Repo.RoleRepo.findByRole(java.lang.String)! No property role found for type Role! at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.12.jar:5.3.12] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.12.jar:5.3.12] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.6.jar:2.5.6] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.6.jar:2.5.6] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.6.jar:2.5.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.6.jar:2.5.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.6.jar:2.5.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.6.jar:2.5.6] at app.gym.v1.GymApplication.main(GymApplication.java:10) ~[classes/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[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:568) ~[na:na] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.5.6.jar:2.5.6] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepo' defined in app.gym.v1.Repo.RoleRepo 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 app.gym.v1.Model.Domain.Role app.gym.v1.Repo.RoleRepo.findByRole(java.lang.String)! Reason: Failed to create query for method public abstract app.gym.v1.Model.Domain.Role app.gym.v1.Repo.RoleRepo.findByRole(java.lang.String)! No property role found for type Role!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract app.gym.v1.Model.Domain.Role app.gym.v1.Repo.RoleRepo.findByRole(java.lang.String)! No property role found for type Role! at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-5.3.12.jar:5.3.12] ... 24 common frames omitted Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract app.gym.v1.Model.Domain.Role app.gym.v1.Repo.RoleRepo.findByRole(java.lang.String)! Reason: Failed to create query for method public abstract app.gym.v1.Model.Domain.Role app.gym.v1.Repo.RoleRepo.findByRole(java.lang.String)! No property role found for type Role!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract app.gym.v1.Model.Domain.Role app.gym.v1.Repo.RoleRepo.findByRole(java.lang.String)! No property role found for type Role! at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:106) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(QueryExecutorMethodInterceptor.java:94) ~[spring-data-commons-2.5.6.jar:2.5.6] at java.base/java.util.stream.ReferencePipeline$3$1.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$1.forEachRemaining(Collections.java:1061) ~[na:na] at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1845) ~[na:na] at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[na:na] at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[na:na] at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921) ~[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.5.6.jar:2.5.6] at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new$0(QueryExecutorMethodInterceptor.java:86) ~[spring-data-commons-2.5.6.jar:2.5.6] at java.base/java.util.Optional.map(Optional.java:260) ~[na:na] at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lt;initgt;(QueryExecutorMethodInterceptor.java:86) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:360) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:323) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.util.Lazy.getNullable(Lazy.java:230) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:329) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144) ~[spring-data-jpa-2.5.6.jar:2.5.6] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.12.jar:5.3.12] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.12.jar:5.3.12] ... 35 common frames omitted Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract app.gym.v1.Model.Domain.Role app.gym.v1.Repo.RoleRepo.findByRole(java.lang.String)! No property role found for type Role! at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.lt;initgt;(PartTreeJpaQuery.java:96) ~[spring-data-jpa-2.5.6.jar:2.5.6] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:113) ~[spring-data-jpa-2.5.6.jar:2.5.6] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:254) ~[spring-data-jpa-2.5.6.jar:2.5.6] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:87) ~[spring-data-jpa-2.5.6.jar:2.5.6] at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:102) ~[spring-data-commons-2.5.6.jar:2.5.6] ... 57 common frames omitted Caused by: org.springframework.data.mapping.PropertyReferenceException: No property role found for type Role! at org.springframework.data.mapping.PropertyPath.lt;initgt;(PropertyPath.java:90) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:437) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:413) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:366) ~[spring-data-commons-2.5.6.jar:2.5.6] 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.5.6.jar:2.5.6] at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:331) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.repository.query.parser.Part.lt;initgt;(Part.java:81) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new$0(PartTree.java:249) ~[spring-data-commons-2.5.6.jar:2.5.6] at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) ~[na:na] at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179) ~[na:na] at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:992) ~[na:na] at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[na:na] at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[na:na] at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921) ~[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.lt;initgt;(PartTree.java:250) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new$0(PartTree.java:383) ~[spring-data-commons-2.5.6.jar:2.5.6] at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) ~[na:na] at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179) ~[na:na] at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:992) ~[na:na] at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[na:na] at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[na:na] at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921) ~[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.lt;initgt;(PartTree.java:384) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.repository.query.parser.PartTree.lt;initgt;(PartTree.java:95) ~[spring-data-commons-2.5.6.jar:2.5.6] at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.lt;initgt;(PartTreeJpaQuery.java:89) ~[spring-data-jpa-2.5.6.jar:2.5.6] ... 61 common frames omitted Process finished with exit code 0
это репозиторий пользовательских реализаций:
package app.gym.v1.Service; import app.gym.v1.Model.Domain.Role; import app.gym.v1.Model.User; import app.gym.v1.Repo.RoleRepo; import app.gym.v1.Repo.UserRepo; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.util.List; @Service @RequiredArgsConstructor @Transactional @Slf4j public class UserServiceImpl implements UserService{ private final UserRepo userRepo; private final RoleRepo roleRepo; @Override public User saveUser(User user) { return userRepo.save(user); } @Override public Role saveRole(Role role) { return roleRepo.save(role); } @Override public void addUserRole(String phone, String roleName) { User user = userRepo.findByPhone(phone); Role role = roleRepo.findByRole(roleName); user.getRoles().add(role); } @Override public User getUser(String phone) { return userRepo.findByPhone(phone); } @Override public User getUserByEmail(String email) { return userRepo.findByEmail(email); } @Override public User getUserByFname(String fname) { return userRepo.findByFname(fname); } @Override public User getUserByMname(String mname) { return userRepo.findByMname(mname); } @Override public User getUserByLname(String lname) { return userRepo.findByLname(lname); } @Override public Listlt;Usergt; getUsers() { return userRepo.findAll(); } }
и это хранилище и сервис для пользователя:
Обслуживание пользователей
@Service public interface UserService { User saveUser(User user); Role saveRole(Role role); void addUserRole(String phone, String roleName); User getUser(String phone); User getUserByEmail(String email); User getUserByFname(String fname); User getUserByMname(String mname); User getUserByLname(String lname); Listlt;Usergt; getUsers(); }
Хранилище пользователей
public interface UserRepo extends JpaRepositorylt;User, Longgt; { User findByPhone(String phone); User findByEmail(String email); User findByFname(String fname); User findByMname(String mname); User findByLname(String lname); }
попробуйте удалить пустоту, но это та же проблема Я много искал об этом, но не нашел никаких решений.
Комментарии:
1. »
...Failed to create query for method public abstract app.gym.v1.Model.Domain.Role app.gym.v1.Repo.RoleRepo.findByRole(java.lang.String)! No property role found for type Role!
» — Сообщение об ошибке довольно явное: в сущности нет свойстваrole
Role
.2. Пожалуйста, покажите роль и роль
3. RoleRepo открытого интерфейса расширяет JpaRepositorylt;Роль, Длиннаяgt; { Роль findByRole(имя роли строки); }
4. @Id @Генерируемое значение(стратегия = АВТО) @Столбец(значение null = false, обновляемое = false) @JsonProperty(доступ = JsonProperty. Доступ. WRITE_ONLY) закрытый длинный идентификатор; @Столбец(имя = «имя_роли») имя роли в частной строке;
5. @Симон Мартинелли
Ответ №1:
Ваши методы поиска неверны, потому что он использует роль, но свойство-имя роли.
Это было бы правильно:
Role findByRoleName(String roleName);
Комментарии:
1. это работает, большое спасибо, но я не мог поставить вам лайк за ваш ответ, потому что мне еще не исполнилось 15 лет с моей ставкой
2. Да, большое спасибо
3. @MohamadJAlanbaki Пожалуйста, примите правильный ответ, то есть нажмите галочку слева от ответа, чтобы сделать его зеленым, это полезно для других участников здесь, чтобы иметь возможность видеть, что на вопрос был дан правильный ответ.