Java @Entity 和 @Table 中的名称属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18732646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Name attribute in @Entity and @Table
提问by
I have a doubt, because name attribute is there in both @Entity and @Table
我有一个疑问,因为 @Entity 和 @Table 中都有 name 属性
For example, I'm allowed to have same value for name attribute
例如,我可以对 name 属性使用相同的值
@Entity(name = "someThing")
@Table(name = "someThing")
and I can have different names as well for same class
我也可以为同一个班级使用不同的名字
@Entity(name = "someThing")
@Table(name = "otherThing")
Can anybody tell me what is the difference between these two and why we have same attribute in both?
谁能告诉我这两者之间有什么区别以及为什么我们在两者中具有相同的属性?
采纳答案by ankit
@Entity(name = "someThing") => this name will be used to name the Entity @Table(name = "someThing") => this name will be used to name a table in DB
@Entity(name = "someThing") => this name will be used to name the Entity @Table(name = "someThing") => this name will be used to name a table in DB
So, in the first case your table and entity will have the same name, that will allow you to access your table with the same name as the entity while writing HQL or JPQL.
因此,在第一种情况下,您的表和实体将具有相同的名称,这将允许您在编写 HQL 或 JPQL 时使用与实体相同的名称访问您的表。
And in second case while writing queries you have to use the name given in @Entityand the name given in @Tablewill be used to name the table in the DB.
而在第二种情况下,同时编写查询,你必须使用指定的名称@Entity并定名为@Table将被用来命名在数据库表。
So in HQL your someThingwill refer to otherThingin the DB.
因此,在 HQL 中,您的someThing将引用数据库中的otherThing。
回答by Manbumihu Manavan
@Entity(name = "someThing")
=> this name will be used to identify the domain ..this name will only be identified by hql
queries ..ie ..name of the domain object
@Entity(name = "someThing")
=> 此名称将用于标识域 ..此名称将仅由hql
查询标识..ie ..name 域对象
@Table(name = "someThing")
=> this name will be used to which table referred by domain object..ie ..name of the table
@Table(name = "someThing")
=> 此名称将用于域对象引用的表..即..表的名称
回答by Coral
@Table's name attribute is the actual table name. @Entitiy's name is useful if you have two @Entity classes with the same name and you need a way to differentiate them when running queries.
@Table 的 name 属性是实际的表名。如果您有两个具有相同名称的 @Entity 类,并且在运行查询时需要一种区分它们的方法,则@Entitiy 的名称很有用。
回答by Bhuwan Tripathi
@Entity
is useful with model classes to denote that this is the entity or table
@Entity
与模型类一起用于表示这是实体或表
@Table
is used to provide any specific name to your table if you want to provide any different name
@Table
如果您想提供任何不同的名称,则用于为您的表提供任何特定名称
Note: if you don't use @Table
then hibernate consider that @Entity
is your table name by default and @Entity
must
注意:如果您不使用,@Table
则休眠认为@Entity
默认情况下这是您的表名,并且@Entity
必须
@Entity
@Table(name = "emp")
public class Employee implements java.io.Serializable
{
}