簡體   English   中英

Hibernate中的地圖注釋?

[英]Map Annotation in Hibernate?

在User類的creditBalances字段上應該添加什么注釋?

credit_balance表

CREATE TABLE `credit_balance` (
  `user_id` varchar(255) NOT NULL,
  `currency` char(3) DEFAULT NULL,
  `amount` decimal(12,4) DEFAULT NULL
)

信用等級

@Embeddable
public class Credit {
  @Column(name="currency", columnDefinition="CHAR(3)", nullable=false)
  Currency currency;
  @Column(name="amount", columnDefinition="DECIMAL(12,4)", nullable=false)
  BigDecimal amount;
}

用戶類別

@Entity
public class User {
  @Id
  String id;

  //What annotation goes here?
  Map<Currency, Credit> creditBalances;
}

我們正在使用Hibernate 3.4。

我瀏覽了http://docs.jboss.org/hibernate/annotations/3.4/reference/en/html/entity.html#entity-mapping-association-collections,但很快就迷路了。

說明: currency列應同時用於地圖鍵和Credit對象的貨幣字段。

在Hibernate中有可能嗎?

這是我使用的一種變體。 通常,使用@ElementCollection@ManyToMany批注,其他則視情況而定。

@ElementCollection
@Column(name = "value", nullable=false)
@MapKeyColumn(name="name")
@JoinTable(name = "from_to", joinColumns = @JoinColumn(name = "to_id"))
Map<String, String>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM