【问题标题】:How to customize the column names of the embedded object?如何自定义嵌入对象的列名?
【发布时间】:2019-04-16 07:35:58
【问题描述】:

我正在尝试使用 Spring Data JDBC,但我无法理解是否可以自定义嵌入对象的列名(在 JPA 中,我们为此使用 @AttributeOverrides)。

在我的模型中,我创建了一个类Amount,我想在不同类型的对象中重用它们。

public class Amount {
    private BigDecimal value;
    private String currency;

   //getters, settes, contructors
}

我想将它像 2 个嵌入值一样保存在两个表中:housescars

在表houses 中,我希望将列称为house_price_valuehouse_price_currency。在cars 表中,它们应该被称为car_eval_valuecar_eval_currency

public class House {
      @Id
      Long id;
      int numberOfRooms; 
      @Embedded
      Amount amount;

   //other attributes, getters, setters, constructors
}
public class Car {
      @Id
      Long id;
      String model;
      @Embedded
      Amount amount;

   //other attributes, getters, setters, constructors
}

问题是注解@Column只适用于属性,应该设置在Amount-class级别。这使得这个类不可重用。

在 JPA 中我会使用它,但在 JDBC 中找不到此注释:

@AttributeOverrides(value = {
    @AttributeOverride(name = "value", column = @Column(name = "house_price_value")),
    @AttributeOverride(name = "currency", column = @Column(name = "house_price_currency"))
})

我没有看到其他解决方案吗?

【问题讨论】:

  • @mate00 不完全。我应该将这些列放在 Amount 类中,因此我不知道如何为不同的表自定义它并使其可重用
  • 我想我没有关注。您是否要在多个表中使用 Amount(假设 A、B 和 C),但仅在表 A 中您希望列的名称与 B 和 C 中的不同?
  • @mate00 是的,差不多。在所有表格中,我希望“价值”和“货币”具有不同的列名

标签: java spring-data-jdbc


【解决方案1】:

我认为它应该与@Embeddedvalue 属性一起使用。此值应包含前缀。

@Embedded(value = "house_price_")
Amount approvalValue;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2014-03-03
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多