Spring @未调用RepositoryEventHandler
我为我的应用程序用户创建了一个简单的存储库:Spring @未调用RepositoryEventHandler,spring,spring-data-jpa,Spring,Spring Data Jpa,我为我的应用程序用户创建了一个简单的存储库: @RepositoryRestResource(path = "users") public interface AppUserRepository extends JpaRepository<AppUserModel, Long> { List<AppUserModel> findByUsername(@Param("username") String username); } 问题是这两个事件处理程序都没有被
@RepositoryRestResource(path = "users")
public interface AppUserRepository extends JpaRepository<AppUserModel, Long> {
List<AppUserModel> findByUsername(@Param("username") String username);
}
问题是这两个事件处理程序都没有被执行。因此,AppUserModel
尝试被持久化,但失败了,因为没有生成和设置salt(密码)-这就是我如何判断JpaRepository
到目前为止是否正常工作的原因
{
"cause": {
"cause": {
"cause": null,
"message": "ERROR: null value in column "password_salt" violates not-null constraint Detail: Failing row contains (26, 2017-11-18 21:21:47.534, 2017-11-18 21:21:47.534, f, asd, null, haha@gmx.at)."
},
"message": "could not execute statement"
},
"message": "could not execute statement; SQL [n/a]; constraint [password_salt]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"
}
在我看来,您只需要创建
AppUserService
bean。最简单的选择是使用@组件对其进行注释
@Component
@RepositoryEventHandler(AppUserModel.class)
public class AppUserService {
//...
}
另一种方法是从另一个配置类中提供bean:
@Configuration
public class RepositoryConfiguration {
@Bean
AppUserEventHandler appUserService() {
return new AppUserEventHandler();
}
}
但是,我更喜欢@varren提供的解决方案。Oh。我刚刚在另一个配置类中找到了另一个使用
@Bean
的解决方案。这也行得通,我会将其作为另一个答案发布,但你的解决方案当然要简单得多。
@Configuration
public class RepositoryConfiguration {
@Bean
AppUserEventHandler appUserService() {
return new AppUserEventHandler();
}
}