简体   繁体   English

使用注释在Hibernate中使用复合主键

[英]Compound Primary Key in Hibernate using Annotations

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-mapping in 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表约束拒绝查询。

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. 因此,BA_SEQ字段上的@Id。 Also implemented hashCode and equals to supplement. 还实现了hashCode和等于补充。

Use @EmbeddedId and @Embeddable . 使用@EmbeddedId@Embeddable Roughly: 大致:

@EmbeddedId
private CompositeKey key;


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

    @Column
    private int somethingElse;
}

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

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