什么是使用JPA时@Entity和@Table名称参数之间的区别? [英] What's the difference between the name argument in @Entity and @Table when using JPA?

查看:509
本文介绍了什么是使用JPA时@Entity和@Table名称参数之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JPA2无一不 @Entity @Table 有一个名称属性,E。 G:

  @Entity(NAME =富)
@Table(NAME =酒吧)
巴兹类

我应该用什么,哪些是可选的?

在我的特定情况下,我有一个类用户和类,它还有其他要求(据据我所知),因为它们是在SQL保留字。

如何将一个有效的解决方案看起来和这名字会写查询时,我指的是实体?

更新。我加了 NAME =组组中的两个注释,也做了同样的用户,但现在我得到这个错误:

 异常说明:本表[USERS]是不是在这个描述符present。
描述:RelationalDescriptor(example.Group - > DatabaseTable(组)])

 内部异常:java.sql.SQLException中:表在声明中没有找到[SELECT ID,MAXIMUMROLE,MEMBERSHIPS_ID了用户]


解决方案

@Table是可选的。需要@Entity注释为一个POJO类作为一个实体,但名称属性不是强制性的。

如果你有一个类

  @Entity
 myEntity所一流{}

与名称的表 myEntity所将被创建和实体名称为 myEntity所。您JPQL查询是:

 从myEntity所选择*

在JPQL你总是使用实体名称,默认情况下它是类名。

如果你有一个类

  @Entity(NAME =MyEntityName)
 @Table(NAME =MyEntityTableName)
 myEntity所一流{}

然后用名称表的 MyEntityTableName 创建和实体名称为 MyEntityName

您JPQL查询将是:

 从MyEntityName选择*

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?

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

Internal Exception: java.sql.SQLException: Table not found in statement [SELECT ID, MAXIMUMROLE, MEMBERSHIPS_ID FROM USERS]

解决方案

@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

这篇关于什么是使用JPA时@Entity和@Table名称参数之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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