Section 1

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/11

encourage image

There's no tags or description

Looks like no tags are added yet.

12 Terms

1
New cards

Implementation Practice 1:

Pagination & Sorting | Scenario: Generate the Java code to create a Pageable request for the 4th page of students, limiting to 20 per page, sorted primarily by lastName (descending) and secondarily by firstName (ascending).

Sort multiSort = Sort.by("lastName").descending().and(Sort.by("firstName").ascending()); // Pages are 0-indexed, so 3 represents the 4th page Pageable page = PageRequest.of(3, 20, multiSort);


2
New cards

Implementation Practice 2:

Native SQL Deletion | Scenario: Write the exact Spring Data JPA method signature and @Query annotation to delete a Student by their id using native SQL.

@Transactional @Modifying @Query(value = "DELETE FROM student WHERE id = ?1", nativeQuery = true) int deleteStudentById(Integer id);


3
New cards

Implementation Practice 3:

Custom JPQL Search | Scenario: Write a custom JPQL query method in StudentRepository to find all students whose email ends with a specific domain passed as a parameter.

@Query("SELECT s FROM Student s WHERE s.email LIKE %:domain") List findByEmailDomain(@Param("domain") String domain);


4
New cards

Implementation Practice 4:

Sequence Generation Mapping | Scenario: The Student entity requires sequence generation for its primary key. Write the exact field and annotations to implement this database mapping.

@Id @SequenceGenerator(name = "student_sequence", sequenceName = "student_sequence", allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "student_sequence") private Integer id;


5
New cards

Implementation Practice 5:

Command Line Runner | Scenario: Write a standard CommandLineRunner bean that prints the total count of students in the database to the console on startup.

@Bean CommandLineRunner countRunner(StudentRepository studentRepository) { return args -> { System.out.println("Total students: " + studentRepository.count()); }; }


6
New cards

Implementation Practice 6:

Pagination Iteration | Scenario: Write a code block to iterate over a sorted Pageable result set from a repository method (studentRepository.findAll(sort2)) and print each student's first and last name to the console.

studentRepository.findAll(sort2).forEach(student ->{ System.out.println(student.getFirstName() + " "+ student.getLastName()); });


7
New cards

Implementation Practice 7:

REST Controller Setup | Scenario: Generate the class-level annotations and constructor required to set up a REST controller mapped to the "Software-eng" endpoint. You must also inject the softwareEnginnerService via the constructor.

@RestController()
 @RequestMapping("Software-eng")
 public class Controller {
 private softwareEnginnerService softwareEnginnerService;
 public Controller(softwareEnginnerService softwareEnginnerService) { 
this.softwareEnginnerService = softwareEnginnerService;
 } 
}


8
New cards

Implementation Practice 8:

POST Request Mapping | Scenario: Write a controller method mapped with @PostMapping to add a new SoftwareEngineer. The method must extract the entity from the HTTP request body using the @RequestBody annotation before passing it to the service layer.

@PostMapping public SoftwareEngineer AddSoftwareEng(@RequestBody SoftwareEngineer softwareEngineer){
 return softwareEnginnerService.addSoftwareEng(softwareEngineer); }


9
New cards

Implementation Practice 9:

Exception Handling in Service Updates | Scenario: Write a service method to update an existing engineer by their ID, updating both their name and tech stack before saving. If the engineer is not found in the repository, throw a NoSuchElementException with the exact message "Engineer with ID[id] is not foud".

public SoftwareEngineer updateEngById(Integer id, SoftwareEngineer softwareEngineer) { SoftwareEngineer existingInfo = softwareEngineerRespository.findById(id).orElseThrow(()-> new NoSuchElementException("Engineer with ID" + id +" is not foud")); existingInfo.setName(softwareEngineer.getName()); existingInfo.setTechStack(softwareEngineer.getTechStack()); return softwareEngineerRespository.save(existingInfo); }


10
New cards

Implementation Practice 10:

Path Variables and Request Params | Scenario: Write a @DeleteMapping controller method that captures an id from the URL path variable and a sort string from the request parameters. Pass both variables down to the service layer's deleteEngById method.

@DeleteMapping("/{id}") public void deleteEngById(@PathVariable Integer id, @RequestParam String sort){ softwareEnginnerService.deleteEngById(id,sort); }


11
New cards

Implementation Practice 11:

Implementing PrePersist Mapping |

Scenario: Write a method within an entity class that automatically sets a createdAt timestamp field (ZonedDateTime) to the current system time right before the entity is saved to the database for the first time.

@PrePersist public void prePersisy(){ createdAt= ZonedDateTime.now(); }


12
New cards

Implementation Practice 12:

Configuring a One-to-One Foreign Key Relationship | Scenario: Configure a field in an entity class to establish a one-to-one relationship with a Student entity. Ensure the foreign key is mapped to a column named "student_id" referencing the target "id" column, and explicitly name the constraint student_id_card_student_id_fk.

@OneToOne @JoinColumn(name = "student_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "student_id_card_student_id_fk")) private Student student;