【问题标题】:@Column doesn't work with @ElementCollection@Column 不适用于 @ElementCollection
【发布时间】:2014-04-18 11:19:42
【问题描述】:

如果@ElementCollection 已被使用,我有一个包含两列的表格。 我想重命名这些列。我可以通过@CollectionTable(joinColumns=@JoinColumn(name="login_id"))col1 执行此操作,但是如何重命名col2? 这种情况不起作用:

@ElementCollection
@CollectionTable(joinColumns=@JoinColumn(name="login_id")
)
@Column(name = "profile_id")
public Set<Profile> getDefaultProfiles() {
    return defaultProfiles;
}

col2的名字留在defaultProfiles_id

我使用Hibernate 4.3.4.Final

【问题讨论】:

  • 什么是Profile?如果是实体,为什么要用@ElementCollection来指代呢?
  • 你的意思是我应该改用@OneToMany
  • 是的,@OneToMany@ManyToMany,取决于您的需要。
  • 好的。看起来 @Column 仅适用于基本数据类型的 @ElementCollection,例如 Set&lt;String&gt;
  • 它也适用于地图

标签: java hibernate jpa jpa-2.0


【解决方案1】:

使用地图的完整示例:

@ElementCollection
@MapKeyColumn(name="name")
@Column(name="value")
@CollectionTable(name="example_attributes", joinColumns=@JoinColumn(name="example_id"))
private Map<String, String> attributes = new HashMap<>();

还有一个完整的列表示例:

@ElementCollection(fetch = FetchType.EAGER)
@Column(name = "attribute")
@CollectionTable(name = "entity_attributes", joinColumns = @JoinColumn(name = "entity_id"))
private List<String> attributes = new ArrayList<>();

【讨论】:

    猜你喜欢
    • 2011-12-26
    • 2020-12-29
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多