开发者

Map Annotation in Hibernate?

开发者 https://www.devze.com 2023-02-21 14:12 出处:网络
What annotation should go on the creditBalances field of the User class? credit_balance table CREATE TABLE `credit_balance` (

What annotation should go on the creditBalances field of the User class?

credit_balance table

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

Credit class

@Embeddable
public class Credit {
  @Column(name="currency", columnDefinition="CHAR(3)", nullable=false)
  Curr开发者_运维知识库ency currency;
  @Column(name="amount", columnDefinition="DECIMAL(12,4)", nullable=false)
  BigDecimal amount;
}

User class

@Entity
public class User {
  @Id
  String id;

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

We are using Hibernate 3.4.

I've looked through http://docs.jboss.org/hibernate/annotations/3.4/reference/en/html/entity.html#entity-mapping-association-collections but get lost quickly.

Clarification: The currency column should be used for both the map key and the currency field of the Credit object.

Is this possible in Hibernate?


This is one variant I use. In general the @ElementCollection or @ManyToMany annotations are used, the others are situational.

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

精彩评论

暂无评论...
验证码 换一张
取 消