【问题标题】:Map Annotation in Hibernate?Hibernate中的地图注释?
【发布时间】:2011-04-03 15:46:07
【问题描述】:

应该在 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 中可行吗?

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

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

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

    【讨论】:

    • ElementCollection 是在 3.5 中添加的。我如何在 3.4 中做到这一点?同样作为可嵌入对象,Credit 没有 id 或它自己的表。
    • @CollectionOfElements。查看 documentation 进行嵌入。
    猜你喜欢
    • 1970-01-01
    • 2011-12-07
    • 2011-06-03
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 2011-02-26
    • 2017-05-22
    • 1970-01-01
    相关资源
    最近更新 更多