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>
精彩评论