4

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?

Henry
  • 631
  • 10
  • 21

2 Answers2

5

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;
}
Nguyen Tuan Anh
  • 1,036
  • 8
  • 14
2

Yes it is possible to have multiple orm.xml files on the same application. But remember you need to reference them inside your persistence.xml with the <mapping-file>..</mapping-file> tag.

In your case, and let's say these orm.xml files are under META-INF/orm folder, your persistence.xml will have the following mappings:

<mapping-file>META-INF/orm/student_orm.xml</mapping-file>
<mapping-file>META-INF/orm/teacher_orm.xml</mapping-file>
Atef
  • 1,294
  • 1
  • 14
  • 27
  • 2
    Do you have the full config example for the persistence.xml without the datasource config? Because from the samples I saw online, it requires that. And, my project is able to run without a persistence.xml, and it technically doesn't need the datasource – Henry Jun 07 '20 at 12:41