【问题标题】:Ebean database model with a serialized object column具有序列化对象列的 Ebean 数据库模型
【发布时间】:2021-02-22 15:23:52
【问题描述】:

我正在使用带有 ebean 的 playframework。 我有以下型号:

public class MailData {
  private String title;
  private String body;
}

public class Envelope extends Model {
  @Id
  private UUID id;

  private MailData mailData;
  private Date sent;
}

我想要一个数据库表,即信封。 该表应该有一个 MailData 列,其中将存储 MailData 对象的序列化字符串。我该怎么做?

我最接近的是 @Embeddable 和 @Embedded 标签,这导致字段标题和正文位于信封表内,这不是预期的结果。

【问题讨论】:

    标签: java playframework ebean


    【解决方案1】:

    我找到了一种使用转换器的方法:

    @Converter
    public class MailDataConverter implements AttributeConverter<MailData, String> {
      @Override
      public String convertToDatabaseColumn(MailData attribute) {
        //serialize here
      }
    
      @Override
      public MailData convertToEntityAttribute(String dbData) {
        //deserialize here
      }
    }
    
    public class MailData {
      private String title;
      private String body;
    }
    
    public class Envelope extends Model {
      @Id
      private UUID id;
    
      @Column
      @Convert(converter = MailDataConverter.class)    
      private MailData mailData;
    
      private Date sent;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-20
      • 1970-01-01
      • 2011-02-26
      • 2020-01-26
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      相关资源
      最近更新 更多