使用 JPA 时,@Entity 和 @Table 中的 name 参数有什么区别?
我使用的是 JPA2,@Entity
和 @Table
都有一个 name
属性,例如:
@Entity(name="Foo")
@Table (name="Bar")
class Baz
我应该使用什么,哪些是可选的?
在我的具体情况下,我有一个类 User
和一个类 Group
,它们有额外的要求(据我所知),因为它们是 SQL 中的保留字。
一个可行的解决方案是什么样子的?在编写查询时我应该用哪个名称来引用该实体?
更新:我将 name="GROUPS"
添加到 Group
中的两个注释中,并对 User
执行相同的操作,但现在我收到此错误
Exception Description: The table [USERS] is not present in this descriptor.
Descriptor: RelationalDescriptor(example.Group --> [DatabaseTable(GROUPS)])
:这个错误
Internal Exception: java.sql.SQLException: Table not found in statement [SELECT ID, MAXIMUMROLE, MEMBERSHIPS_ID FROM USERS]
I'm using JPA2 and both @Entity
and @Table
have a name
attribute, e. g.:
@Entity(name="Foo")
@Table (name="Bar")
class Baz
What should I use, which ones are optional?
In my specific case I have a class User
and a class Group
, which have additional requirements (as far as I understand) because they are reserved words in SQL.
How would a working solution look like and with which name would I refer to the entity when writing queries?
Update: I added name="GROUPS"
to both annotations in Group
and did the same for User
, but now I get this error:
Exception Description: The table [USERS] is not present in this descriptor.
Descriptor: RelationalDescriptor(example.Group --> [DatabaseTable(GROUPS)])
and this error
Internal Exception: java.sql.SQLException: Table not found in statement [SELECT ID, MAXIMUMROLE, MEMBERSHIPS_ID FROM USERS]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Table 是可选的。将 POJO 类注释为实体需要 @Entity,但 name 属性不是必需的。
如果您有一个
名为“MyEntity”的 A 类表,并且实体名称将为 MyEntity。您的 JPQL 查询将是:
在 JPQL 中,您始终使用实体名称,默认情况下它是类名称。
如果您有一个类
,则会创建一个名为 MyEntityTableName 的表,并且实体名称为 MyEntityName。
您的 JPQL 查询将是:
@Table is optional. @Entity is needed for annotating a POJO class as an entity, but the name attribute is not mandatory.
If you have a class
A table with name "MyEntity" will be created and the Entity name will be MyEntity. Your JPQL query would be:
In JPQL you always use the Entity name and by default it is the class name.
if you have a class
then a table with name MyEntityTableName is created and the entity name is MyEntityName.
Your JPQL query would be :
@Entity
中的 name 用于 JPA-QL 查询,它默认为不带包的类名(或 Java 术语中的非限定类名),如果您更改它必须确保在构建查询时使用此名称。@Table
中的name 是保存该实体的表名称。The name in
@Entity
is for JPA-QL queries, it defaults to the class name without package (or unqualified class name, in Java lingo), if you change it you have to make sure you use this name when building queries.The name in
@Table
is the table name where this entity is saved.@Entity
对于模型类很有用,表示这是实体或表@Table
用于为表提供任何特定名称(如果您想提供任何不同的名称)注意:如果您不使用
@Table
那么 hibernate 会默认认为@Entity
是您的表名@Entity
is useful with model classes to denote that this is the entity or table@Table
is used to provide any specific name to your table if you want to provide any different nameNote: if you don't use
@Table
then hibernate consider that@Entity
is your table name by default