spring data with boot in details
spring data with boot in details
---------------------------
1. hello world
2. CURD operation
3. turning off repo definations
4. derived quaries from method names
5. relational, logical, date related,containing checking quaries
6. JPQL data with spring data, Sort quaries result using method arguments
7. pagination
8. joins
spring.jpa.hibernate.ddl-auto=update
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
spring.jpa.show-sql=true
spring.datasource.url= jdbc:mysql://localhost:3306/data
spring.datasource.username=root
spring.datasource.password=root
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long bookId;
private String title;
private Date publishDate;
private int pageCount;
private double price;
}
@SpringBootApplication
public class SpringDataApplication implements CommandLineRunner {
@Autowired
private BookRepo repo;
@Override
public void run(String... arg0) throws Exception {
repo.save(new Book("java is fun", new Date(), 300, 340));
repo.save(new Book("java is fun", new Date(), 300, 340));
}
}
CURD operation
-------------
-> adding records
repository.delete(1L);
repository.delete(repository.findOne(2L));
repository.delete(repository.findAll(new ArrayList<Long>(){{
add(3L);
add(4L);
}}));
repository.deleteInBatch(repository.findAll(new ArrayList<Long>(){{
add(5L);
add(6L);
}}));
repository.deleteAll();
repository.deleteAllInBatch();
for(Book book:books){
System.out.println(book);
}
3. turning off repo definations
----------------------------------
-> lets say our requirment is to create an safe repo, only for reading
purpose
-> use @NoRepositoryBean, Spring jpa will only give implementation for
methods that are mentioned in interface!
@NoRepositoryBean
public interface ReadOnlyRepository<T,ID extends Serializable> extends
Repository<T, ID> {
T findOne(ID id);
Iterable<T> findAll();
}
class Person{
private firstName;
Ex:
public interface PersonRepository extends CrudRepository<Person,
String>{
public List<Person>findByFirstName(String firstName);
}
Agenda:
-------
How derived queries are constructed?
@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
More examples:
-------------
testing :
---------
List<Book> books=repository.findByTitleEndingWith("action");
for(Book b: books)
System.out.println(b.getTitle());
relational comparision methods in derived quaries
----------------------------------------------------
testing:
---------
for(Book b:repository.findByTitleContainingOrderByTitleDesc("a")){
System.out.println(b);
}
6. JPQL data with spring data, Sort quaries result using method arguments
--------------------------------------------------------------------------
testing:
---------
Named Quaries
---------------
Demo:
......
}
.....
testing:
---------
=> Pagination is imp tech for large result set to displayed to a web page
=> breading down larger data set to sub set... pagination
=> serch engine
Spring provide paging and sorting repo out of the box....contain several
method to support pagination
Hello World:
to see record of page one (0 for first page) and three records
to see record of page two (1 for first page) and three records
ex:
for(Book b:repository.findAll(new PageRequest(0,3))){
System.out.println(b);
}
Sorting records:
----------------
Sorting using Spring JPA is very easy:
for(Book b:repository.findAll(new
Sort(Sort.Direction.DESC,"pageCount"))){
System.out.println(b);
}
Getting sorted first sort Book as per aurthor lastName and then as per
pageCount
-----------------------------------------------------------------------------
--------
for(Book b:repository.findAll(new
Sort(Sort.Direction.ASC,"author.lastName").and(new
Sort(Sort.Direction.DESC,"pageCount")))){
System.out.println(b);
}
Alternative:
==============
@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
Using it:
----------
Page page=repository.findByPageCountGreaterThan(120, new PageRequest(0,3));
for (Book b : repository.findByPageCountGreaterThan(120, new
PageRequest(0,3))) {
System.out.println(b);
}
Slice?
-----
@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
How to use?
---------
Slice slice = repository.findByPageCountGreaterThan(120, new
PageRequest(0,3));
More efficient then Page, but do not give imp inventory information such as
getTotalPages()
Joins:
---------
one to many:
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long bookId;
private String title;
private Date publishDate;
private int pageCount;
private double price;
@ManyToOne
@JoinColumn(name = "book_category_id")
private BookCategory bookCategory;
@Entity
@Table(name = "book_category")
public class BookCategory {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@Repository
public interface BookCategoryRepository extends JpaRepository<BookCategory,
Integer>{
}
b.setBookCategory(bookCategory);
b2.setBookCategory(bookCategory);
bookCategory.getBooks().add(b2);
bookCategory.getBooks().add(b);
repo.save(bookCategory);
https://roytuts.com/spring-data-jpa-namedquery-and-namedqueries-example/
https://roytuts.com/spring-boot-data-jpa-left-right-inner-and-cross-join-examples/