不会持久化支持实体到哈希映射的转换 [英] Does blaze persistence support entity to hashmap conversion

查看:129
本文介绍了不会持久化支持实体到哈希映射的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出色的持久性支持实体到键值对的转换,EntityView字段的名称也可以与实际Entity的名称不同

Does blaze persistence support conversion of entity to key value pairs, also can EntityView fields have a different name than that of actual Entity

@Entity
@Table(name = "users")
public class User {

  @Column
  private String name;

  @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
  private Set<UserPropertyValue> userPropertyValues = new HashSet<>();

}

@Entity
@Table(name = "user_property_values")
public class UserPropertyValue {

  @Column
  private String value;

  @ManyToOne
  JoinColumn(name = "properties_id")
  private Property property;
}

@Entity
@Table(name = "properties")
public class Property {
  @Id
  private Long id;
  
  @Column
  private String name;
}

我想要EntityView这样的东西.

I Would want the EntityView something like this.

@EntityView(User.class)
interface UserView {
  private String getUserName();
  private HashMap<String, String>  userPropertyValues();
}

基本上名称是此处的用户名,并且 userPropertyValues 应该是包含以下内容的HashMap:

Basically name is the username here and userPropertyValues should be HashMap containing:

  1. 键作为属性-> 名称
  2. 值作为 PropertyValues ->
  1. key as the Property -> name
  2. value as PropertyValues -> value

它是否还有助于创建自定义转换器,例如我想将LocalDateTime更改为具有各种不同格式/样式的String?

Also does it help create custom converters, say I want to change LocalDateTime to String with various different format/pattern ?

推荐答案

当然,您可以使用@Mapping批注指定JPQL.Next映射,如下所示:

Sure, you can use the @Mapping annotation to specify a JPQL.Next mapping which could look like the following:

@EntityView(User.class)
interface UserView {
  @Mapping("name")
  String getUserName();
  Set<UserPropertyValueView> getUserPropertyValues();
}
@EntityView(UserPropertyValue.class)
interface UserPropertyValueView {
  @Mapping("property.name")
  String getPropertyName();
  String getValue();
}

如果您真的想要一张地图,也可以使用以下内容:

If you really want a map, you could also use the following:

@EntityView(User.class)
public abstract class UserView {

  private final Map<String, String> values;

  public UserView(@Mapping("userPropertyValues") Set<UserPropertyValueView> values) {
    this.values = values.stream().collect(Collectors.toMap(UserPropertyValueView::getPropertyName, UserPropertyValueView::getValue))
  }

  @Mapping("name")
  public abstract String getUserName();
  public Map<String, String> getUserPropertyValues() {
    return values;
  }
}
@EntityView(UserPropertyValue.class)
interface UserPropertyValueView {
  @Mapping("property.name")
  String getPropertyName();
  String getValue();
}

通过注释映射地图键将在目前正在开发的最终1.5.0版本中引入: https://github.com/Blazebit/blaze-persistence/issues/401

Mapping map keys through an annotation will be introduced in the final 1.5.0 release which is currently being worked on: https://github.com/Blazebit/blaze-persistence/issues/401

这篇关于不会持久化支持实体到哈希映射的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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