16- Create JPA Employee (Model or Entity) and Repository
Creating Employee Model
springboot-backendd --> src/main/java --> net.javaguides.springboot.model --> Employee.java
package net.javaguides.springboot.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
@Table(name="employees")
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
@Column(name="first_name",nullable=false)
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="email")
private String email;
//Constructors using superclass
public Employee(){
}
//Constructors using field
public Employee( String firstName, String lastName, String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
//Getters and Setters
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Creating Employee Repository
springboot-backendd --> src/main/java--> net.javaguides.springboot.repository--> EmployeeRepository.java
package net.javaguides.springboot.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import net.javaguides.springboot.model.Employee;
/*Spring Data JPA internally provides @Repository Annotations so we no need
to add @Repository annotation to Employee Repository interface*/
public interface EmployeeRepository extends JpaRepository <Employee,Long> {
}
Comments
Post a Comment