0

I am trying to develop a spring mvc based application in which my db structure has identical multiple tables bearing different names that relate to loggedin user's project (for operational reasons this is necessary). This way new tables get created and closed programmatically. Now how one can connect to multiple tables using the same model. For example:

@Entity
@Table(name="TABLE_X12")
public class User implements Serializable {
    public static final long serialVersionUID = 1L;

        @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
        private int id;

        @NotEmpty
        @Column(name="id_key", unique=true, nullable=false)
        private String id;
        ...
        ...
   }    

The table name "TABLE_X12" will be different for each logged-in user while the rest of the code and structure (like queries etc.) remain same.

Is it possible. I have done it in php because I simply get the applicable project name, generate the table name and query it accordingly.

user6404606
  • 143
  • 3
  • 12
  • like this? https://stackoverflow.com/questions/8573468/hibernate-data-object-with-a-dynamic-table-name-by-annotations – msp Feb 16 '18 at 12:43
  • Exactly, but once the class initiated, one cannot dynamically change name even if we put in a session. That is what I understood, unlike php. I feel, the best option seems to be POJO approach. I am not an expert in spring mvc. – user6404606 Feb 16 '18 at 14:45

1 Answers1

0

You don't need to create a POJO entity class and instantiate it at run time. I believe what you seek is the Hibernate dynamic models. Refer to the docs here

http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#persistent-classes-dynamicmodels.

mwangii
  • 11
  • 2
  • Please don't just add links as an answer. Try quoting the source and explain it in your answer. – msp Feb 16 '18 at 12:53
  • Thank you for the reply. The amount of information is too large for me to come to conclusion on its usefulness. I am now diving into the problem again. – user6404606 Feb 16 '18 at 23:49