0

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;
}

I Would want the EntityView something like this.

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

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

  1. key as the Property -> name
  2. value as PropertyValues -> value

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

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58
solecoder
  • 191
  • 3
  • 12
  • FYI, we just released 1.5.0 which implements support for mapping maps. See the documentation for more information about this: https://persistence.blazebit.com/documentation/1.5/entity-view/manual/en_US/#custom-indexed-collection-mapping – Christian Beikov Sep 03 '20 at 15:53

1 Answers1

0

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();
}

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

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58