gpt4 book ai didi

java - 在 JPA 中使用对象作为主键和复合键

转载 作者:行者123 更新时间:2023-12-01 12:57:09 26 4
gpt4 key购买 nike

我想知道在 JPA 中使用对象作为组合键是否是一个做法。例如,我有几个复合键,它们是来自另一个表的外键并与它们映射。

复合类

@Embeddable
public class CashInstrumentComposite implements Serializable {


private static final long serialVersionUID = -6065538857637001219L;

@Column(name = "instrument_id", nullable = false)
private String instrumentId;

@OneToOne
@JoinColumn(name = "company_id")
private Company companyId;

@Column(name = "instrument_type", nullable = true)
@Enumerated(EnumType.STRING)
private InstrumentType instrumentType;

@Column(name = "batch_no", nullable = false)
private String batchNumber;
}

实体类

@Entity
@Table(name = "bank_corporate_cash_instrument")
public class CashInstrument extends Model {

/* serial version id. */
private static final long serialVersionUID = 8360452197690274885L;

/* specify the composite key */
@EmbeddedId
private CashInstrumentComposite compositeId;

我可以毫无问题地坚持使用这个,但是我读了这个,

http://docs.oracle.com/javaee/6/tutorial/doc/bnbqa.html#bnbqf

它说主键应该是其中之一

  • Java 基本类型
  • Java 原始包装类型
  • java.lang.String
  • java.util.Date(时间类型应为 DATE)
  • java.sql.Date
  • java.math.BigDecimal
  • java.math.BigInteger

那么我在这里做错了什么?请解释一下。谢谢

最佳答案

我认为您没有正确解释该文档的内容:

The primary key, or the property or field of a composite primary key, must be one of the following Java language types:

关于java - 在 JPA 中使用对象作为主键和复合键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23783248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com