I am building a SpringBoot application and I am trying to programatically populate my test database.
I came up with this:
@Profile("dev")
@Component
public class DatabaseFillerOnStartup implements ApplicationListener<ContextRefreshedEvent> {
@Resource
private SomeRepository someRepository;
@Resource //This doesn't work
private SessionFactory sessionFactory;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
...
One of my entities has a Blob where I want to save an image:
private Blob createBlobWithSampleImage() {
InputStream imageStream = this.getClass().getClassLoader().getResourceAsStream("sample1.jpg");
LobCreator lobCreator = Hibernate.getLobCreator(sessionFactory.getCurrentSession());
try {
return lobCreator.createBlob(IOUtils.toByteArray(imageStream));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
The problem is that I can't manage to inject the sessionFactory.
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException
Is there a better way to achieve what I want?