Name attribute in @Entity and @Table

user2728475 picture user2728475 · Sep 11, 2013 · Viewed 97.6k times · Source

I have a doubt, because name attribute is there in both @Entity and @Table

For example, I'm allowed to have same value for name attribute

@Entity(name = "someThing")
@Table(name = "someThing")

and I can have different names as well for same class

 @Entity(name = "someThing")
 @Table(name = "otherThing")

Can anybody tell me what is the difference between these two and why we have same attribute in both?

Answer

ankit picture ankit · Sep 11, 2013
@Entity(name = "someThing") => this name will be used to name the Entity
@Table(name = "someThing")  => this name will be used to name a table in DB

So, in the first case your table and entity will have the same name, that will allow you to access your table with the same name as the entity while writing HQL or JPQL.

And in second case while writing queries you have to use the name given in @Entity and the name given in @Table will be used to name the table in the DB.

So in HQL your someThing will refer to otherThing in the DB.