2011-04-03 192 views
1

應該在User類的creditBalances字段上註釋什麼?在Hibernate中映射註釋?

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; 
} 

User類

@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對象的映射關鍵字和貨幣字段。

這可能在休眠?

回答

0

這是我使用的一個變體。通常使用@ElementCollection@ManyToMany註釋,其他註釋是情境性的。

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

ElementCollection在3.5中添加。我如何在3.4中做到這一點?作爲一種可嵌入的方式,Credit在這方面沒有ID或自己的表格。 – Gabriel 2011-04-04 07:58:41

+0

'@ CollectionOfElements'。查看嵌入的[documentation](http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html#collections-ofvalues)。 – 2011-04-04 12:08:22