Spring Data JPA Content Assist

This commit is contained in:
danthe1st
2023-02-20 15:20:15 +01:00
committed by aboyko
parent 4584be1fe8
commit 97d66062c0
13 changed files with 812 additions and 50 deletions

View File

@@ -2,7 +2,6 @@ package org.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -19,18 +18,19 @@ public class Application {
@Bean
public CommandLineRunner demo(CustomerRepository repository) {
return (args) -> {
return args -> {
Employee employee = new Employee(1234, "Margot", "Al-Harazi");
// save a couple of customers
repository.save(new Customer("Jack", "Bauer"));
repository.save(new Customer("Chloe", "O'Brian"));
repository.save(new Customer("Kim", "Bauer"));
repository.save(new Customer("David", "Palmer"));
repository.save(new Customer("Michelle", "Dessler"));
repository.save(new Customer("Jack", "Bauer", employee));
repository.save(new Customer("Chloe", "O'Brian", employee));
repository.save(new Customer("Kim", "Bauer", employee));
repository.save(new Customer("David", "Palmer", employee));
repository.save(new Customer("Michelle", "Dessler", employee));
// fetch all customers
log.info("Customers found with findAll():");
log.info("-------------------------------");
for (Customer customer : repository.findAll()) {
for(Customer customer : repository.findAll()){
log.info(customer.toString());
}
log.info("");
@@ -45,7 +45,7 @@ public class Application {
// fetch customers by last name
log.info("Customer found with findByLastName('Bauer'):");
log.info("--------------------------------------------");
for (Customer bauer : repository.findByLastName("Bauer")) {
for(Customer bauer : repository.findByLastName("Bauer")){
log.info(bauer.toString());
}
log.info("");

View File

@@ -5,29 +5,34 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
private boolean thisCustomerIsSpecial;//contains keyword in name
protected Customer() {}
@ManyToOne
private Employee responsibleEmployee;
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
protected Customer() {}
@Override
public String toString() {
return String.format(
"Customer[id=%d, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
public Customer(String firstName, String lastName, Employee responsibleEmployee) {
this.firstName = firstName;
this.lastName = lastName;
this.responsibleEmployee = responsibleEmployee;
}
@Override
public String toString() {
return "Customer [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", responsibleEmployee="
+ responsibleEmployee + "]";
}
// end::sample[]
@@ -42,5 +47,12 @@ public class Customer {
public String getLastName() {
return lastName;
}
}
public boolean isThisCustomerIsSpecial() {
return thisCustomerIsSpecial;
}
public Employee getResponsibleEmployee() {
return responsibleEmployee;
}
}

View File

@@ -0,0 +1,47 @@
// tag::sample[]
package org.test;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
private Long socialSecurityNumber;
private String firstName;
private String lastName;
protected Employee() {
}
public Employee(long socialSecurityNumber, String firstName, String lastName) {
this.socialSecurityNumber = socialSecurityNumber;
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Employee[socialSecurityNumber=%d, firstName='%s', lastName='%s']",
socialSecurityNumber, firstName, lastName
);
}
// end::sample[]
public Long getSocialSecurityNumber() {
return socialSecurityNumber;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}

View File

@@ -1,7 +1,5 @@
package org.test;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface TestCustomerRepositoryForCompletions extends CrudRepository<Customer, Long> {