Thursday 15 September 2022

                             JPA (Java Persistence API)

This method is use to map any Database table with Java class. 


Take example, we have a table named user_profile in schema: user

We run an SQL query to create a table:  

 CREATE TABLE [user_profile](
    [user_ID] [bigint] IDENTITY(1,1),
    [user_code] [varchar](50) NOT NULL,
    [user_name] [varchar](50),
    [Address] [varchar](25),
    [City] [varchar](50),
    [AppointmentDate] [datetime],

  ) ON [PRIMARY]



Now, we create a Java class which will be mapped to the table as well as fields present in it. The purpose of this is that we can either use the Java Class to fetch data or insert data into table.


UserEntity.java

@Entity
@Table(name = "user_profile")
public class UserEntity implements Serializable {

@Id
@Column(name = "user_id")
private int userId;

@JsonInclude(Include.NON_EMPTY)
@Column(name = "user_code")
private int userCode;

@JsonInclude(Include.NON_EMPTY)
@Column(name = "user_name")
private String userName;

@Column(name = "Address")
private String address;

@Column(name = "City")
private String city;

@Columns(name = "Appointment")
private Date appointment;



}

No comments:

Post a Comment