Используйте SessionFactory в Hibernate5 и Springboot 2.1.x - PullRequest
2 голосов
/ 04 октября 2019

Я перевожу мое приложение Spring boot 1.5.x на Springboot 2.1.x, мы используем spring-data-jpa для наших операций crud. Для вставки объекта BLOB-объекта я использовал следующий код:

@Bean 
    public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf) {
        return hemf.getSessionFactory();
    }
    @Autowired
    private SessionFactory sessionFactory;

    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }


    private FileContent getContent(MultipartFile file) throws IOException {

        FileContent fileContent = new FileContent();
        LobCreator blob = Hibernate.getLobCreator(getSession());
        fileContent.setContent(blob.createBlob(file.getInputStream(), -1));
        return fileContent;
    }

В Hibernate 5 HibernateEntityManagerFactory устарел, а существующее решение не работает после того, как возникло исключение миграции:


Field fileContentRepository in com.example.demo.api.RestController required a bean named 'entityManagerFactory' that could not be found.

Похоже, что компонент entityManagerFactory не создан.

...