合并现有对象不会更新数据库中已更改的@Embedded

时间:2016-02-09 14:28:14

标签: java sql-server osgi eclipselink amdatu

我正在使用Eclipselink的Amdatu实现来兼容OSGI。

有人能说出为什么这不起作用吗?

下面的类定义为实体,应该保留。每次更新 Ptz2PlPalletUnloadOrder 时, OrderRepoImpl 中的 save 方法会将持久订单与更新版本合并,其中 Ptz2PlPalletUnloadDefinition def OrderState orderState 已更改。

无论如何,我能够使用em.merge方法更新数据库中的orderState,但我无法更新BD中的@Embedded def对象。更新而不在数据库中创建新行是非常优选的。

合并对象时不会抛出异常,log4j会包含在问题的底部。

Ptz2PlPalletUnloadOrder

@Entity(name = Ptz2PlPalletUnloadOrder.TABLE_NAME)
@Table(name = Ptz2PlPalletUnloadOrder.TABLE_NAME)
public class Ptz2PlPalletUnloadOrder implements Order {
    public static final String TYPE = "ptz2pl.pallet.unload";
    public static final String TABLE_NAME = "order_ptz2pl_pallet_unload";

    // Order ID
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long dbId;

    @Temporal(TemporalType.TIMESTAMP)
    private Date created = new Date();

    // States
    @Enumerated(EnumType.STRING)
    private OrderState orderState = OrderState.NEW;

    @Embedded
    private Ptz2PlPalletUnloadDefinition def;
}

Ptz2PlPalletUnloadDefinition

持久的课程。

@Embeddable
public class Ptz2PlPalletUnloadDefinition {

    @Embedded
    private Station station;
    private volatile Integer agvId;
    private volatile String agvSystemName;
    @Temporal(TemporalType.TIMESTAMP)
    private Date deadLine;
    private String userId;
    private Long palletType;

    private Ptz2PlPalletUnloadDefinition() {

    }
}

OrderRepoImpl

@Transactional
@Component(provide = ManagedTransactional.class)
public class OrderRepoImpl implements OrderRepo, ManagedTransactional {

    private EntityManager em;

    @Override
    public Ptz2PlPalletUnloadOrder save(Ptz2PlPalletUnloadOrder order) {
        Ptz2PlPalletUnloadOrder result = em.merge(order);
        return result;
    }
}

通过此方法制动时,结果值具有正确的值。因此可以预期数据库已经更新,但是当Ptz2PlPalletUnloadOrder的待更新字段是嵌入对象时,不会发生这种不幸。

日志

[EL Finer]: 2016-02-09 15:25:32.393--ServerSession(1465675217)--Thread(Thread[pool-7-thread-4,5,Configuration Admin Service])--client acquired: 2038620953
[EL Finer]: 2016-02-09 15:25:32.393--UnitOfWork(769186)--Thread(Thread[pool-7-thread-4,5,Configuration Admin Service])--TX binding to tx mgr, status=STATUS_ACTIVE
[EL Finer]: 2016-02-09 15:25:32.394--ClientSession(2038620953)--Thread(Thread[pool-7-thread-4,5,Configuration Admin Service])--acquire unit of work: 769186
[EL Finest]: 2016-02-09 15:25:32.395--UnitOfWork(769186)--Thread(Thread[pool-7-thread-4,5,Configuration Admin Service])--Merge clone with references Ptz2PlPalletUnloadOrder [id=1, orderState=QUEUED, location=Location [stationId=10, systemName=BEING], allocatedAgvId=null, allocatedAgvSystemName=null]
[EL Finer]: 2016-02-09 15:25:37.014--UnitOfWork(769186)--Thread(Thread[pool-7-thread-4,5,Configuration Admin Service])--TX beforeCompletion callback, status=STATUS_ACTIVE
[EL Finer]: 2016-02-09 15:25:37.015--UnitOfWork(769186)--Thread(Thread[pool-7-thread-4,5,Configuration Admin Service])--begin unit of work commit
[EL Finer]: 2016-02-09 15:25:37.016--UnitOfWork(769186)--Thread(Thread[pool-7-thread-4,5,Configuration Admin Service])--TX afterCompletion callback, status=COMMITTED
[EL Finer]: 2016-02-09 15:25:37.017--UnitOfWork(769186)--Thread(Thread[pool-7-thread-4,5,Configuration Admin Service])--end unit of work commit
[EL Finer]: 2016-02-09 15:25:37.018--UnitOfWork(769186)--Thread(Thread[pool-7-thread-4,5,Configuration Admin Service])--release unit of work
[EL Finer]: 2016-02-09 15:25:37.019--ClientSession(2038620953)--Thread(Thread[pool-7-thread-4,5,Configuration Admin Service])--client released

1 个答案:

答案 0 :(得分:2)

我已经在amdatu-jpa测试用例中复制了这个。

在EclipseLink论坛上发现了一个类似的问题https://www.eclipse.org/forums/index.php/t/474144/

禁用编织可解决我创建的测试中的问题。要禁用,请将下面的属性添加到persistence.xml

<property name="eclipselink.weaving" value="false"/>