简体   繁体   中英

Mapping a complex Map in JPA

i just can't obtain a persistence with an entity which has a field like this:

private Map<String, List<String>> filterValueRange;

i've tried so far:

@ElementCollection
    @JoinTable(name="ATTRIBUTE_VALUE_RANGE", joinColumns=@JoinColumn(name="ID"))
    @MapKeyColumn (name="Filter_Id")
    @Column(name="FILTER")
    private Map<String, List<String>> filterValueRange;

But it seems there is still something missing.

Can anybody point me to the right direction?

I'm using jpa as interface, but there's Hibernate under the hood.

thanks!

I don't believe it is possible. What you are trying to achieve is to map element collection of element collections.

In case I am wrong, you should use the @CollectionTable annotation to define your jointable.

But I think you need to define Embedable that represent value range and has its own ElementCollection of values. Than you can map this embedable to your filterValueRange and access the list of values through it.

If you don't need to query by your filterValueRange you can serialize it simply to Blob.

Mapping nested collection relationship is not supported with JPA however you can easily overcome this by changing the object model a little, for example:

@OneToMany(mappedBy = "parent")
@MapKeyColumn (name="Filter_Id")
private Map<String, ValueRange> filterValueRange;

and in the corresponding ValueRange entity:

@ManyToOne
private Parent parent;

Alternatively you may also take a look at the following post:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM