使用 JPA 时,@Entity 和 @Table 中的 name 参数有什么区别?

发布于 2024-12-02 02:35:41 字数 789 浏览 1 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫瑾少女 2024-12-09 02:35:41

@Table 是可选的。将 POJO 类注释为实体需要 @Entity,但 name 属性不是必需的。

如果您有一个

 @Entity
 class MyEntity {}

名为“MyEntity”的 A 类表,并且实体名称将为 MyEntity。您的 JPQL 查询将是:

 select * from MyEntity

在 JPQL 中,您始终使用实体名称,默认情况下它是类名称。

如果您有一个类

 @Entity(name="MyEntityName")
 @Table(name="MyEntityTableName")
 class MyEntity {}

,则会创建一个名为 MyEntityTableName 的表,并且实体名称为 MyEntityName

您的 JPQL 查询将是:

 select * from MyEntityName

@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

 @Entity
 class MyEntity {}

A table with name "MyEntity" will be created and the Entity name will be MyEntity. Your JPQL query would be:

 select * from MyEntity

In JPQL you always use the Entity name and by default it is the class name.

if you have a class

 @Entity(name="MyEntityName")
 @Table(name="MyEntityTableName")
 class MyEntity {}

then a table with name MyEntityTableName is created and the entity name is MyEntityName.

Your JPQL query would be :

 select * from MyEntityName
寂寞陪衬 2024-12-09 02:35:41

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

寂寞陪衬 2024-12-09 02:35:41

@Entity 对于模型类很有用,表示这是实体或表

@Table 用于为表提供任何特定名称(如果您想提供任何不同的名称)

注意:如果您不使用 @Table 那么 hibernate 会默认认为 @Entity 是您的表名

@Entity    
@Table(name = "emp")     
public class Employee implements java.io.Serializable { }

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

Note: if you don't use @Table then hibernate consider that @Entity is your table name by default

@Entity    
@Table(name = "emp")     
public class Employee implements java.io.Serializable { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文