Spring data jpa manage fest details | CSE JFSD Skill 9

Published on November 02, 2021
Last updated November 02, 2021

Innovation Anywhere is a technical fest conducted by Ed Informatics. Help the organization in building a Spring Boot Application where it has the records of the organizer details who conducts the fest, the details of the fest and the details of the participants who attended the fest.

In this exercise we need to manage details of the participants and organizers of a fest using a Spring Boot Application.

we will maintain two tables in the database.One table for organizers details and the other table for participants details.

Let us start with the participants table. first create a pojo class of the participant.

Participant will have id, name, email, test 1 score and test 2 score.

Let us create a participant pojo class with the above mentioned fields and create getters and setters for all the fields.

Id will be auto generated.

skill9ParticipantDetails.java

package in.anreddy.techfest;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Skill9ParticipantDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String name;
    private String email;
    private int test1score;
    private int test2score;

    public String getName() {
        return name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public int getTest2score() {
        return test2score;
    }

    public void setTest2score(int test2score) {
        this.test2score = test2score;
    }

    public int getTest1score() {
        return test1score;
    }

    public void setTest1score(int test1score) {
        this.test1score = test1score;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Now we need to do the same but for organizer details.

Organizer will not have test scores so let’s remove them and create a new pojo class for organizer.

skill9FestOrganizers.java

package in.anreddy.techfest;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Skill9FestOrganizers {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String name;
    private String email;

    public String getName() {
        return name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Now create two repositories for organizer and participant.

OrganizerRepository.java

package in.anreddy.techfest;

import org.springframework.data.repository.CrudRepository;

public interface OrganizerRepository extends CrudRepository<Skill9FestOrganizers, Integer> {
}

ParticipantRepository.java

package in.anreddy.techfest;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ParticipantRepository extends CrudRepository<Skill9ParticipantDetails, Integer> {
}

These two repositories will be used to manage the participants and organizers.Repositories significantly decrease the amount of boilerplate code needed to manage the data.

Now we need to create a controller class to process the requests.

we will have four methods in the controller class two with GetMapping to get the details of the participants and organizers and the other two with PostMapping to add the participants and organizers to the database.

the post methos will take name and email as parameters.

MainController.java

package in.anreddy.techfest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MainController {
    @Autowired
    private ParticipantRepository participantRepository;

    @Autowired
    private OrganizerRepository organizerRepository;

    @PostMapping(path = "/add-participant") // Map ONLY POST Requests
    public @ResponseBody String addNewParticipant(@RequestParam String name, @RequestParam String email) {
        Skill9ParticipantDetails n = new Skill9ParticipantDetails();
        n.setName(name);
        n.setEmail(email);
        participantRepository.save(n);
        return "Participant added successfully";
    }

    @GetMapping(path = "/all-participants")
    public @ResponseBody Iterable<Skill9ParticipantDetails> getAllParticipants() {
        // This returns a JSON or XML with the users
        return participantRepository.findAll();
    }

    @GetMapping(path = "/all-organizers")
    public @ResponseBody Iterable<Skill9FestOrganizers> getAllOrganizers() {
        // This returns a JSON or XML with the users
        return organizerRepository.findAll();
    }

    @PostMapping(path = "/add-organizer") // Map ONLY POST Requests
    public @ResponseBody String addNewOrganizer(@RequestParam String name, @RequestParam String email) {
        Skill9FestOrganizers n = new Skill9FestOrganizers();
        n.setName(name);
        n.setEmail(email);
        organizerRepository.save(n);
        return "Organizer added successfully";
    }
}

Now we have a spring boot application that can add and view organizers and participants details and also configure your application.properties

Goto application.properties and add the following lines:

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.username=system
spring.datasource.password=ayyo
spring.jpa.hibernate.ddl-auto=update

Don’t forget to change your password.

The above lines are for oracle database.If you are using any other database change the driver class name and database url accordingly.



Tags :