Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
784 views
in Technique[技术] by (71.8m points)

java - In @Table(name = "tableName") - make "tableName" a variable in JPA

I am using JPA and I need to make the "tableName" a variable.

In a database, I have many tables, and my code needs to access the table where I specify it to read.

@Entity
@Table(name = "tableName")
public class Database implements Serializable {...............}

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do something like this, if thats your concern, I guess. Never tried it, its just a wild guess. But thats the usual practice -- I follow for Named Queries; yes, that's a different thing altogether.

@Entity
@Table(name = Database.tableName)
public class Database implements Serializable {
    public static final String tableName = "TABLE_1";
    ...............
}

But I don't see why anyone would do that. Could you tell us what are you up to? Why you have few tables exactly same definition?

[Edited]

I tried your solution. It did not work, it says: The value for annotation attribute Table.name must be a constant expression.

So, isn't that clear enough? I mean you can't do that. And I believe its quite logical. If you want Hibernate to generate your schema then you can define all the entities you want, in the schema, and with the appropriate relationships.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...