Reputation: 25457
I have created a simple repository for my app users:
@RepositoryRestResource(path = "users")
public interface AppUserRepository extends JpaRepository<AppUserModel, Long> {
List<AppUserModel> findByUsername(@Param("username") String username);
}
However, I need to take care of password encryption before a new user gets inserted. For this I implemented a @RepositoryEventHandler
:
@RepositoryEventHandler(AppUserModel.class)
public class AppUserService {
@HandleBeforeSave
public void handleProfileSave(AppUserModel appUserModel) {
System.out.println("Before save ..");
}
@HandleBeforeCreate
public void register(AppUserModel appUserModel) {
System.out.println("Before create ..");
}
}
The problem is that none of those two event handlers are getting executed. As a result AppUserModel
is tried to be persisted but fails because no salt (for the password) has been generated and set - that's how I can tell that the JpaRepository<AppUserModel, Long>
is working so far.
{
"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, [email protected])."
},
"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"
}
Upvotes: 1
Views: 1041
Reputation: 25457
Another way could be providing the bean from within another configuration class:
@Configuration
public class RepositoryConfiguration {
@Bean
AppUserEventHandler appUserService() {
return new AppUserEventHandler();
}
}
However, I prefer the solution provided by @varren.
Upvotes: 0
Reputation: 14731
It looks to me that you just need to create AppUserService
bean. The easiest option would be to annotate it with @Component
@Component
@RepositoryEventHandler(AppUserModel.class)
public class AppUserService {
//...
}
Upvotes: 2