简体   繁体   English

如何使用OpenJPA将对象从一个表“移动”到另一个表?

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

I have two tables, that share the same definition. 我有两个表,它们具有相同的定义。 I commonly insert new objects into one of these tables, let's call it Table1. 我通常将新对象插入这些表之一,我们称之为Table1。 From time to time, I want to move one entry from Table1 to the other table (Table2). 有时,我想将一个条目从Table1移到另一个表(Table2)。

How can I achieve this using the OpenJPA Framework? 如何使用OpenJPA框架实现这一目标? 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). (我只想在代码中保留此逻辑)。

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. 然后,您可以通过删除原始实体从原始表中删除原始记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用openjpa将实体从一个数据库移动到另一个数据库? - How to move entity from one database to another using openjpa? 如何将引用从一个对象移动到另一个对象? - How can I move a reference from one object to another? OpenJPA将另一个表中的数据插入一个表中 - OpenJPA inserting into one table data from another table OpenJPA OptimisticLockException:从一个PersistenceUnit分离对象并将其保存到另一个对象 - OpenJPA OptimisticLockException: Detaching object from one PersistenceUnit and saving it to another 如何将对象形状从一个位置移动到另一位置? - How would I move an object shape from one location to another? 如何使用Android Canvas将图像从一个点移动到另一个点 - How can I move an image from one point to another using Android Canvas 如何将 Bitmap 对象从一个活动传递到另一个活动 - How can I pass a Bitmap object from one activity to another 我如何将请求对象从一个类传递到另一个类 - how can i pass request object from one class to another 如何将物体从一个点以固定角度均匀地移动到另一点? - How to move an object uniformly from one point to another at a fixed angle? 如何通过坐标使对象从一个点移动到另一个点? - How to make an object move from one point to another via coordinates?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM