Creating a model

In this project, we will create a REST API to see the list of user details where we can get a username, email ID, and contact number. So let's create a model of a user where the class name is UserModel.kt.

Here is the code of the model class:

@Entity
@Table(name="user_jpa")
@EntityListeners(AuditingEntityListener::class)
data class UserModel(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
var id: Long = 0,

@NotBlank
@Column(name = "name")
var name: String ?= null,

@NotBlank
@Column(name = "email")
var email: String ?= null,

@NotBlank
@Column(name = "contact_number")
var contact_number: String ?= null
)

Here, our UserModel class has the following fields:

Unlike JDBC, you don't need to create any table manually in your database. JPA will create a table using the UserModel. Let's look at how to create a table in our database using this UserModel object: