Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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/
@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();
}
@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();
}
@ManyToMany
private List<Project> projects;
@ManyToMany(mappedBy = "projects")
private List<Tag> tags;
@ManyToOne
private Project project;
@OneToMany(mappedBy = "project")
private List<Task> tasks;
@OneToOne
Project project;
@OneToOne(mappedBy = "project")
private ProjectDetails projectDetails;
@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 + '\'' +
"}";
}
}
@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 + '\'' +
'}';
}
}
public interface ProjectDetailsRepository extends CrudRepository<ProjectDetails, Integer> { }
public interface ProjectRepository extends CrudRepository<Project, Integer> { }
@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();
}
@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();
}
@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";
}
@Entity
public class Tag {
@GeneratedValue
@Id
private int id;
private String name;
// Gettery i settery
}
public interface TagRepository extends CrudRepository<Tag, Integer> { }
@Autowired
public TagRepository tagRepository;
@RequestMapping("/tagging")
@ResponseBody
public String tagsAndProjects() {
Tag tag1 = new Tag();
tag1.setName("Tag 1");
Tag tag2 = new Tag();
tag2.setName("Tag 2");
Project project1 = new Project();
project1.setName("Project 1");
Project project2 = new Project();
project2.setName("Project 2");
projectRepository.save(project1);
projectRepository.save(project2);
project1.setTags(new ArrayList<>());
project1.getTags().add(tag1);
project2.setTags(new ArrayList<>());
project2.getTags().add(tag1);
project2.getTags().add(tag2);
tag1.setProjects(new ArrayList<>());
tag1.getProjects().add(project1);
tag1.getProjects().add(project2);
tag2.setProjects(new ArrayList<>());
tag2.getProjects().add(project2);
tagRepository.save(tag1);
tagRepository.save(tag2);
return "OK";
}
@RequestMapping("/by-project")
@ResponseBody
public String tagsByProject() {
StringBuilder out = new StringBuilder();
projectRepository.findAll().forEach(project -> { // 1
out.append("project: ").append(project.getName()).append("<br>");
project.getTags().forEach(tag -> { // 2
out.append("- ").append(tag.getName()).append("<br>");
});
});
return out.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.