Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA having multiple orm.xml files

Based on my last post: Spring JPA long SQL String for NativeQuery

I've managed to find a "place" to write clean SQLs so that I can directly copy and paste it in the DB software for faster testing.

But another problem comes, if I wan to segregate my orm.xml file, say:

  1. student_orm.xml
  2. teacher_orm.xml

I tried renaming the files as seen above, but it says the methods in the files are not found during startup. Any idea how I can let the project understand the multiple orm.xml files?

like image 298
Henry Avatar asked Nov 04 '25 16:11

Henry


1 Answers

Probably you already found the solution, but still post mine here in case others are still struggling with this.

When working with Spring JPA and Spring Boot, you may be familiar with the way we configure data sources. So while configuring the entity manager factory, set the mapping resources to the (set of) ORM files you have, as below. This will override the default "META-INF/orm.xml" that Spring Boot does for you.

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

    // ..
    // other configurations removed for brevity
    // ..

    factory.setMappingResources("META-INF/orm/student_orm.xml", "META-INF/orm/teacher_orm.xml");

    return factory;
}
like image 86
Nguyen Tuan Anh Avatar answered Nov 06 '25 08:11

Nguyen Tuan Anh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!