Difference between Entity and Table annotation - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Sunday, June 2, 2019

Difference between Entity and Table annotation

@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

  1. @Entity(name = "someName") : this name will be used to name the Entity
  2. @Table(name = "otherName") : 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.
package com.walking.techie.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@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. You can refer this from here. 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

In this 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 the entity name you use in your JPQL will refer to otherName in the DB.

1 comment :

  1. Useless article. No major differences mentioned here but rather how to write the query.

    ReplyDelete