Entity Class
First we will create an object or hibernate POJO class to store phone information. Also this class will be an Entity class and will be linked with Phone table in database.
Create a java class Phone.java under com.spring.demo.model package and copy following code into it.
Note:
First we’ve annotated the class with @Entity which tells Hibernate that this class represents an object that we can persist.
The @Table(name = "PHONE") annotation tells Hibernate which table to map properties in this class to. The first property in this class is our object ID which will be unique for all events persisted. This is why we’ve annotated it with @Id.
The @GeneratedValue annotation says that this value will be determined by the datasource, not by the code.
The @Column(name = "name") annotation is used to map the property name column in the PHONE table.
The 2nd @Column(name = "review") annotation is used to map the property review column in the PHONE table.
@Override toString() is useful for logging, debugging, or any other circumstance where you need to be able to render any and every object you encounter as a string.
Implement equals() and hashCode()
Phone.java
package com.spring.demo.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Entity bean with JPA annotations Hibernate provides JPA implementation
*/
@Entity
@Table(name = "PHONE")
public class Phone {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "name")
private String name;
@Column(name = "review")
private String review;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getReview() {
return review;
}
public void setReview(String review) {
this.review = review;
}
@Override
public String toString() {
return "Phone [id=" + id + ", name=" + name + ", review=" + review + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Phone other = (Phone) obj;
if (id != other.id)
return false;
return true;
}
}