Why are my Map entries not stored?

#1

Why are my values of the map "states" not stored in the database? It seems the 'State' is stored, but not the 'Authority'?

@Entity
public class Ticket {
    @Id @GeneratedValue
    private long id;

    @OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL, mappedBy = "ticket",     orphanRemoval=true) @MapKeyEnumerated(EnumType.STRING)
    private Map<State, Authority> states = new HashMap<State, Authority>();
}

@Entity
public class Authority {
    @Id @GeneratedValue
    private long id;

    private LocalDateTime dateTime = null;

    @ManyToOne(fetch = FetchType.EAGER)
    private Ticket ticket = null;

    [...]
}

public enum State {
    OPEN("Offen"),
    CLOSED("Geschlossen");
    [...]
}
#2

Actually the  'Authority' (value) is stored (I see entries in the Explorer), but there seems to miss the connection. After DB request I can't get it. The 'State' (key) I can get after DB request, but I can't see it in the Explorer.

#3

I have now changed to and it is working:

@OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval=true) @MapKey(name="state") @MapKeyEnumerated(EnumType.STRING)
private Map states = new HashMap();

Is this correct now? "mappedBy" is not required? But I have to set "@MapKey"? Or is this optional?

#4

You have 2 options:

  1. Not using @MappedBy: In that case the map content will be stored embedded in the Ticket instances and no further information is needed  by ObjectDB (so it should work also without specifying @MapKey, etc.).
  2. Using @MappedBy: In that case the map will NOT be stored in the Ticket instances and instead will be retrieved when needed using queries on the Authority instances. You must specify the Authority class as a value, e.g. using Map<State, Authority> as the field type, specify the reverse field state in that class in mappedBy and the key field, e.g. using MapKeyEnumerated. Otherwise ObjectDB will not have the minimum required information to create the necessary queries to retrieve the data.

See more regarding the differences between these types of relationships in the manual.

ObjectDB Support

Reply