我怎样才能“移动”?使用 OpenJPA 从一个表到另一个表的对象?
我有两个表,它们共享相同的定义。我通常将新对象插入到其中一个表中,我们将其称为 Table1。有时,我想将一个条目从表 1 移动到另一个表(表 2)。
如何使用 OpenJPA 框架实现这一目标?该对象显然绑定到一个表......
@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
}
除了在数据库级别使用“删除时”触发器之外,还有什么想法吗? (我想仅在代码中保留此逻辑)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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.