Spring Boot Entity Class You must Know About [Indetail]

Entity --> [Which is used to create a table & DatabaseStructure) 


 import jakarta.persistence.Entity; 
import jakarta.persistence.GeneratedValue;
 import jakarta.persistence.GenerationType; 
import jakarta.persistence.Id;
 import jakarta.persistence.Table; 

@Entity 
@Table
 public class UserDetail { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) 
 private Long id; 


  1. GenerationType.AUTO Description: This option allows the persistence provider (JPA implementation, such as Hibernate) to choose the generation strategy for the primary key automatically. It may use different strategies based on the database and its configuration. 

  Usage: It's a good default choice, especially when you want to rely on the JPA provider to manage the key generation for you. 
 ##Code 
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO) 
private Long id;


  2. GenerationType.IDENTITY Description: With this strategy, the primary key is generated by the database when a new record is inserted. The database must support auto-increment columns or sequences. The generated key is returned after the insert operation. 

  Usage: This strategy is often used with MySQL and PostgreSQL, where you want the database to handle the primary key generation.
 ### Code 
 @Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Long id;



  3. GenerationType.SEQUENCE Description: This strategy uses a database sequence to generate primary keys. A sequence is an object that generates a sequence of unique numeric values in a database. This approach can provide better performance in some cases, especially in high-concurrency environments.

  Usage: Commonly used with databases like Oracle and PostgreSQL. 

 ### Code :
 @Id 
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_sequence") @SequenceGenerator(name = "my_sequence", sequenceName = "my_sequence_name", allocationSize = 1) 
private Long id; 



  4. GenerationType.TABLE Description: This strategy uses a separate table to generate the primary key. It involves creating a table that maintains the last used value for the primary key. This approach is more flexible but can be slower and more complex compared to the others. Usage: It is generally less commonly used due to performance considerations but can be useful in specific scenarios.


 ## Code Style 

 @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "my_table_gen") @TableGenerator(name = "my_table_gen", table = "my_key_table", pkColumnName = "key_name", valueColumnName = "key_value", allocationSize = 1)
 private Long id; 

 Summary 
 AUTO: Let JPA choose the strategy.
 IDENTITY: Use auto-incrementing primary key generation by the database. 
 SEQUENCE: Use database sequences to generate primary keys. 
 TABLE: Use a separate table to manage key generation. Choose the strategy based on your application's requirements and the capabilities of your underlying database.



 ## Need things for Entity Class
  1. You need Empty Constructuor _______________________________    public UserDetail() {

    // TODO Auto-generated constructor stub

    }

  2. Constructor with Variable Like : ______________public UserDetail(String name, String email) {

    super();

    this.name = name;

    this.email = email;

    }

  3. Getter And Setter 

  4. toString for All as we will deal as String for all______ @Override

    public String toString() {

    return "user [id=" + id + ", name=" + name + ", email=" + email + "]";

    }



## Sample of Entity

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table
public class UserDetail {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
public UserDetail(String name, String email) {
super();
this.name = name;
this.email = email;
}
public UserDetail() {
// TODO Auto-generated constructor stub
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "user [id=" + id + ", name=" + name + ", email=" + email + "]";
}
}



Comments

Popular posts from this blog

Spring Boot Basic Controller

content v mabager in spring boot