Project creation with Spring Boot
First, we need to create a Spring Boot with JPA and H2 using https://start.spring.io/. We generate it and we import it in our Eclipse.
MyTable entity
We will need an entity that will have as properties the same fields than the database table. We annotate the entity with JPA annotations.
MyTable.java
package com.luisgomezcaballero.springjpademo; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class MyTable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long myId; private String myString; private Date myDate; private Float myFloat; public MyTable() { } public MyTable(Long myId, String myString, Date myDate, Float myFloat) { super(); this.myId = myId; this.myString = myString; this.myDate = myDate; this.myFloat = myFloat; } public Long getMyId() { return myId; } public void setMyId(Long myId) { this.myId = myId; } public String getMyString() { return myString; } public void setMyString(String myString) { this.myString = myString; } public Date getMyDate() { return myDate; } public void setMyDate(Date myDate) { this.myDate = myDate; } public Float getMyFloat() { return myFloat; } public void setMyFloat(Float myFloat) { this.myFloat = myFloat; } @Override public String toString() { return "MyTable [myId=" + myId + ", myString=" + myString + ", myDate=" + myDate + ", myFloat=" + myFloat + "]"; } }
MyTable repository
Now we create a custom repository extending the one from Spring. In this case we are not creating new methods but we could do so in case we need specific queries.
MyTableRepository.java
package com.luisgomezcaballero.springjpademo; import org.springframework.data.repository.CrudRepository; public interface MyTableRepository extends CrudRepository<MyTable, Long> { }
Example
We create a demo method that will be executed on application start and will generate rows in the table of the H2 virtual database and after that it will perform queries on that information.
SpringJpaDemoApplication.java
package com.luisgomezcaballero.springjpademo; import java.util.Date; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class SpringJpaDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringJpaDemoApplication.class, args); } @Bean public CommandLineRunner demo(MyTableRepository repository) { return (args) -> { repository.save(new MyTable((long) 1, "myString1", new Date(), (float) 1)); repository.save(new MyTable((long) 2, "myString2", new Date(), (float) 2)); System.out.println("findAll():"); for (MyTable myTable : repository.findAll()) { System.out.println(myTable.toString()); } System.out.println(); MyTable myTable = repository.findOne(1L); System.out.println("findOne(1L):"); System.out.println(myTable.toString()); System.out.println(); }; } }
Repository
The code of this project is available at https://github.com/luisgomezcaballero/spring-jpa-demo.
So, what do you think ?