2011-04-03 188 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