我试图让api返回一个注释列表,通过与标签的多对多关系关联,给定一个标签ID。Spring boot自动创建了一个名为notes_tables的桥接表,其中包含一个notes_id字段和一个labels_id字段。Spring Boot还创建了一个notes表和一个labels表。我尝试了以下步骤:
@Query(value="select * from notes join notes_labels on note.id=notes_id join labels on labels_id=labels.id where labels_id=:lid", nativeQuery=true)
public List<Note> findNotesForLabel(@Param("lid") int labelId);
我只需要让它工作,但我特别好奇我是否能让它与jpa方法查询一起工作。任何查询都可以,只要它可以工作。
package com.example.maapi.models;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import java.util.List;
import java.util.Objects;
@Entity
@Table(name = "notes")
public class Note {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String note;
private String title;
private String status = "private";
@ManyToOne
@JsonIgnore
private User user;
@ManyToOne
@JsonIgnore
private Folder folder;
@ManyToMany
@JsonIgnore
private List<Label> labels;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Folder getFolder() {
return folder;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void setFolder(Folder folder) {
this.folder = folder;
}
public List<Label> getLabels() {
return labels;
}
public void setLabels(List<Label> labels) {
this.labels = labels;
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Note)) {
return false;
}
Note note = (Note) o;
return id == note.id && Objects.equals(note, note.note) &&
Objects.equals(title, note.title) && Objects.equals(status,
note.status) && Objects.equals(user, note.user) &&
Objects.equals(folder, note.folder) && Objects.equals(labels,
note.labels);
}
@Override
public int hashCode() {
return Objects.hash(id, note, title, status, user, folder,
labels);
}
}
package com.example.maapi.models;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import java.util.List;
import java.util.Objects;
@Entity
@Table(name = "labels")
public class Label {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String title;
private String status = "private";
@ManyToOne
@JsonIgnore
private User user;
@ManyToOne
@JsonIgnore
private Folder folder;
@ManyToMany(mappedBy = "labels")
@JsonIgnore
private List<Note> notes;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Folder getFolder() {
return folder;
}
public void setFolder(Folder folder) {
this.folder = folder;
}
public List<Note> getNotes() {
return notes;
}
public void setNotes(List<Note> notes) {
this.notes = notes;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Label)) {
return false;
}
Label label = (Label) o;
return id == label.id && Objects.equals(title, label.title) &&
Objects.equals(status, label.status) && Objects.equals(user,
label.user) && Objects.equals(folder, label.folder) &&
Objects.equals(notes, label.notes);
}
@Override
public int hashCode() {
return Objects.hash(id, title, status, user, folder, notes);
}
}
package com.example.maapi.services;
import com.example.maapi.models.Folder;
import com.example.maapi.models.Note;
import com.example.maapi.models.User;
import com.example.maapi.repositories.FolderRepo;
import com.example.maapi.repositories.NoteRepo;
import com.example.maapi.repositories.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class NoteService {
@Autowired
NoteRepo noteRepo;
@Autowired
UserRepo userRepo;
@Autowired
FolderRepo folderRepo;
public List<Note> findAllNotes(){
return noteRepo.findAllNotes();
}
public Note findNoteById(int noteId){
return noteRepo.findNoteById(noteId);
}
public List<Note> findNotesByUser(int userId){
return noteRepo.findNotesByUser(userId);
}
public Note createNoteForUser(int userId, Note note){
User user = userRepo.findUserById(userId);
note.setUser(user);
return noteRepo.save(note);
}
public List<Note> findNotesByFolder(int folderId){
return noteRepo.findNotesByFolder(folderId);
}
public Note createNoteForFolder(int folderId, Note note){
Folder folder = folderRepo.findFolderById(folderId);
note.setFolder(folder);
note.setUser(folder.getUser());
return noteRepo.save(note);
}
public int updateNote(int noteId, Note updatedNote){
Note note = noteRepo.findNoteById(noteId);
updatedNote.setUser(note.getUser());
updatedNote.setFolder(note.getFolder());
noteRepo.save(updatedNote);
if(updatedNote.equals(note)){
return 1;
} else {
return 0;
}
}
public int deleteNote(int noteId){
noteRepo.deleteById(noteId);
if(noteRepo.findNoteById(noteId) == null) {
return 1;
} else {
return 0;
}
}
// SEARCH IMPLEMENTATION
public List<Note> searchForNote(String note){
return noteRepo.searchForNote(note);
}
}
labelservice.java
所以这是我能想出的一种春战利品方法。CrudRepository有findById(整数id),它返回一个可选对象。您所要做的就是optional.get()返回封装的对象,然后可以用getter返回所需的字段(在我的例子中是列表注释)。
// CrudRepo interface provides the findById method which returns an Optional<Label>
// object that may or may not exist. Optional.get() returns the encapsulated object.
public List<Note> findNotesByLabelId(int labelId) {
Optional<Label> label = labelRepo.findById(labelId);
return label.get().getNotes();
}
我正在读MongoDB在行动这本书。我有一个关于的问题。 在中,该书给出了一个多对多关系的示例。它给出了文档和文档。 我理解这里的多对多关联。基本上,可以有一个带有_id等数组的键。所以我不想问类似MongoDB多对多关联的问题 我的问题是关于的,书中给出了关于查询多对多关系的两个示例查询。下面是两个查询: > 是什么意思?我以为只存在于Ruby驱动程序中。 中的是什么?是收藏吗? 中的是什么?
问题内容: 首先导入k和模块: 声明和对象: 有三个表:和。该Artist对象可以链接到多个。并且该Album对象可以链接到多个对象。的是保持之间的关系和紧密的: 所以我们有Artist链接到Album这是与Genre它看起来像这样:。 完成此设置后,我们Genre首先创建对象: 然后是两张专辑: 和艺术家: 创建数据库后,我们可以查询Albums链接到的内容Genre: 以及Artists链接到
主要内容:示例实际应用中,由于多对多的关系比较复杂,会增加理解和关联的复杂度,所以应用较少。MyBatis 没有实现多对多级联,推荐通过两个一对多级联替换多对多级联,以降低关系的复杂度,简化程序。 例如,一个订单可以有多种商品,一种商品可以对应多个订单,订单与商品就是多对多的级联关系。可以使用一个中间表(订单记录表)将多对多级联转换成两个一对多的关系。 示例 下面以订单和商品(实现“查询所有订单以及每个订单对应
嗨,我有一个角色表和一个权限表,它有多对多的关系。我已经创建了下面链接中提到的实体 https://www.baeldung.com/jpa-多对多 角色实体 权限实体 我已经为每个实体创建了JPA存储库,并且正在尝试将权限保存给一个角色。我在表中已经有了一组/列表的权限,我正在尝试将它们映射到某个角色。我正在尝试使用spring JPA存储库执行下面的代码。 在执行我得到的代码时 org.hib
实现了一对多的关系,它运行良好。 我的问题是当我运行下面的查询时,如果表有100个员工行,每个员工有两个部门。数据库查询被调用了101次,因为对每个员工都是调用部门查询,要完成调用全部100行需要很长时间,有没有人可以提出替代的解决方案? 输出XML:
现在我需要正在学习‘程序设计’课程的学生名单 如何使用条件查询实现这一点。