Is it possible with hibernate to set dynamically the name of table and the fields mentioned in the entity class with annotations. I found that is something called naming strategy but I didn't understand it well. First, I wondered what are the possible solutions to dynamically define the name of table as well as the name of fields? Second, can someone explain to me the main role of the class ImprovedNamingStrategy and cite an example of use.
2 Answers
From "Java Persistence with hibernate" - You can extend ImprovedNamingStrategy
class (Which implements NamingStrategy
interface and provides default behavior) and override methods
public String tableName(String tableName) {
return "YourTableName";
}
There are other methods as well. Hope this helps!!

- 1,164
- 1
- 10
- 22
-
Assume that I have in my data base tow existed tables myTable1 and myTable2. I would like to map myTable1 to FirstEntity and myTable2 to SecondEntity. How to do that with ImprovedNamingStrategy ? – housseminfo May 27 '15 at 08:40
-
Why would you use ImprovedNamingStrategy for that? You can put @Table(name="myTable1") annotation in your entity class. – Ouney May 27 '15 at 09:03
-
In fact, I would like to externalize the name of the table to that the entity class should map in a configuration file because my application will be used by many clients and every client has his own data base. thus, To deploy the application the client should be mention the name of table corresponding with each entity That's why I neeed to externalize the parameters specicifying the database schema. Hope I was Specific. @Ouney – housseminfo May 27 '15 at 09:15
-
Not sure if i got it right...but you can use a placeholder in your hbm.xml file (not sure how to do it with annotation) e.g. &tableName and then create a file say tableNames.dtd and assign the tableName attribute value in that dtd. Include this dtd in your hbm.xml dtd. – Ouney May 27 '15 at 09:39
If you are using spring , u can have a look at SpringNamingStrategy.It already extends the ImprovedNamingStrategy.If required u can override the classToTableName method.If you are using spring-boot , this class name can be externalized with the property spring.jpa.hibernate.naming-strategy . http://docs.spring.io/autorepo/docs/spring-boot/1.1.5.RELEASE/api/org/springframework/boot/orm/jpa/SpringNamingStrategy.html#SpringNamingStrategy()
Hope this helped.

- 13
- 5