This post is continuing from the last Setup post of a Sprig Data project.
To Persist a record in the database we require an object, So lets write our Model Class.
package com.sd.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="CITY")
public class City {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long cityId;
@Column(name="CITY_NAME")
private String name;
@Column(name="CITY_POPULATION")
private Long population;
public Long getCityId() {
return cityId;
}
public void setCityId(Long cityId) {
this.cityId = cityId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getPopulation() {
return population;
}
public void setPopulation(Long population) {
this.population = population;
}
}
This model City class is simple to understand and does not require much explanation.
As explained in the first post, we need an interface to extend JpaRepository. So this is how we do it.
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.sd.model.City;
@Repository
public interface CityRepository extends JpaRepository<City, Long> {
}
That will be enough to pick our Model and get the job started.
Write this basic code and you will get the object persisted.
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(SpringDataApplication.class, args);
CityRepository cr = context.getBean(CityRepository.class);
cr.save(getCity());
}
private static City getCity(){
City city = new City();
city.setCityId(1l);
city.setName("Shimla");
city.setPopulation(100000l);
return city;
}
I will explain how this worked in the next post.
To Persist a record in the database we require an object, So lets write our Model Class.
package com.sd.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="CITY")
public class City {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long cityId;
@Column(name="CITY_NAME")
private String name;
@Column(name="CITY_POPULATION")
private Long population;
public Long getCityId() {
return cityId;
}
public void setCityId(Long cityId) {
this.cityId = cityId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getPopulation() {
return population;
}
public void setPopulation(Long population) {
this.population = population;
}
}
This model City class is simple to understand and does not require much explanation.
As explained in the first post, we need an interface to extend JpaRepository. So this is how we do it.
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.sd.model.City;
@Repository
public interface CityRepository extends JpaRepository<City, Long> {
}
That will be enough to pick our Model and get the job started.
Write this basic code and you will get the object persisted.
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(SpringDataApplication.class, args);
CityRepository cr = context.getBean(CityRepository.class);
cr.save(getCity());
}
private static City getCity(){
City city = new City();
city.setCityId(1l);
city.setName("Shimla");
city.setPopulation(100000l);
return city;
}
I will explain how this worked in the next post.