1
好吧,所以我在目錄和文件之間有多對多的關係。但是,映射表還包含一個時間戳記(dateFound)。所以,我創建了四個模型類:休眠多對多映射+連接表更新不起作用
- 目錄
- 文件
- 文件目錄
- FileDirectoryID
Directory類:
...
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.directory")
private Set<FileDirectory> fileDirectories;
...
文件類:
...
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.file", cascade=CascadeType.ALL)
private Set<FileDirectory> fileDirectories;
...
文件目錄:
...
@AssociationOverrides({
@AssociationOverride(name = "pk.file", joinColumns = @JoinColumn(name = "fileID")),
@AssociationOverride(name = "pk.directory", joinColumns = @JoinColumn(name = "directoryID")) })
...
@EmbeddedId
private FileDirectoryID pk = new FileDirectoryID();
...
public void setPK(FileDirectoryID pk) {
this.pk = pk;
}
public FileDirectoryID getPK() {
return pk;
}
public void setFile(File file) {
getPK().setFile(file);
}
public void setDirectory(Directory directory) {
getPK().setDirectory(directory);
}
...
FileDirectoryID:
...
@ManyToOne
private File file;
@ManyToOne
private Directory directory;
...
在服務類中,我有以下幾點:
public static void updateDirectory(int directoryID, String directoryName,
String directoryBaseURL, String directorySearchURL, Set<FileDirectory> fileDirectories) {
Directory directory = (Directory) directoryDAO.get("id", ""
+ directoryID);
directory.setDirectoryName(directoryName);
directory.setDirectoryBaseURL(directoryBaseURL);
directory.setDirectorySearchURL(directorySearchURL);
directory.getFileDirectories().addAll(fileDirectories);
directoryDAO.updateObject(directory);
}
我可以在這一點保證,所有這些字段具有期望值(包括目錄的文件目錄映射)
上述所謂的updateObject具有以下定義:
public void updateObject(DatabaseObject object) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
session.update(object);
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
,其他所有字段的正確更新,但許多一對多的關係不更新。
對不起,這個冗長的帖子,並提前謝謝。 塞繆爾。
謝謝,失蹤的級聯設置是問題。 –