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(n
@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.
@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 and @Entity
must
@Entity
@Table(name = "emp")
public class Employee implements java.io.Serializable
{
}
@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.
@Entity(name = "someThing")
=> this name will be used to identify the domain ..this name will only be identified by hql
queries ..ie ..name of the domain object
@Table(name = "someThing")
=> this name will be used to which table referred by domain object..ie ..name of the table