4. Code Example

Example 4. Excerpt from PersonRepositoryTest Find by PK

                
Person person = personRepository.findOne(FIRST_ID);
                
            

Example 5. Excerpt from PersonRepositoryTest Find All

                
Collection<Person> persons = personRepository.findAll();
                
            

Example 6. Excerpt from PersonRepositoryTest Find by First Name Like

                
List<Person> persons = personRepository.findByFirstNameLike("J%");
                
            

Example 7. Excerpt from PersonRepositoryTest Find by Last Name

                
List<Person> persons = personRepository.findByLastName(LAST_NAME);
                
            

Example 8. Excerpt from PersonRepositoryTest Find by Address

                
List<Person> persons = personRepository.findByAddress(ADDR);
                
            

Example 9. Excerpt from PersonRepositoryTest Paginated Find by Address

                
Page<Person> pageResult = personRepository.findByAddress(ADDR, new PageRequest(page, size));
List<Person> persons = pageResult.getContent();
                
            

Example 10. Excerpt from PersonRepositoryTest Find by First & Last Name

                
List<Person> persons = personRepository.findByName(FIRST_NAME, LAST_NAME);
                
            

Example 11. Excerpt from PersonRepositoryTest Save

[Note]Note

In Spring Data JPA, save and update are both handled by save or saveAndFlush.

                
Professional person = new Professional();
Set<Address> addresses = new HashSet<Address>();
Address address = new Address();
addresses.add(address);

address.setAddress(addr);
address.setCity(CITY);
address.setState(STATE);
address.setZipPostal(ZIP_POSTAL);
address.setCountry(COUNTRY);

person.setFirstName(firstName);
person.setLastName(lastName);
person.setCompanyName(companyName);
person.setCreated(new Date());
person.setAddresses(addresses);

Person result = personRepository.saveAndFlush(person);
                
            

Example 12. Excerpt from PersonRepositoryTest Update

                
Person person = personRepository.findOne(FIRST_ID);
testPersonOne(person);

String lastName = "Jones"; 
person.setLastName(lastName);

personRepository.saveAndFlush(person);
                
            

Example 13. Excerpt from PersonRepositoryTest Delete

                
personRepository.delete(FIRST_ID);