oracle Hibernate 中使用注解的复合主键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2884043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 20:40:28  来源:igfitidea点击:

Compound Primary Key in Hibernate using Annotations

javasqloraclehibernateora-01400

提问by Rich

I have a table which uses two columns to represent its primary key, a transaction id and then the sequence number.

我有一个表,它使用两列来表示其主键、一个事务 ID,然后是序列号。

I tried what was recommended http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-mappingin section 2.2.3.2.2, but when I used the Hibernate session to commit this Entity object, it leaves out the TXN_ID field in the insert statement and only includes the BA_SEQ field!

我尝试了第 2.2.3.2.2 节中推荐的http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-mapping,但是当我使用 Hibernate 会话提交此实体时对象,它忽略了插入语句中的 TXN_ID 字段,只包含 BA_SEQ 字段!

What's going wrong? Here's the related code excerpt:

怎么了?这是相关的代码摘录:

@Id 
@Column(name="TXN_ID")
private long txn_id; public long getTxnId(){return txn_id;} public void setTxnId(long t){this.txn_id=t;}

@Id
@Column(name="BA_SEQ")
private int seq; public int getSeq(){return seq;} public void setSeq(int s){this.seq=s;}





And here are some log statements to show what exactly happens to fail:

这里有一些日志语句来显示究竟发生了什么失败:

In createKeepTxnId of DAO base class: about to commit Transaction :: txn_id->90625 
seq->0 ...<Snip>...

Hibernate: insert into TBL (BA_ACCT_TXN_ID, BA_AUTH_SRVC_TXN_ID, BILL_SRVC_ID, 
BA_BILL_SRVC_TXN_ID, BA_CAUSE_TXN_ID, BA_CHANNEL, CUSTOMER_ID, BA_MERCHANT_FREETEXT, 
MERCHANT_ID, MERCHANT_PARENT_ID, MERCHANT_ROOT_ID, BA_MERCHANT_TXN_ID, BA_PRICE, 
BA_PRICE_CRNCY, BA_PROP_REQS, BA_PROP_VALS, BA_REFERENCE, RESERVED_1, RESERVED_2,
RESERVED_3, SRVC_PROD_ID, BA_STATUS, BA_TAX_NAME, BA_TAX_RATE, BA_TIMESTAMP, BA_SEQ) 
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
[WARN] util.JDBCExceptionReporter SQL Error: 1400, SQLState: 23000
[ERROR] util.JDBCExceptionReporter ORA-01400: cannot insert NULL into 
("SCHEMA"."TBL"."TXN_ID")

The important thing to note is I print out the entity object which has a txn_id set, and then the following insert into statement does not include TXN_ID in the listing and thus the NOT NULL table constraint rejects the query.

需要注意的重要一点是我打印出设置了 txn_id 的实体对象,然后下面的 insert into 语句在列表中不包含 TXN_ID,因此 NOT NULL 表约束拒绝查询。

回答by Rich

how to make a composite primary key (java persistence annotation)

如何制作复合主键(java持久化注解)

This helped.

这有帮助。

@IdClass(TxnPK.class)

and then defining a Serializable implementing class TxnPK with fields just like I wanted in my Entity class, as well as equals and hashCode methods.

然后定义一个 Serializable 实现类 TxnPK,其中的字段就像我在 Entity 类中想要的那样,以及 equals 和 hashCode 方法。

annotation on "secondary" fields of the primary key. So @Id on the BA_SEQ field. Also implemented hashCode and equals to supplement.

主键的“次要”字段注释。所以@Id 在 BA_SEQ 字段上。还实现了 hashCode 和 equals 补充。

回答by Bozho

Use @EmbeddedIdand @Embeddable. Roughly:

使用@EmbeddedId@Embeddable。大致:

@EmbeddedId
private CompositeKey key;


@Embeddable
public class CompositeKey {
    @Column
    private int something;

    @Column
    private int somethingElse;
}