@Entity 和 @Table 中的名称属性 [英] Name attribute in @Entity and @Table

查看:28
本文介绍了@Entity 和 @Table 中的名称属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有疑问,因为@Entity 和@Table 中都有 name 属性

I have a doubt, because name attribute is there in both @Entity and @Table

例如,我可以为 name 属性设置相同的值

For example, I'm allowed to have same value for name attribute

@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?

推荐答案

@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

因此,在第一种情况下,您的表和实体将具有相同的名称,这将允许您在编写 HQL 或 JPQL 时使用与实体相同的名称访问您的表.

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.

在第二种情况下,在编写查询时,您必须使用 @Entity 中给出的名称,@Table 中给出的名称将用于命名表中的表数据库.

And in second case while writing queries you have to use the name given in @Entity and the name given in @Table will be used to name the table in the DB.

因此,在 HQL 中,您的 someThing 将引用数据库中的 otherThing.

So in HQL your someThing will refer to otherThing in the DB.

这篇关于@Entity 和 @Table 中的名称属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆