开发者

How can I "move" an object from one table to another using OpenJPA?

开发者 https://www.devze.com 2023-03-06 12:24 出处:网络
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 t

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 开发者_StackOverflow中文版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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号