Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

java - Spring-boot - add hibernate mapping file to entity manager

I'm migrating legacy app to Spring-boot and have to integrate an hibernate named query mapping file (previously configured in persitence.xml file).

I've come out with a solution with an

...
@Autowired
private DataSource dataSource;

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);

    //...

    sessionFactoryBean.setMappingResources("META-INF/named-queries.hbm.xml");

    return sessionFactoryBean;
}   

But i'm ending having an entityManager bean and a sessionFactory bean in my application!

is it a good solution according to you? Is there a way to add somehow the hibernate mapping file (named-query.hbm.xml) to the entityManager without using the sessionFactory bean?

Thanks in advance for you suggestions

** EDIT ** fro JB Nizet's suggestion, also come up with another solution

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

    entityManagerFactory.setDataSource(dataSource);

    // ...

    entityManagerFactory.setMappingResources("META-INF/named-queries.hbm.xml");

    return entityManagerFactory;
}

and in my DAO/Service, i can still get hibernate session with:

private Session getSession() {
        //return this.sessionFactory.getCurrentSession();
        return this.entityManager.unwrap(Session.class);
    }

But if someone nows if we can do the same thing with spring-boot auto-config with properties, it's welcomed!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Put the *.hbm.xml files under the src/main/resources folder and Spring Boot can automatically scan for them.

If you want to specify the location in the application.properties file, define them to the spring.jpa.mapping-resources attribute.

spring.jpa.mapping-resources=hibernate/MyMapping.hbm.xml,hibernate/MyMapping2.hbm.xml

Tested in SpringBoot 2.1.3, following is the folder structure

src/main/resources/hibernate : Store all the *.hbm.xml files
src/main/resources/application.properties : define the spring boot properties

And if you want to get the hibernate session in your Dao classes, define them as follows:

@Repository
@Transactional
public class XxxDao {
    @Autowired
    private EntityManager entityManager;

    private Session getSession() {
        return entityManager.unwrap(Session.class);
    }
...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...