重命名ElementCollection中的嵌入式数据 [英] Rename Embedded data in ElementCollection

查看:93
本文介绍了重命名ElementCollection中的嵌入式数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意,该问题已由已修复 href ="https://stackoverflow.com/users/1082300/steve-ebersole" title ="Steve Ebersole"> Steve Ebersole ,它包含在Hibernate 5.2.3中.如果您坚持使用较早的版本,我将添加一种解决方法作为答案.

Note this was promptly fixed by Steve Ebersole, it's included in Hibernate 5.2.3. If you're stuck on earlier versions I'll add a workaround as answer.

我有以下工作设置:

一个Embeddable(为了便于阅读,删除了样板)

An Embeddable (boilerplate removed for readability)

@Embeddable
public class TypeValue {
    String type;

    @Column(columnDefinition = "TEXT")
    String value;
}

和一个Entity(为了便于阅读,删除了样板)

And an Entity (boilerplate removed for readability)

@Entity
public class AggregatedTypeValue {
    @Id
    UUID id;

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name = "type", column = @Column(name = "content_type")),
        @AttributeOverride(name = "value", column = @Column(name = "content_value"))
    })
    TypeValue content;

    @ElementCollection(fetch = FetchType.EAGER)
    @MapKeyColumn(name = "attribute_name")
    Map<String, TypeValue> attributes;
}

如您所见,我使用MapKeyColumn重命名了存储地图键的列.这很好.但是,我也想重命名用于Embaddable的列.

As you can see I used MapKeyColumn to rename the column where the key of the map is stored in. This works fine. However I also want to rename the columns used for the Embaddable.

根据我的发现,应该可以使用AttributeOverride:

From what I found this should be possible using AttributeOverride:

@ElementCollection(fetch = FetchType.EAGER)
@AttributeOverrides({
    @AttributeOverride(name = "value.type", column = @Column(name = "attribute_type")),
    @AttributeOverride(name = "value.value", column = @Column(name = "attribute_value"))
})
@MapKeyColumn(name = "attribute_name")
Map<String, TypeValue> attributes;

但是,这会导致

  1. 收集表中的类型和值已正确重命名
  2. 存储键的列名为值"(MapKeyColumn被忽略)
  3. 存储了一个附加的哈希"列,该列成为主键的一部分,显然是对键上的hashCode的调用的结果.
  1. type and value in the collection table are renamed correctly
  2. the column storing the key is named "value" (MapKeyColumn is ignored)
  3. an additional "hash" column is stored, which becomes part of the primary key and is apparently the result of a call to hashCode on the key.

有没有一种方法可以相应地重命名列?

Is there a way to rename the columns accordingly?

修改 我正在使用Hibernate 5.2.2.Final

edit I am using Hibernate 5.2.2.Final

推荐答案

注意,这很快得到了 Steve Ebersole 修复,休眠5.2.3.如果您坚持使用较早的版本,这就是我作为解决方法所做的事情.

Note this was promptly fixed by Steve Ebersole, it's included in Hibernate 5.2.3. If you're stuck on earlier versions, here's what I did as a workaround.

我添加了一个新的Embeddable

@Embeddable
public class AttributeName {
    String name;
}

并将其用作键:

@ElementCollection(fetch = FetchType.LAZY)
@AttributeOverrides({
        @AttributeOverride(name = "key.name", column = @Column(name = "attribute_name", nullable = false)),
        @AttributeOverride(name = "value.type", column = @Column(name = "attribute_type", nullable = false)),
        @AttributeOverride(name = "value.value", column = @Column(name = "attribute_value", nullable = false))
})
Map<AttributeName, TypeValue> attributes;

这可以按预期工作,但是这也意味着您必须以不同的方式访问实际密钥(例如key.getName()),因此可能不是您想要的.

This works as expected, however it also means that you'll have to access the actual key differently (e.g. key.getName()) and thus might not be what you want.

这篇关于重命名ElementCollection中的嵌入式数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆