It would be nice if spring boot will create user defined IdentifierGenerator for hibernate, take advantage of dependency injection over clazz.newInstance().
It should be resolved at hibernate side, so I created https://github.com/hibernate/hibernate-orm/pull/4064, meantime I wonder if spring boot could do something in case of this PR will not be accepted.
for example:
public class RedisIdentifierGenerator implements IdentifierGenerator {
private final RedisTemplate redisTemplate; // autowired by spring
public RedisIdentifierGenerator(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
public Serializable generate(SharedSessionContractImplementor session, Object obj) {
// redis implementation
}
}
@Id
@GeneratedValue(generator = "redis")
@GenericGenerator(name = "redis", strategy = "com.example.RedisIdentifierGenerator")
private @Nullable PK id;
Comment From: wilkinsona
Thanks for the suggestion, but the hook point for us to be able to provide beans to Hibernate is its BeanContainer. Without the Hibernate changes that you've proposed there's nothing that we can do in Spring Boot to make this work. With the Hibernate changes that you've proposed it should work without any further changes in Spring Boot as we already auto-configure Hibernate to use the org.springframework.orm.hibernate5.SpringBeanContainer BeanContainer implementation.