3

I'm using Spring Boot with JPA to connect with my database. I need to set "catalog" property in @Table annotation on entities.

Basically is not a problem

@MappedSuperclass
@NoArgsConstructor
@Table(catalog = "catalog_name")
public abstract class AbstractEntity

but I have to find easy way to change the catalog value, cause that I have different values on different environments.

Could anyone know any other way how to change this by for example application.properties file instead of hardcoded string?

I would be grateful for any solution.

Mateusz Nowak
  • 41
  • 2
  • 6
  • Like using `orm.xml` for ORM information perhaps (instead of hardcoding it in code), and then you don't have to recompile things. –  Jul 10 '18 at 12:49
  • Thanks for the answer, It's seems logic. When I'm using Spring Data JPA maybe there is some property? Or do you know where in orm.xml configure it? Because using Spring Data I don't have such file at all. – Mateusz Nowak Aug 06 '18 at 07:10

2 Answers2

0

As far as I know you cannot edit annotation properties dynamically. Have a look at this post.

Maybe try having multiple abstract entities with it's own catalog name and do a switch based on an application.properties property to get the right one.

Alexandru Hodis
  • 372
  • 4
  • 14
  • How you will make the switch based on property? It's the case what is difiicult in this. I want to find some solution different from annotation if I can't edit annotation properties dynamically. – Mateusz Nowak Aug 06 '18 at 07:08
  • You can get any property using [this](https://stackoverflow.com/questions/30528255/how-to-access-a-value-defined-in-the-application-properties-file-in-spring-boot). And then in you method instantiate classes base on that property. But you will need to define all classes that you will work with and all the tables associated. – Alexandru Hodis Aug 06 '18 at 10:28
0

Maybe such approach will work:

  1. Add property to maven profile
  2. Add property catalog.name to .properties file
  3. Load properties file with Spring @PropertySource annotation
  4. Do something like this:

    @Table(catalog = "${catalog.name}")

mdziob
  • 1,116
  • 13
  • 26