Spring Data JPA FindBy 方法不起作用



我的代码出现了一些错误。我正在尝试在同一个项目中同时使用MongoDB和MYSQL。在这一点上,我认为我很好,但我的 JPARepository 让我在最后几个小时有些头疼。事实是我无法实现 findBy*** 方法。这是我的代码和错误:

存储 库

@Repository
public interface AppUserRepository extends JpaRepository<AppUser,Long> {
AppUser findByUsername(String username);
AppUser findByEmail(String email);
}

@Entity
public class AppUser {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(unique = true)
private String username;
private String password;
@Column(unique = true)
private String email;
private boolean enabled;
@ManyToMany(fetch = FetchType.EAGER)
private Collection<AppRole> roles = new ArrayList<>();
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
private VerificationToken verificationToken;

public AppUser() {
this.enabled = false;
}
public AppUser(Long id, String username, String password, Collection<AppRole> roles,String email) {
this.id = id;
this.username = username;
this.password = password;
this.roles = roles;
this.email = email;
this.enabled = false;
}

public AppUser(String username, String password, Collection<AppRole> roles, String email) {
this.username = username;
this.password = password;
this.roles = roles;
this.email = email;
this.enabled = false;
}
public VerificationToken getVerificationToken() {
return verificationToken;
}
public void setVerificationToken(VerificationToken verificationToken) {
this.verificationToken = verificationToken;
}

public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@JsonIgnore
public String getPassword() {
return password;
}
@JsonSetter
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Collection<AppRole> getRoles() {
return roles;
}
public void setRoles(Collection<AppRole> roles) {
this.roles = roles;
}
}

应用程序类

@SpringBootApplication()
@EnableAutoConfiguration()
@EnableConfigurationProperties
@EntityScan(basePackages = { "com.anne.juliette.pharma.entities"})
@EnableMongoRepositories(basePackageClasses = {OrderRepository.class, PhotoRepository.class})
@EnableJpaRepositories(basePackageClasses = {AppRoleRepository.class, AppUserRepository.class, VerificationTokenRepository.class})
public class PharmaApplication implements CommandLineRunner {
@Bean
public BCryptPasswordEncoder getBCPE(){ return new BCryptPasswordEncoder();}
@Autowired
private AccountService accountService;
public static void main(String[] args) {
SpringApplication.run(PharmaApplication.class, args);
}
@Bean
public FilterRegistrationBean<CorsFilter> simpleCorsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(Collections.singletonList("http://localhost:4200"));
config.setAllowedMethods(Collections.singletonList("*"));
config.setAllowedHeaders(Collections.singletonList("*"));
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
@Override
public void run(String... args) throws Exception {
accountService.saveRole(new AppRole(null,"ADMIN"));
accountService.saveRole(new AppRole(null,"USER"));
AppUser user1 = new AppUser("admin","1234",null, "admin@admin.com");
AppUser user2 = new AppUser("user","1234",null, "user@admin.com");
user1.setEnabled(true);
user2.setEnabled(true);
accountService.saveUser(user1);
accountService.saveUser(user2);
accountService.addRoleToUser("admin@admin.com","ADMIN");
accountService.addRoleToUser("admin@admin.com","USER");
accountService.addRoleToUser("user@admin.com","USER");
System.out.println(accountService.allUsers());
}
}

帐户服务类


@Service
@Transactional
public class AccountServiceImpl implements AccountService {
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Autowired
private AppUserRepository userRepository;
@Autowired
private AppRoleRepository roleRepository;
@Autowired
private VerificationTokenService verificationTokenService;
@Override
public AppUser saveUser(AppUser user) {
String hashPass = bCryptPasswordEncoder.encode(user.getPassword());
user.setPassword(hashPass);
return userRepository.save(user);
}
@Override
public AppRole saveRole(AppRole role) {
return roleRepository.save(role);
}
@Override
public void addRoleToUser(String email, String roleName) {
AppRole role = roleRepository.findByRole(roleName);
AppUser user = userRepository.findByEmail(email);
if(user.getRoles() == null){
List<AppRole> list = new ArrayList<AppRole>();
list.add(role);
user.setRoles(list);
} else {
user.getRoles().add(role);
}
}
@Override
public AppUser findUserByUsername(String username) {  return userRepository.findByUsername(username);    }
@Override
public AppUser findUserByEmail(String email){   return userRepository.findByEmail(email);    }
@Override
public List<AppUser> allUsers() {   return userRepository.findAll();    }
@Override
public boolean emailExist(String email) {
return userRepository.findByEmail(email) != null;
}
@Override
public boolean usernameExist(String username) {
return userRepository.findByUsername(username) != null;
}

}
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Cannot create binding for parameter reference [org.hibernate.query.sqm.tree.expression.JpaCriteriaParameter@66716959] - reference is not a parameter of this query; nested exception is java.lang.IllegalArgumentException: Cannot create binding for parameter reference [org.hibernate.query.sqm.tree.expression.JpaCriteriaParameter@66716959] - reference is not a parameter of this query
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:374) ~[spring-orm-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:257) ~[spring-orm-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528) ~[spring-orm-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) ~[spring-tx-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) ~[spring-tx-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153) ~[spring-tx-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:178) ~[spring-data-jpa-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at com.sun.proxy.$Proxy91.findAll(Unknown Source) ~[na:na]
at com.anne.juliette.pharma.services.AccountServiceImpl.allUsers(AccountServiceImpl.java:65) ~[main/:na]
at com.anne.juliette.pharma.services.AccountServiceImpl$$FastClassBySpringCGLIB$$4e5d1390.invoke(<generated>) ~[main/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:366) ~[spring-tx-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) ~[spring-tx-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at com.anne.juliette.pharma.services.AccountServiceImpl$$EnhancerBySpringCGLIB$$55c904fc.allUsers(<generated>) ~[main/:na]
at com.anne.juliette.pharma.PharmaApplication.run(PharmaApplication.java:77) [main/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) [spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
... 5 common frames omitted
Caused by: java.lang.IllegalArgumentException: Cannot create binding for parameter reference [org.hibernate.query.sqm.tree.expression.JpaCriteriaParameter@66716959] - reference is not a parameter of this query
at org.hibernate.query.internal.QueryParameterBindingsImpl.makeBinding(QueryParameterBindingsImpl.java:95) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
at org.hibernate.query.internal.QueryParameterBindingsImpl.getBinding(QueryParameterBindingsImpl.java:122) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
at org.hibernate.query.sqm.internal.SqmUtil.createJdbcParameterBindings(SqmUtil.java:173) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:155) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:437) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
at org.hibernate.query.spi.AbstractQuery.list(AbstractQuery.java:1356) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
at org.hibernate.query.Query.getResultList(Query.java:136) ~[hibernate-core-6.0.0.Alpha5.jar:6.0.0.Alpha5]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:355) ~[spring-data-jpa-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:78) ~[spring-data-jpa-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
at org.springframework.data.repository.core.support.ImplementationInvocationMetadata.invoke(ImplementationInvocationMetadata.java:72) ~[spring-data-commons-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:382) ~[spring-data-commons-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:205) ~[spring-data-commons-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:549) ~[spring-data-commons-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:155) ~[spring-data-commons-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130) ~[spring-data-commons-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) ~[spring-data-commons-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:366) ~[spring-tx-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) ~[spring-tx-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.6.RELEASE.jar:5.2.6.RELEASE]
... 26 common frames omitted

您使用的是 Hibernate 6.0.0.Alpha5,这是一个 alpha 版本,远未准备好生产,可能包含这样的错误。尝试最新的稳定版本,即今天的5.4.16.Final。

最新更新