2

我有一个双向地图映射。读取有效,但 Hibernate 不存储映射键列。当我签入调试器时,一切都设置好了。

INSERT 查询是:

insert into rel_custField (field_id, release_id, value, id) values (22, 1, 'fwefwe', 3)

它还应该包含设置name列。我应该如何做到这一点?

也许问题是地图项实体没有name属性?但我从未在 docs/spec 中看到它应该这样做。
如果我选择该解决方案,我将不得不手动设置 name 属性,这似乎不切实际且多余。

@SuppressWarnings("serial")
@Entity @Table(name="`release`")
public class Release implements Serializable, IHasTraits {

    @MapKeyColumn(name = "name")
    @OneToMany(mappedBy = "release", cascade = CascadeType.ALL )
    //@JoinColumn(referencedColumnName = "release_id") // Only at one side.
    private Map<String, ReleaseCustomField> customFields = new HashMap();

    ...
}

@Entity
@Table(name = "rel_custField", uniqueConstraints = {
    @UniqueConstraint(name = "rel_prodcf", columnNames = {"release_id", "field_id"})
})
public class ReleaseCustomField implements Serializable {

    @Id @GeneratedValue( strategy = GenerationType.AUTO )
    private Long id;

    // Make uni-dir?
    @ManyToOne(optional = false)
    @JoinColumn(name = "release_id", nullable = false, updatable = false)
    private Release release;

    ....
}
4

1 回答 1

0

根据我在这里找到的描述:http ://en.wikibooks.org/wiki/Java_Persistence/Relationships#Map_Key_Columns_.28JPA_2.0.29

它不应该是目标实体的属性,而是列名。例如:

@MapKeyColumn(name = "id")
@OneToMany(mappedBy = "release", cascade = CascadeType.ALL )
private Map<String, ReleaseCustomField> customFields = new HashMap();
于 2013-01-30T10:13:55.357 回答