将休眠关联映射为Java.util.Map

时间:2018-08-26 11:07:45

标签: hibernate

我正在尝试将休眠关联映射为Java.util.Map。 频道表中有一列merchant_account_id 商家帐户表不会引用回渠道表。它仅具有merchant_account_id,param_name和param_value。 Mercer_account_id可以使用不同的param_name值多次出现。

我在做什么错?该地图为空,除非merchant_account_id在商家帐户表中仅出现一次。

@Entity
@Table(schema = "P", name = "CHANNEL")
public class ChannelEntity implements ProcessingChannel, PersistentEntity<Long> {

  private enum ParamName {
    SE_NUMBER,
    SUBMITTER_ID
  }

  @Id
  @Column(name = "CHANNEL_ID")
  private Long id;

  @OneToMany(targetEntity = MerchantAccountEntity.class, fetch = FetchType.EAGER)
  @JoinColumn(name = "MERCHANT_ACCOUNT_ID")
  @MapKey(name = "id.paramName")
  private Map<String, MerchantAccountEntity> merchantAccount = new HashMap<>();

  public Map<String, MerchantAccountEntity> getMerchantAccount() {
    return merchantAccount;
  }

  @Override
  public String getSeNumber() {
    return getStringValue(ParamName.SE_NUMBER);
  }

  @Override
  public String getSubmitterId() {
    return getStringValue(ParamName.SUBMITTER_ID);
  }

  private String getStringValue(ParamName paramName) {
    if (merchantAccount.containsKey(paramName.name())) {
      return merchantAccount.get(paramName.name()).getParamValue();
    }
    return null;
  }
}


@Entity
@Table(schema = "P", name = "MERCHANT_ACCOUNT")
public class MerchantAccountEntity implements MerchantAccount, PersistentEntity<String> {

  @EmbeddedId
  private MerchantAccountEntityPK id;

  @Column(name = "PARAM_VALUE")
  private String paramValue;

  @Override
  public String getId() {
    return id.getId();
  }

  @Override
  public String getParamName() {
    return id.getParamName();
  }

  @Override
  public String getParamValue() {
    return paramValue;
  }
} 

 @Embeddable
 public class MerchantAccountEntityPK implements Serializable {

      private static final long serialVersionUID = 4598402273070888330L;

      @Column(name = "MERCHANT_ACCOUNT_ID")
      private Long merchantAccountId;

      @Column(name = "PARAM_NAME")
      private String paramName;


      public String getId() {
        return merchantAccountId + "+" + paramName;
      }

      public String getParamName() {
        return paramName;
      }   
 } 

0 个答案:

没有答案