简体   繁体   English

休眠多对多单向映射

[英]Hibernate Many To Many unidirectional mapping

I have two tables USERS and FILES. 我有两个表USERS和FILES。 I want to be able to control the users that can download a file, and for this I was thinking of creating and intermediary table FILE_PERMISSIONS with a user_id and a file_id. 我希望能够控制可以下载文件的用户,为此,我正在考虑创建一个带有user_id和file_id的中间表FILE_PERMISSIONS。

Looking at database level I understand how can I solve the problem, but going up at Hibernate level, I can't really understand how should I map this relation. 从数据库级别看,我知道如何解决该问题,但是在Hibernate级别,我真的不明白如何映射此关系。 The way I see it is something like this: 我的看法是这样的:

public class User {
   private Integer userId;
}

public class File {
   private Integer fileId;
   private List<Integer> userIds;
}

So I want my File object to know the id property of all the users that can download the file, but not vice versa, so that a user doesn't know about those files. 因此,我希望我的File对象知道所有可以下载文件的用户的id属性,但反之则不然,这样用户就不知道这些文件。

From what I read I can use a many to many unidirectional relation but I'm not sure that I can only have the id of the user, and not the user object itself. 从我阅读的内容中,我可以使用多对多的单向关系,但是我不确定只能拥有用户的ID,而不能拥有用户对象本身。

You can manage it having the following structure. 您可以使用以下结构进行管理。

User: 用户:

@Entity
public class User {
    @Id
    private Integer userId;

    // getters, setters
}

File: 文件:

@Entity
public class File {
    @Id
    private Integer fileId;

    @ManyToMany
    @JoinTable(
            name = "file_permissions",
            joinColumns = @JoinColumn(name = "file_id"),
            inverseJoinColumns = @JoinColumn(name = "user_id")
    )
    private Set<User> users;

    // getters, setters
}

You can benefit from making an easier design using @OneToMany relationship instead. 您可以使用@OneToMany关系来简化设计,从而从中受益。 This way you could create a service in order to manage File permissions, instead of relying on user service or file services to do so. 这样,您可以创建服务来管理文件权限,而不必依赖用户服务或文件服务来创建服务。

I propose something like: 我建议类似:

User.java User.java

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Version
    private Integer version;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
    private List<FilePermissions> filePermissionsList= new ArrayList<>();


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public List<FilePermissions> getFilePermissionsList() {
        return filePermissionsList;
    }

    public void setFilePermissionsList(List<FilePermissions> filePermissionsList) {
        this.filePermissionsList = filePermissionsList;
    }
}

Notice User has a list of FilePermissions. 注意用户具有FilePermissions列表。

Your FilePermission class should be like: 您的FilePermission类应类似于:

@Entity
public class FilePermissions {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Version
    private Integer version;

    @ManyToOne
    private User user;

    @OneToOne
    private File file;

    private Permission permission;


    public FilePermissions() {

    }

    public FilePermissions(User user, File file, Permission permission) {
        this.user = user;
        this.file = file;
        this.permission = permission;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public Permission getPermission() {
        return permission;
    }

    public void setPermission(Permission permission) {
        this.permission = permission;
    }
}

Notice the @ManytoOne relationship back to the user, as well as the @OneToOne relationship to the File class. 注意@ManytoOne关系回到用户,以及@OneToOne关系回到File类。 Here you can store the detail on what permission user have, in this case i have a enumeration. 在这里,您可以存储有关权限用户拥有的详细信息,在这种情况下,我有一个枚举。

Your File class is straight forward: 您的File类很简单:

@Entity
public class File {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Version
    private Integer version;

    private String name;

    public File() {}

    public File(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

You can see solution in detail here: https://github.com/ccoloradoc/HibernateFilePermissionSample 您可以在此处详细查看解决方案: https : //github.com/ccoloradoc/HibernateFilePermissionSample

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM