3

I am trying to use the Spring Data JDBC and I cannot understand if there is a possibility to customize the column names of the embedded object (in JPA we use @AttributeOverrides for this).

In my model I created a class Amount they I would like to reuse in different types of objects.

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

   //getters, settes, contructors
}

I would like to save it like 2 embedded values in two tables: houses and cars.

In the table houses I want the columns to be called house_price_value and house_price_currency. In the table cars they should be called car_eval_value and car_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
}

The problem is that the annotation @Column is applicable only to the attribute and should be set on the Amount-class level. which makes this class not reusable.

In JPA I would have used this but in JDBC this annotation is not found:

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

Don't I see another solution?

youngDev
  • 329
  • 3
  • 16
  • Wasn't that suitable for you: https://docs.spring.io/spring-data/jdbc/docs/1.0.6.RELEASE/reference/html/#jdbc.entity-persistence.custom-column-name – mate00 Apr 16 '19 at 07:57
  • @mate00 not completely. I should put these columns inside the Amount-class and therefore I don't know how to customize it for different tables and make it reusable – youngDev Apr 16 '19 at 08:03
  • I think I'm not following. Do you want to use Amount in more than one table (let's say A, B and C), but only in table A you want a different name for the column than in B and C? – mate00 Apr 16 '19 at 08:05
  • @mate00 yes, almost. in all the tables I want to have different column names for "value" and "currency" – youngDev Apr 16 '19 at 08:22

1 Answers1

1

I think it should work with value attribute of the @Embedded. This value should contain a prefix.

@Embedded(value = "house_price_")
Amount approvalValue;
youngDev
  • 329
  • 3
  • 16