name attribute in @Entity and @Table

送分小仙女□ 提交于 2019-12-17 17:38:36

问题


I have a doubt name attribute is there in both

@Entity and @Table

for example I am 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 ?


回答1:


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




回答2:


@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




回答3:


@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    
{

}



回答4:


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



来源:https://stackoverflow.com/questions/18732646/name-attribute-in-entity-and-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!