我该如何“移动"使用OpenJPA从一个表到另一个表的对象? [英] How can I "move" an object from one table to another using OpenJPA?

查看:76
本文介绍了我该如何“移动"使用OpenJPA从一个表到另一个表的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,它们共享相同的定义.我通常将新对象插入这些表之一,我们称之为Table1.我不时希望将一个条目从Table1移到另一个表(Table2).

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).

如何使用OpenJPA框架实现这一目标?该对象显然已绑定到一张桌子...

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).

推荐答案

好吧,我想不出一站式解决方案".

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.

这篇关于我该如何“移动"使用OpenJPA从一个表到另一个表的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆