简体   繁体   中英

JPA - use class as an entity and embeddable at once?

I'm JPA newbie and I have probably stupid question ;)

Let Node be a class representing some blog post.

@Entity
@Table(name="nodes")
public class Node {
    @Id
    @GeneratedValue
    private long id;
    private String title;
    private String body;
}

I want to make an archive of deleted nodes in separate table (I don't want to mess nodes table with removed nodes using deleted flag). Archive entry should have additional fields: DateTime deletedAt , String deletedBy and maybe something else.

Is there any possibility to make a class ArchivedNode in the following way:

@Table(name="archived_nodes")
@Entity
public class ArchivedNode {
    private DateTime deletedAt;
    private String deletedBy;
    // ...
    @Embedded
    private Node node;
}

? Or maybe there is a better solution for archiving entities in separate tables?

Create a JPA repository for ArchivedNode . Each time you wish to call the delete() method on a Node , convert the Node you wish to delete into an ArchivedNode and save the ArchivedNode into the archived_nodes table.

Also, the Node within ArchivedNode should be a @OneToOne unidirectional relationship.

...
@OneToOne
@JoinColumn(name="id")
private Node node;
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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