简体   繁体   English

如何通过 Hibernate 直接更新外键值?

[英]How do you update a foreign key value directly via Hibernate?

I have a couple of objects that are mapped to tables in a database using Hibernate, BatchTransaction and Transaction.我有几个对象使用 Hibernate、BatchTransaction 和 Transaction 映射到数据库中的表。 BatchTransaction's table (batch_transactions) has a foreign key reference to transactions, named transaction_id. BatchTransaction 的表 (batch_transactions) 有一个对事务的外键引用,名为 transaction_id。

In the past I have used a batch runner that used internal calls to run the batch transactions and complete the reference from BatchTransaction to Transaction once the transaction is complete.过去,我使用了一个批处理运行器,它使用内部调用来运行批处理事务,并在事务完成后完成从 BatchTransaction 到 Transaction 的引用。 After a Transaction has been inserted, I just call batchTransaction.setTransaction(txn), so I have a @ManyToOne mapping from BatchTransaction to Transaction.插入事务后,我只需调用batchTransaction.setTransaction(txn),因此我有一个从BatchTransaction 到事务的@ManyToOne 映射。

I am changing the batch runner so that it executes its transactions through a Web service.我正在更改批处理运行程序,以便它通过 Web 服务执行其事务。 The ID of the newly inserted Transaction will be returned by the service and I'll want to update transaction_id in BatchTransaction directly (rather than using the setter for the Transaction field on BatchTransaction, which would require me to load the newly inserted item unnecessarily).新插入的事务的 ID 将由服务返回,我想直接更新 BatchTransaction 中的 transaction_id(而不是使用 BatchTransaction 上的 Transaction 字段的设置器,这将需要我不必要地加载新插入的项目)。

It seems like the most logical way to do it is to use SQL rather than Hibernate, but I was wondering if there's a more elegant approach.似乎最合乎逻辑的方法是使用 SQL 而不是 Hibernate,但我想知道是否有更优雅的方法。 Any ideas?有任何想法吗?


Here's the basic mapping.这是基本映射。

BatchQuery.java批量查询.java

@Entity
@Table(name = "batch_queries")
public class BatchQuery
{
    @ManyToOne
    @JoinColumn(name = "query_id")
    public Query getQuery()
    {
        return mQuery;
    }
}

Query.java查询.java

@Entity
@Table(name = "queries")
public class Query
{
}

The idea is to update the query_id column in batch_queries without setting the "query" property on a BatchQuery object.这个想法是更新 batch_queries 中的 query_id 列,而不在 BatchQuery object 上设置“查询”属性。

Using a direct SQL update, or an HQL update, is certainly feasible.使用直接 SQL 更新或 HQL 更新当然是可行的。

Not seeing the full problem, it looks to me like you might be making a modification to your domain that's worth documenting in your domain.没有看到完整的问题,在我看来,您可能正在对您的域进行修改,值得在您的域中记录。 You may be moving to having a BatchTransaction that has as a member just the TransactionId and not the full transaction.您可能会转向拥有一个 BatchTransaction,它的成员只是 TransactionId 而不是完整的事务。

If in other activities, the BatchTransaction will still be needing to hydrate that Transaction, I'd consider adding a separate mapping for the TransactionId, and having that be the managing mapping (make the Transaction association update and insert false).如果在其他活动中,BatchTransaction 仍然需要为该事务补充水分,我会考虑为 TransactionId 添加一个单独的映射,并将其作为管理映射(使事务关联更新并插入 false)。

If BatchTransaction will no longer be concerned with the full Transaction, just remove that association after adding a the TransactionId field.如果 BatchTransaction 将不再关注完整的 Transaction,只需在添加 TransactionId 字段后删除该关联即可。

As you have writeen, we can use SQL to achieve solution for above problem.正如您所写,我们可以使用 SQL 来解决上述问题。 But i will suggest not to update the primary keys via SQL.但我建议不要通过 SQL 更新主键。

Now, as you are changing the key, which means you are creating alltogether a new object, for this, you can first delete the existing object, with the previous key, and then try to insert a new object with the updated key(in your case transaction_id)现在,当您更改密钥时,这意味着您正在创建一个新的 object,为此,您可以先使用以前的密钥删除现有的 object,然后尝试插入一个新的 ZA8CFDE6331BD59EB2AC966F8911C4B666案例 transaction_id)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM