ManyToMany

时间:2017-11-08 13:00:48

标签: sql jackson spring-data-jpa

我有以下情况。用户向我的服务器发出REST请求(http://localhost/server/folders)。服务器知道用户,现在应仅响应用户的相关信息,这意味着仅显示用户注册的文件夹,并仅显示用户负责的任务。

我已经可以使用jpa-method findByRegistrationsInternalId来获取用户注册的文件夹,但是我得到了所有任务,即使是那些我没有被分配到的任务。

那么如何过滤任务呢?

我有以下实体

文件夹-1 - n->用户

文件夹-n - m->任务-1--1-> ResponsibleUser

public class Folder
{
    @Id
    private String id;

    ...

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "Folder", orphanRemoval = true)
    @Setter(AccessLevel.NONE)
    private Set<FolderRegistration> registrations = new HashSet<FolderRegistration>();

    @OrderBy("id ASC")
    @ManyToMany(cascade = CascadeType.ALL, mappedBy = "Folders")
    @Setter(AccessLevel.NONE)
    private Set<Task> tasks = new HashSet<Task>();

    ....
}

public class FolderRegistration
{
    @Id
    private String internalId;

    @Id
    @Column(insertable = false, updatable = false)
    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private String FolderId;

    @JsonIgnore
    @ManyToOne
    @JoinColumns(
    { @JoinColumn(name = "FolderId", referencedColumnName = "id"),
            @JoinColumn(name = "tenant", referencedColumnName = "tenant", insertable = false, updatable = false) })
    private Folder Folder;
}

public class Task
{
    @Id
    private Long id;

    ....

    @JsonIgnore
    private String responsibleEmployeeId;

    ....

    @JsonIgnore
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "Folder_Task", //
            joinColumns =
            { @JoinColumn(name = "tenant", referencedColumnName = "tenant", insertable = false, updatable = false), @JoinColumn(name = "taskId", referencedColumnName = "id") }, //
            inverseJoinColumns =
            { @JoinColumn(name = "tenant", referencedColumnName = "tenant", insertable = false, updatable = false), @JoinColumn(name = "FolderId", referencedColumnName = "id") })
    private List<Folder> Folders = new ArrayList<Folder>();     

    ....
}

public interface LogisticsTaskFolderRepository extends JpaRepository<LogisticsTaskFolder, LogisticsTaskFolderId>
{      
    List<Folder> findByRegistrationsInternalId(String internalId);

}

@RestController
@RequestMapping(value = "/api/folder")
public class FolderEndpoint
{
    @Autowired
    FolderService folderService;

    @GetMapping
    List<Folder> getFolders() throws Exception
    {
        return folderService.getFolders();
    }
}

0 个答案:

没有答案