1

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?

Saita
  • 964
  • 11
  • 36
  • Thanks to riversidetraveler answer I managed to find this other thread http://stackoverflow.com/questions/26667910/no-currentsessioncontext-configured that helped too. – Saita Feb 09 '16 at 21:22

1 Answers1

1

You're not showing where you've configured your session factory. Are you using spring-boot-starter-data-jpa or are you wiring up the SessionFactory yourself in an @Configuration annotated class or via standard xml bean config?

Edit: Based on that answer check out this stackoverflow answer.

Community
  • 1
  • 1
  • I am using spring-boot-starter-data-jpa. This is really my first project from scratch so some things are just working by themselves and I don't really know howyet. – Saita Feb 09 '16 at 20:30