Name attribute in @Entity and @Table

后端 未结 4 1880
一向
一向 2020-12-01 02:30

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         


        
相关标签:
4条回答
  • 2020-12-01 02:55
    @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.

    0 讨论(0)
  • 2020-12-01 03:12

    @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    
    {
    
    }
    
    0 讨论(0)
  • 2020-12-01 03:17

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

    0 讨论(0)
  • 2020-12-01 03:19

    @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

    0 讨论(0)
提交回复
热议问题