Create a Profile entity named Profile.kt with the @Entity annotation to convert this class into an entity class. Here is the code of this model class (the entire code can be found at the provided GitHub Link):
@Entity
class Profile : Serializable {
constructor(id: Long) {
this.id = id
}
constructor(name: String) {
this.username = name
}
-----
-----
@JsonProperty("contactNumber")
var contactNumber: String? = null
@JsonProperty("dob")
var dOB: Date? = null
@JsonProperty("city")
var city: String? = null
@JsonProperty("country")
var country: String? = null
}
In this class, we have 11 elements, which contain all the user's details. We have four constructors to use this model according to our tasks. Here are the constructors:
constructor(id: Long) {
----
----
}
constructor(name: String) {
----
----
}
constructor(id: Long, name: String, password: String) {
----
----
}
constructor(username: String, password: String, email: String, accCreatedTime: Instant,
firstName: String?, lastName: String?, contactNumber: String?, dOB: Date?,
city: String?, country: String?) {
----
----
}
Now let's discuss the annotations that are used in this class:
@Id
@GeneratedValue
var id: Long? = 0
According to the previous code, we used @Id annotation on the id, which means that id is the primary key of the Profile entity. The @GeneratedValue annotation means it increments the value of id.
Here is a snippet of the code for the password object:
@JsonIgnore
@JsonProperty("password")
var password: String = ""
According to this code, @JsonIgnore uses variables or functions. If you use it, then the requested JSON won't show this variable. Here, we used it on the password, and that means no-one can fetch the password.
@JsonProperty defines that during the serialization and deserialization of JSON, it changes the visibility of the logical property of its element.