0
我試圖堅持文件對象的新條目,該條目在用戶和部門之間具有多對多關係(許多用戶/部門可以在此場景中訪問文件) user_fileobject和dept_fileobject的連接表中的信息未反映出來。堅持自己,用戶和部門的實例化列表在不同的類中,但是這些列表是否也需要被持久化?在多對多關係連接表中沒有新條目JPA
這裏有實例的用戶和部門的名單代碼:
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();
}
下面是用戶的POJO, Department和FileObject類:
@實體
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",
[email protected](name="user_id"),
[email protected](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",
[email protected](name="dept_id"),
[email protected](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>();
}
代碼本身有問題嗎?
做我需要堅持的用戶和部門呢?也只是應用於fileobject類的級聯? –
如果你使用級聯,那麼你不需要事先保存它們。它們將與FileObject一起被保存。 –