68

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]
Timotej Leginus
  • 304
  • 3
  • 18
soc
  • 27,983
  • 20
  • 111
  • 215

3 Answers3

104

@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
Community
  • 1
  • 1
Dhanush Gopinath
  • 5,652
  • 6
  • 37
  • 68
  • 5
    I (mis)learned JPA from the internet and I was providing the name of an existing MySQL table as the Entity name as a way of mapping the two. That worked for me but only coincidentally, because Hibernate (maybe all JPA implementations?) uses the Entity name as the default table name if @Table "name" isn't provided. However, as soon as I started using JPQL queries as opposed to CriteriaBuilder queries, I started getting "FooBar is not mapped" exceptions because I had unknowingly changed the way I needed to refer to the entity in queries. I hope that sharing my blunder helps someone. – spaaarky21 Dec 28 '12 at 19:16
32

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.

andr
  • 15,970
  • 10
  • 45
  • 59
Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
5

@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 { }
soc
  • 27,983
  • 20
  • 111
  • 215
Bhuwan Tripathi
  • 248
  • 3
  • 6