在Hibernate中映射注释?

时间:2011-04-03 15:46:08

标签: java 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中吗?

1 个答案:

答案 0 :(得分:0)

这是我使用的一种变体。通常使用@ElementCollection@ManyToMany注释,其他注释是情境性的。

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