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

喜夏-厌秋 提交于 2019-12-11 07:34:53

问题


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.



来源:https://stackoverflow.com/questions/6040969/how-can-i-move-an-object-from-one-table-to-another-using-openjpa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!