>,java,hibernate,dictionary,jpa"/>

首页 > 解决方案 > JPA 如何持久化地图>

问题描述

我想使用注释将电子邮件保存在 Oracle 数据库中。我的Email班级有一个 Map 属性recipients,它按 TO、CC 和 BCC 分组,并有一组电子邮件地址作为值:

@Entity
@Table(name="EMAIL")
public class Email {
    @Id
    @SequenceGenerator(name="emailSeq", sequenceName="EMAIL_SEQ", allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="emailSeq")
    @Column(name="id")
    private Long id = null;

    @ElementCollection(targetClass=Set.class)
    @CollectionTable(name = "emailrecipients", joinColumns=@JoinColumn(name="email_id"))
    @MapKeyEnumerated(EnumType.STRING)
    @Column(name="address")
    private Map<RecipientType, Set<String>> recipients = new HashMap<RecipientType, Set<String>>();
    ...
}

public enum RecipientType {
    TO,
    CC,
    BCC
}

我的数据库如下所示:

CREATE TABLE email
( id number(10) NOT NULL,
  ...
  CONSTRAINT email_pk PRIMARY KEY (id)
);

CREATE TABLE emailrecipients
( email_id number(10) NOT NULL,
  --Addresstypes: "TO", "CC", "BCC"
  addresstype varchar2(50) NOT NULL,
  address varchar2(100) NOT NULL,
  CONSTRAINT fk_emailrecipients_email FOREIGN KEY (email_id) REFERENCES email(id)
);

我无法正确进行映射,并且出现以下错误:

org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: emailrecipients, for columns: [org.hibernate.mapping.Column(address)]

有任何想法吗?

标签: javahibernatedictionaryjpa

解决方案


推荐阅读