I'm using Hibernate with Panache and I need to add a user when the application starts.
For that, I'm annotating my bean with @Startup
and then I have a method with the annotation @PostConstruct
.
Currently, I'm using the following code:
@Startup
@ApplicationScoped
class AuthService {
@Inject
lateinit var userRepository: UserRepository
@PostConstruct
fun init() {
logger.info("Creating admin user")
val user = User(
"Admin", ADMIN_NAME, BcryptUtil.bcryptHash(ADMIN_PASS), mutableSetOf(Role.ADMIN)
)
Panache.withTransaction {
userRepository.persist(user)
}.subscribe().with({
logger.info("Done")
}, { fail ->
logger.error("Failed admin creation: $fail")
})
}
}
From what I found, when this method is called there are no guarantees that everything is already set, and I guess that is why it sometimes fails with the error Session/EntityManager is closed
.
I have already checked this question but since it is for Spring, the method doesn't work and I didn't find anything similar for Quarkus.
Am I missing any solution or is there a better approach to this?