dem31
dem31

Reputation: 57

Multiple @ElementCollection mapped to different columns of the same table

In my POJO class, I have 3 related lists with the same length with no null values:

List<String> date;
List<String> priceA;
List<String> priceB;

Can I map them to 1 table instead of 3, so that I can have a row of (class_id, date, priceA, priceB)?

Upvotes: 3

Views: 3135

Answers (1)

Michele Mariotti
Michele Mariotti

Reputation: 7449

You can only if you are willing to change your java model:

@ElementCollection
@CollectionTable(name = "ITEM_DETAIL", joinColumns = @JoinColumn(name = "ITEM_ID")
private List<ItemDetail> details;

where

@Embeddable
public class ItemDetail implements Serializable
{
    @Temporal(DATE)
    private Date date;

    @Column
    private Double priceA;

    @Column
    private Double priceB;

    // getters/setters
} 

Upvotes: 5

Related Questions