Created
April 22, 2020 08:17
-
-
Save mloza/b0dd617eadbc699f4a86b505546b062a to your computer and use it in GitHub Desktop.
Kod źródłowy do wpisu o Spring Boot - Spring Data JPA część II: Powiązania między tabelami znajdującego się pod adresem https://blog.mloza.pl/spring-boot-spring-data-jpa-czesc-ii-powiazania-miedzy-tabelami/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RequestMapping("/add-task") | |
@ResponseBody | |
public String addTask() { | |
Project project = projectRepository.findOne(1); | |
Task task = new Task() | |
.withBudget(12.00) | |
.withDescription("New task") | |
.withName("New task") | |
.withProject(project); // 1 | |
project.getTasks().add(task); // 2 | |
taskRepository.save(task); | |
return projectsAndTasks(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RequestMapping("/delete-task") | |
@ResponseBody | |
public String deleteTask() { | |
Task task = taskRepository.findOne(1L); | |
Project project = task.getProject(); | |
project.getTasks().remove(task); | |
taskRepository.delete(task); | |
return projectsAndTasks(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ManyToMany | |
private List<Project> projects; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ManyToMany(mappedBy = "projects") | |
private List<Tag> tags; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ManyToOne | |
private Project project; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OneToMany(mappedBy = "project") | |
private List<Task> tasks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OneToOne | |
Project project; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OneToOne(mappedBy = "project") | |
private ProjectDetails projectDetails; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Project { | |
@GeneratedValue | |
@Id | |
private int id; | |
@Column | |
private String name; | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return "Project{" + | |
"id=" + id + | |
", name='" + name + '\'' + | |
"}"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class ProjectDetails { | |
@GeneratedValue | |
@Id | |
private int id; | |
@Column | |
private String description; | |
// pomijam gettery i settery dla czytelności | |
@Override | |
public String toString() { | |
return "ProjectDetails{" + | |
"id=" + id + | |
", description='" + description + '\'' + | |
'}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ProjectDetailsRepository extends CrudRepository<ProjectDetails, Integer> { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ProjectRepository extends CrudRepository<Project, Integer> { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Autowired | |
public ProjectRepository projectRepository; | |
@RequestMapping("/project-tasks") | |
@ResponseBody | |
public String projectsAndTasks() { | |
StringBuilder response = new StringBuilder(); | |
for(Project project: projectRepository.findAll()) { | |
response.append(project).append("<br>"); | |
for(Task task: project.getTasks()) { // 1 | |
response.append("- ").append(task).append("<br>"); | |
} | |
} | |
return response.toString(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RequestMapping("/by-tag") | |
@ResponseBody | |
public String projectsByTag() { | |
StringBuilder out = new StringBuilder(); | |
tagRepository.findAll().forEach(tag -> { | |
out.append("tag: ").append(tag.getName()).append("<br>"); | |
tag.getProjects().forEach(project -> { | |
out.append("- ").append(project.getName()).append("<br>"); | |
}); | |
}); | |
return out.toString(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RequestMapping("/remove-tag") | |
@ResponseBody | |
public String removeTag() { | |
Optional<Project> project = | |
StreamSupport | |
.stream(projectRepository | |
.findAll() | |
.spliterator(), false) | |
.filter(p -> p.getName().equals("Project 2")) | |
.findAny(); | |
project.ifPresent(p -> { | |
Optional<Tag> tag = p.getTags().stream() | |
.filter(t -> t.getName().equals("Tag 2")) | |
.findAny(); | |
tag.ifPresent(t -> { | |
t.getProjects().remove(p); | |
p.getTags().remove(t); | |
tagRepository.save(t); | |
projectRepository.save(p); | |
}); | |
}); | |
return "OK"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Tag { | |
@GeneratedValue | |
@Id | |
private int id; | |
private String name; | |
// Gettery i settery | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface TagRepository extends CrudRepository<Tag, Integer> { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment