How can I “move” an object from one table to anoth

2019-08-15 07:56发布

问题:

I have two tables, that share the same definition. I commonly insert new objects into one of these tables, let's call it Table1. From time to time, I want to move one entry from Table1 to the other table (Table2).

How can I achieve this using the OpenJPA Framework? The object is clearly bound to one table ...

@Entity
@Table(name="TRACKING")
public class TrackingEntry {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @Column(name="LASTNAME")
    private String lastName;

    @Column(name="FIRSTNAME")
    private String firstName;

    // rest omitted
}

Any Ideas besides using an "on delete" trigger on database level? (I'd like to hold this logic in code only).

回答1:

Well, I cannot think of a "one-step solution".

Your entities are inherently linked to a table. So, one solution could be to define a secondary entity class linked to your secondary table.

This secondary class could inherit from the first one to ensure compatibility, and you could provide a copy constructor that receives an instance of the parent and copies all attributes every time you want to move a record. Then you could delete the original record from the original table by deleting the original entity.