Difference between Entity and Table annotation | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

@javax.persistence.Entity: Specifies that the class is an entity. This annotation is applied to the entity class.

@javax.persistence.Table: Specifies the primary table for the annotated entity. Additional tables may be specified using SecondaryTable or SecondaryTables annotation. If no Table annotation is specified for an entity class, the default values apply.

Difference between @Entity and @Table

  • @Entity(name = "entityName") : this name will be used to name the Entity
  • @Table(name = "tableName") : this name will be used to name a Table in DB

There will be only two valid scenario. Let’s understand this with examples. We will using persistence Message class through out this discussions.

Message.java

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Message {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "ID")
  private Long id;
 
  @Column(name = "MESSAGE")
  private String message;
 
  public Message(String message) {
    this.message = message;
  }
}

We will have both properties <property name="hbm2ddl.auto">create-drop</property> and <property name="show_sql">true</property> in hibernate.cfg.xml file. So that every time we run Hibernate application table will deleted if it exists in DB and create new one in DB and sql queries will print on console.

Scenario 1: Only @Entity annotation used

Scenario 1.1: When only @Entity annotation used on persistence class

@Entity
public class Message {
    ...
}
  • If you don’t use name element then table and entity name will be same as default; name of persistence class.
  • Hibernate Query: create table Message (ID bigint not null auto_increment, MESSAGE varchar(255), primary key (ID)) engine=InnoDB

Scenario 1.2: When @Entity annotation used with name element on persistence class

@Entity(name = "message")
public class Message {
    ...
}
  • If you use name element then table and entity name will be same as value of name element.
  • Hibernate Query: create table message (ID bigint not null auto_increment, MESSAGE varchar(255), primary key (ID)) engine=InnoDB
  • In this case 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.

Scenario 2: Both @Entity and @Table annotations used

Scenario 2.1: @Entity and @Table annotations used on persistence class

@Entity
@Table
public class Message {
    ...
}
  • This is same as scenario 1.1.
  • Hibernate Query: create table Message (ID bigint not null auto_increment, MESSAGE varchar(255), primary key (ID)) engine=InnoDB

Scenario 2.2: @Entity(name = “MESSAGE”) and @Table annotations used on persistence class

@Entity(name = "MESSAGE")
@Table
public class Message {
    ...
}
  • Hibernate Query: create table MESSAGE (ID bigint not null auto_increment, MESSAGE varchar(255), primary key (ID)) engine=InnoDB

Scenario 2.3: @Entity and @Table(name = “text_message”) annotations used on persistence class

@Entity
@Table(name = "text_message")
public class Message {
    ...
}
  • Hibernate Query: create table text_message (ID bigint not null auto_increment, MESSAGE varchar(255), primary key (ID)) engine=InnoDB

Scenario 2.4: @Entity(name = “MESSAGE”) and @Table(name = “text_message”) annotations used on persistence class

@Entity(name = "MESSAGE")
@Table(name = "text_message")
public class Message {
    ...
}
  • Hibernate Query: create table text_message (ID bigint not null auto_increment, MESSAGE varchar(255), primary key (ID)) engine=InnoDB

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s