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

后端 未结 3 1714
Happy的楠姐
Happy的楠姐 2020-12-24 01:55

I\'m using JPA2 and both @Entity and @Table have a name attribute, e. g.:

@Entity(name=\"Foo\")
@Table (name=\"Bar\")
         


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-24 02:16

    @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
    

提交回复
热议问题