1

I have seen some posts talking about NamingStrategy but it is not exacting what I am looking for (or I don't know to to approach it).

I need, depending on some conditions, to change the name of the @Table in the @Entity class passing an ID, something like this:

@Entity
@Table(name = "SA1{companyID}")
public class Cliente implements Serializable, Cloneable {

That "companyID" will something like: 010, 020, 030

Is there any whay to achieve that?

Francisco Souza
  • 806
  • 15
  • 38
  • what do you mean "dynamically change" ? You want to change it somewhere and create a new EMF? – Neil Stockton May 12 '16 at 14:53
  • I have these tables **SA1010, SA1030, SA1040** with same structure, depending on what company the user is the selects (this is read only db) are made on different tables, and I would like to use the same entity class. – Francisco Souza May 12 '16 at 17:57
  • yes, and is that one EMF per company? or are you wanting to dynamically select the table mid-EMF ?! – Neil Stockton May 12 '16 at 18:06

2 Answers2

2

It will be better, if you use @MappedSuperclass

@MappedSuperclass
class Cliente {

  @Column
  private String xxx;

}

@Entity
class SA1010 extends Cliente { 


}
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
0

You can do it by implementing NamingStrategy

Please see posts below which will help you:

How do I configure JPA table name at runtime?

Change Table Name of an Entity on runtime?

Dynamic Naming Table with Hibernate

Community
  • 1
  • 1
shankarsh15
  • 1,947
  • 1
  • 11
  • 16