Home > Blockchain >  Spring-Data-JPA @ElementCollection converts Double to ints in postgres db
Spring-Data-JPA @ElementCollection converts Double to ints in postgres db

Time:09-17

Currently I try to learn Spring (Boot). For my application i have chosen Postgres as my database.

While most of my classes, JoinColums etc. work well, I cannot get my head around the @ElementCollection annotation. From what I understand, it creates a second table to resolve Lists with 1-n relations. But other then I intended, jpa translate my List to a table with integers.

@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "mts_values")
public class MtsValue extends BaseEntity {

    @GeneratedValue
    private Long valueId;

    @ElementCollection(targetClass = Double.class)
    @Column(name = "values")
    private List<Double> values;
}

These are the two colums in the database, the right one is the MtsValue Table and the other the one that is created by the @ElementCollection annotation

enter image description here enter image description here

Would be nice if someone can explain me, how I get JPA to stop translating my Doubles to ints :)

Thanks in advance!

Greetings Pascal

CodePudding user response:

I had the same problem. Basically target class is 'optional', it is used for specifying Java objects that have generics, all of them but the collections. If it is a collection it is not necessary to specify the target class. Well in my case it just messed up things specifying it. So in your case it would be

@ElementCollection
@Column(name = "values")
private List<Double> values;

CodePudding user response:

Found a solution myself, I needed to use BigDecimal instead of Double and use BigDecimal as TargetClass. Now everything works.

  • Related