[英]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.