Please, try this...
- Create class SystemProps
public class SystemProps {
private static Map<String,String> props = null;
public static void loadPropsAll(){
props = new HashMap<>();
File file = null;
try {
file = ResourceUtils.getFile("classpath:application.properties");
loadProps(file,props);
String x = SystemProperties.get("spring.profiles.active");
if(x != null) {
file = ResourceUtils.getFile("classpath:application-" + x + ".properties");
loadProps(file, props);
}
}catch (Exception e){e.printStackTrace();}finally {
}
}
private static void loadProps(File file, Map<String,String> props){
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
while (br.ready()){
String line = br.readLine();
System.out.println(line);
if(!line.startsWith("#") && line.indexOf("=")!=-1){
props.put(""+line.split("=")[0].trim(), line.split("=")[1].trim());
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String getProp(String name){
if(props==null)
loadPropsAll();
return props.get(name);
}
}
- Create custom naming strategy for jpa
public class CustomPhysicalNamingStrategy extends SpringPhysicalNamingStrategy {
@Override
public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment) {
if(name.getText().startsWith("$")){
String x = name.getText().replace("$","").replaceAll("\\{","").replaceAll("}","");
String schema = SystemProps.getProp(x);
return super.toPhysicalSchemaName(new Identifier(schema,name.isQuoted()), jdbcEnvironment);
}
return super.toPhysicalSchemaName(name, jdbcEnvironment);
}
}
- in application.properties file add properties...
spring.jpa.hibernate.naming.physical-strategy=my.package.CustomPhysicalNamingStrategy
my_schema_name_property=SCHEMA_NAME_2
Now you can use it
@Entity
@Table(name"TABLE_NAME_1", schema="${my_schema_name_property}")
public class EntityName1{
...
}