Saturday, 14 September 2019

Spring boot Adding JPA Capability

Adding JPA Capability


adding spring-boot-starter-data-jpa dependency

➤Adds Spring JDBC/Transaction Management

  • Adds Spring ORM
  • Adds Hibernate/entity manager
  • Adds Spring Data JPA subproject


➤does not add driver of database
needed to add manually (HSQL db)

SPRING Data JPA


  • this is typical web application architecture
  • REST controller provides CRUD interface to client
  • DAO provide CRUD interface to DB
              JSON/XML
                      ⬍
               Controller
                      ⬇
               Services
                     ⬇
                DAO
                    
                  DB

Spring Data - 

spring data provides dynamic repositories,it dynamically implements 
JPA,MongoDB etc

Spring Layer/Controllers have almost no logic


                    JSON/XML
                      ⬍
                     Controller
                      ⬇
                      Services
                     ⬇
                Spring Data Repositories
                     
                  DB




Main Projects for Spring data
Spring data jpa
spring data mongodb
spring data Neo4j
spring data solar
spring data REDIS
..............



Demo-
Demonstration Adding Spring Data JPA

add dependecy in pom.xml
- spring-boot-starter-data-jpa

- org.hsqldb/hsqldb
- Annotate domain objects with JPA
- Extend CrudRepository


1. Add dependency in my POM


             <dependency>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>

2. Annotate your pojo with JPA annotation

Team.java
public class Team {
  String name;
  String location;
  @Id @GeneratedValue
  Long id;
  @OneToMany(cascade = CascadeType.ALL)
  @JoinColumn(name="teamId")
  Set<Player> players;

  public Team() {
  super();
  }
.................

}

Player.java


@Entity

public class Player {
@Id @GeneratedValue
Long id;
public Player() {
super();
}

        ........
}


3- Create Repository

package com.dao;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

import com.domain.Team;

public interface TeamDao extends CrudRepository<Team, Long>{
List<Team> findAll();
Team findByName(String name);
}

4- Change Controller class


package com.demo;


import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.dao.TeamDao;
import com.domain.Team;

@RestController
public class InitialController {
    @Autowired
private TeamDao teamDao;
@RequestMapping("/teams/{name}")
public @ResponseBody Team helloWorld(@PathVariable String name) {
return teamDao.findByName(name);
}
 
}


5- DemoApplication

package com.demo;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

import com.dao.TeamDao;
import com.domain.Player;
import com.domain.Team;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer{

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);

}

@PostConstruct
public void init() {
Set<Player> players = new HashSet<>();
players.add(new Player("player1","A"));
players.add(new Player("player2","B"));
players.add(new Player("player3","C"));

Team team= new Team("xyz","India","p",players);
teamDao.save(team);
}

@Autowired
TeamDao teamDao;

}



Spring Data - REST


  • plugins to dynamic repositories
  • Generate restful interface  
  •          -get,put,post,delete
  • code needed only to override defaults
                           JSON/XML
                                                      ⬍
                          spring Data Rest
                                                       ⬍
                          Spring data Repository
                                                       ⬍
                                   DB 


add below dependecy in pom.xml

          <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>

</dependency>

go to my teamDAO and add RestResources

package com.dao;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RestResource;

import com.domain.Team;
//it is typed repository i can tell what data type it is going to serve

@RestResource(path = "teams",rel = "teams")
public interface TeamDao extends CrudRepository<Team, Long>{

List<Team> findAll();

Team findByName(String name);

}

3.Controller

package com.demo;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.dao.TeamDao;
import com.domain.Team;

@RestController
public class InitialController {

    @Autowired
TeamDao teamDao;


@RequestMapping("/hi/{name}")
public @ResponseBody Team helloWorld(@PathVariable String name) {

return teamDao.findByName(name);
}
 

}

4. Application

package com.demo;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

import com.dao.TeamDao;
import com.domain.Player;
import com.domain.Team;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer{

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
@PostConstruct
public void init() {
Set<Player> players = new HashSet<>();
players.add(new Player("player1","A"));
players.add(new Player("player2","B"));
players.add(new Player("player3","C"));
Team team= new Team("xyz","India",players);
teamDao.save(team);
}
@Autowired
TeamDao teamDao;
}

so here

Adding Spring Data Rest 

when application starts

  • @RestResource annotation interpreated
  • @Controllers bean created
  • @Request mapping created


Adding HATEOAS

spring data rest simply returns RESful resources

conversion handled by Jackson or JAXB

➤ underline Data relationship used to build Links
- if matching repositories exists





No comments:

Post a Comment

Note: only a member of this blog may post a comment.