Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between the name argument in @Entity and @Table when using JPA?

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]
like image 862
soc Avatar asked Aug 31 '11 15:08

soc


People also ask

What is the difference between @entity and @table in JPA?

@Table's name attribute is the actual table name. @Entitiy's name is useful if you have two @Entity classes with the same name and you need a way to differentiate them when running queries.

What is the difference between @entity and @table annotation?

Entity means the class which you will use in your program and Table means the actual Database table that you will access through your program. Hibernate being an ORM requires you to declare the relationship between the tables and the classes mapping to them.

What is difference between @entity and @table in spring boot?

You need @Entity but @Table is an optional annotation that you can override default table name, schema name etc.

What does @entity annotation mean?

@Entity annotation defines that a class can be mapped to a table. And that is it, it is just a marker, like for example Serializable interface.


3 Answers

@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 
like image 64
Dhanush Gopinath Avatar answered Sep 23 '22 07:09

Dhanush Gopinath


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.

like image 24
Maurício Linhares Avatar answered Sep 21 '22 07:09

Maurício Linhares


@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 { }
like image 44
Bhuwan Tripathi Avatar answered Sep 24 '22 07:09

Bhuwan Tripathi