7

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!

5
  • You could place your named query using annotations on Entities java file. Commented Sep 12, 2015 at 7:13
  • Why don't you set the mapping resources on the LocalContainerEntityManagerFactoryBean instead? docs.spring.io/spring/docs/current/javadoc-api/org/… Commented Sep 12, 2015 at 7:28
  • @Jay Thanks for you reply Jay. As mentionned, I'm using a legacy code and i have to integrate the mapping file as it is. Commented Sep 12, 2015 at 7:32
  • @JBNizet Thanks for your suggestion JB. Is it possible to set mapping resource using spring-boot properties (and auto config mecanism) or do i have to define the LocalContainerEntityManagerFactoryBean myself? Commented Sep 12, 2015 at 7:36
  • Define your mapping-file in a persistence.xml., and ditch the custom configured one. Or migrate your named-queries.hbm.xml to an orm.xml (the JPA equivalent of this file) and place it in META-INF and it will be loaded automatically. Commented Feb 27, 2019 at 12:18

2 Answers 2

7

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);
    }
...
}
Sign up to request clarification or add additional context in comments.

1 Comment

For me (using hibernate 5.5) it works with spring.jpa.mapping-resources property only. Putting the files into src/main/resources without listing them in the property did not work.
0
@Autowired
private ResourceLoader rl;


@Bean
public LocalSessionFactoryBean sessionFactory() throws IOException {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setMappingLocations(loadResources());
}

public Resource[] loadResources() {
    Resource[] resources = null;
    try {
        resources = ResourcePatternUtils.getResourcePatternResolver(rl)
            .getResources("classpath:/hibernate/*.hbm.xml");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return resources;
}

1 Comment

This code doesn't compile. sessionFactory() is not returning anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.