Annotate a Map field with @ElementCollection or @Basic

#1

Hello,

we have an entity with a field whose type is a Map.

Once we annotate the field with @ElementCollection, in the other case with @Basic.

Is @Basic also a valid annotation for a Map field? And when it is valid why @Basic is faster at runtime.

@Entity
public class TestEntity {

    @ElementCollection
    private Map<Integer, String> values = new HashMap<>();

    @Basic
    private Map<Integer, String> values = new HashMap<>();
}

best regards
BTC

#2

> Is @Basic also a valid annotation for a Map field?

It is valid in ObjectDB (although probably not intended to be used for maps in JPA).

> And when it is valid why @Basic is faster at runtime.

It could be due to the different default fetch value (LAZY for ElementCollection, EAGER for Basic). You may try using an @ElementCollection(fetch=FetchType.EAGER) and see if it is fast as @Basic

ObjectDB Support
#3

I tested it already with @ElementCollection(fetch=FetchType.EAGER).
It is little bit faster as before.
But also this configuration needs 80 seconds in comparing to @Basic (60 seconds).

#4

Another possible difference is that it seems that @ElementCollection is considered to be fully embedded with no external relationships and therefore excluded (as optimisation) from automatic cascading.

You can also try @OneToMany(fetch=FetchType.EAGER), which may be more appropriate for maps that contain external relationships.

ObjectDB Support

Reply