Using @Table Hibernate

Asked

Viewed 74 times

0

How do I use the annotation @Table of Hibernate using a variable that brings the table name for example @Table(name = "dep4"+Dados.getCodEmp)? 'Cause I’m using it that way but I was presented with the following mistake:

The value for annotation attribute Table.name must be a constant expression

I need this because in the database of the system in question the tables are generated with the following prefix nomenclature + the code assigned to the company, for example dep4+(CodEmp). This code I take from a text file that is inside the application directory.

  • I can do one thing, but I don’t understand this "dep4"+Dados.getCodEmp. Is that the table name or would it be a "prefix"? Type: ("dep4"+Dados.getCodEmp) + "_nome_da_tabela"

  • The "dep4"+Data.getCodEmp would be the name of my table, where the dep4 is the default prefix of the table name Data.getCodEmp is where I get the company code to compose the table name. For example I am logged in to the bank with the code company 01 then the name of my table would be dep401

  • Oh yes, the solution I had as a suggestion was for another purpose. You can generate the names dynamically, but not by annotating @Table: https://stackoverflow.com/a/1507980/4056678

2 answers

1

I’m afraid I can’t do this.

@Table is a Java annotation, and Java annotations cannot receive values in time from Runtime. Java annotations can only receive constants, as the error message informed you.

-1

You must create a Generating Database Schemas or Interceptors for the schemed your database.

public class Main {

   public static void main(String[] args) {
     Persistence.generateSchema("samplePU", null);
   }
}

Take a look at the links.

  • 1

    While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - Of Revision

  • The link is not an answer to his fate. I just put it to him to take a look ..

  • These are not my rules, these are Stackoverflow rules, I recommend you read https://answall.com/tour

Browser other questions tagged

You are not signed in. Login or sign up in order to post.