ChatGPT解决这个技术问题 Extra ChatGPT

Name attribute in @Entity and @Table

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?

@Entity is deprecated now.
@PhilipRego: While Hibernate's \@Entity (i.e. org.hibernate.annotations.Entity) is deprecated, JPA's \@Entity (i.e. javax.persistence.Entity) is alive and well.
Confused. In iOS CoreData, a table is called "entity", so are the both naming means a DB table just in different names? And each one serve for Hibernate or JPA. If yes, I'm wondering why they don't use the same dame name if they work together :)

M
Martin Peter

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


K
Kasun Siyambalapitiya

@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


E
Eiko

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

}

N
Nestor Milyaev

Here is a real-life example with some good practice tips, as I just spent 2 hours trying to figure out why my HQL query was throwing the

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: MyEntity is not mapped [SELECT e FROM MyEntity e ... exception when I used the my_entity in the @Entity annotation.

@Entity name is used to refer your entity throughout the application, notably in HQL queries, and @Table is the actual DB table name, for example:

@Entity(name = "SomeThing")
@Table(name = "some_thing")
public class SomeThing {
    @Id
    private Long id;
}

, then your JPA repository might be something like:

    @Repository
    public interface BikeOfferRepository extends JpaRepository<SomeThing, Long> {
    
        /** A contrived example as in reality you'd use built-in 
            query for this type of select */
        @Query("SELECT o FROM SomeThing WHERE o.id =:id") // <-- Here you use "SomeThing", the entity name in HQL
        SomeThing findAllByBikeOwner(@Param("id") Long id);
    }

BTW, it's a good pracitce to use class name or camel-case name for the entity name and lowercase spaced with underscores for a table and column names (as in my example above). See here more on DB naming conventions: https://www.sqlshack.com/learn-sql-naming-conventions/.

And in the actual database (or if you use old-style JDBC queries) you'd use:

select * from some_thing where id=xxx;

S
Soumyaansh

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


When do you have two entities with the same name? Is it when the class names are equal, but classes are in different packages (qualified names are different)?