0
我是JPA新手,我有可能愚蠢的問題;)JPA - 使用類作爲一個實體並且可以一次嵌入?
讓Node
是代表一些博客帖子的類。
@Entity
@Table(name="nodes")
public class Node {
@Id
@GeneratedValue
private long id;
private String title;
private String body;
}
我想在不同的表中刪除節點的歸檔(我不想惹nodes
表使用deleted
標誌移除的節點)。存檔條目應該有其他字段:DateTime deletedAt
,String deletedBy
也許還有別的。
是否有可能作出下列方式一類ArchivedNode
:
@Table(name="archived_nodes")
@Entity
public class ArchivedNode {
private DateTime deletedAt;
private String deletedBy;
// ...
@Embedded
private Node node;
}
?或者,也許有更好的解決方案來存檔實體在不同的表格中?
你不能直接做,因爲如果某件事是一個實體,那麼它有一個「ID」,而一個嵌入式對象沒有。例如,您可以擁有一個節點,其中包含可提供節點所有字段的Embeddable,然後將該Embeddable對象也存儲在ArchivedNode中。 –
Eclipselink支持軟刪除和歷史記錄表https://wiki.eclipse.org/EclipseLink/Examples/JPA/SoftDelete https://wiki.eclipse.org/EclipseLink/Examples/JPA/History其他提供商應該有類似的東西 – Chris