在多对多关系JPA

时间:2017-02-23 10:22:32

标签: java mysql jpa eclipselink

我正在尝试保留用户和部门之间有多对多关系的文件对象的新条目(许多用户/部门可以在此方案中访问文件)但是在持久化信息之后,在user_fileobject和dept_fileobject的连接表中是没反映出来。持久化本身和用户和部门的实例化列表在不同的类上,但这些列表是否也需要持久化?

以下是实例化用户和部门列表的代码:

for(int i=0; i<listSharee.length; i++){
                                String username = listSharee[i].replace(" ", ".").toLowerCase();
                                Query q1 =  CpabeDAO.em.createQuery("SELECT u FROM User u WHERE u.username=:username");
                                q1.setParameter("username", username);
                                List<User> userReq = q1.getResultList();
                                if(userReq.isEmpty()){
                                    Query q3 =  CpabeDAO.em.createQuery("SELECT d FROM Department d WHERE d.departmentName=:name");
                                    q3.setParameter("name", listSharee[i]);
                                    List<Department> departmentReq = q3.getResultList();
                                    for(Department dept : departmentReq){
                                        listSharee[i] = listSharee[i].replace(" ", "");
                                        departmentSharee.add(dept);

                                    }
                                }else{
                                    for(User user: userReq){
                                        userSharee.add(user);
                                        listSharee[i] = listSharee[i].replace(" ", "_");

                                    }
                                }
                            }
...
    FileUpload.uploadFile(file, totalPolicy, userLoggedIn, userSharee, departmentSharee);

以下是用于保存文件对象信息的代码:

public static void storeFileInformation(byte[] encryptedRawFileKey, String fileChecksum, String fileKeyChecksum, byte[] ivFileKey, String keyName, String realName, String policy,  User owner, List<User> userSharee, List<Department> departmentSharee ){
//      byte[] encryptedRawFileKey, String fileChecksum, String fileKeyChecksum, byte[] ivFileKey, String keyName, List<User> listUserAccessor, List<Policy> policies, String realName, User userCreator
        CpabeDAO.em.getTransaction().begin();

        FileObject fileObject = new FileObject();
        fileObject.setEncryptedRawFileKey(encryptedRawFileKey);
        fileObject.setFileChecksum(fileChecksum);
        fileObject.setFileKeyChecksum(fileKeyChecksum);
        fileObject.setIvFileKey(ivFileKey);
        fileObject.setKeyName(keyName);
        fileObject.setPolicy(policy);
        fileObject.setRealName(realName);
        fileObject.setListDeptAccessor(departmentSharee);
        fileObject.setListUserAccessor(userSharee);
        fileObject.setUserCreator(owner);

        CpabeDAO.em.persist(fileObject);
        CpabeDAO.em.getTransaction().commit();
        CpabeDAO.em.close();
    }

以下是User,Department和FileObject类的POJO:

@Entity

public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    public int id;
    public String username;
    public String firstname;
    public String lastname;
//  public String attributes;
    public String hashPassword;

    @ManyToOne
    private Department department;

    @OneToMany(mappedBy = "user")
    public List<Device> devices = new ArrayList<Device>();

    @OneToMany(mappedBy = "userCreator")
    public List <FileObject> fileOwned = new ArrayList <FileObject>();

    @ManyToMany
    @JoinTable(name="USER_FILE_ACCESS",
    joinColumns=@JoinColumn(name="user_id"),
       inverseJoinColumns=@JoinColumn(name="fileHasAccess_ID "))
    public List<FileObject> fileHasAccess = new ArrayList<FileObject>();
}

@Entity
public class Department {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    int departmentId;
    String departmentName;

    @OneToMany(mappedBy = "department")
    private List<User> employees = new ArrayList<User>();


    @ManyToMany
    @JoinTable(name="DEPT_FILE_ACCESS",
    joinColumns=@JoinColumn(name="dept_id"),
       inverseJoinColumns=@JoinColumn(name="fileHasAccess_ID "))
    public List<FileObject> deptFileHasAccess = new ArrayList<FileObject>();
    }

@Entity
public class FileObject {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    public int id;

    String fileChecksum;

    String fileKeyChecksum;

    String realName;

    String keyName;

//  @OneToMany(mappedBy = "fileObject")
//  public List<Policy> policies = new ArrayList<Policy>();

    String policy;

    byte[] encryptedRawFileKey;

    byte[] ivFileKey;

    @ManyToOne
    public User userCreator;

    @ManyToMany(mappedBy="fileHasAccess")
    public List<User> listUserAccessor = new ArrayList<User>();

    @ManyToMany(mappedBy="deptFileHasAccess")
    public List<Department> listDeptAccessor = new ArrayList<Department>();
}

代码本身有问题吗?

0 个答案:

没有答案