在@Table(name =" tableName")中 - make" tableName" JPA中的变量 [英] In @Table(name = "tableName") - make "tableName" a variable in JPA
问题描述
我正在使用JPA,我需要将tableName变为变量。
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 {...............}
任何想法?
推荐答案
我猜你可以做这样的事情。 从未尝试过,只是一个狂野的猜测。但那是通常的做法 - 我遵循命名查询;是的,这完全是另一回事。
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?
[已编辑]
我尝试了你的解决方案。它没有
工作,它说:
注释属性Table.name的值必须是
是一个常量表达式。
I tried your solution. It did not work, it says: The value for annotation attribute Table.name must be a constant expression.
那么,还不够清楚吗?我的意思是你不能这样做。而且我相信它非常符合逻辑。如果您希望Hibernate生成您的模式,那么您可以在模式中定义所需的所有实体,并使用适当的关系。
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.
这篇关于在@Table(name =" tableName")中 - make" tableName" JPA中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!