Hi.
Is import javax.persistence.MapKey broken in 3.2.0GA, or am I missing something very fundamental?
I have googled for everything I can think of, search most docs and jira.
The class at the bottom using a Set in the OneToMany mapping works. Changing the Set to a Map, adding @Mapkey seems to break lazy loading. Using a Map and javax.persistence.MapKey, the @OneToMany mapping parcels isn't loaded when first accessed. Well, it is loaded, but if the first access is Map.put, it will just init the map, but ignore the put.
When using javax.persistence.MapKey I have to change this method from:
Code:
public void addParcel(Parcel parcel) {
parcel.setProperty(this);
this.parcels.put(parcel.getObjectId(), parcel); // this will just init this.parcels, the put will be ignored
}
to:
Code:
public void addParcel(Parcel parcel) {
parcel.setProperty(this);
this.parcels.put(parcel.getObjectId(), parcel); // this will trigger lazy load, but not add to collection...
this.parcels.put(parcel.getObjectId(), parcel); // ...and this will add to collection.
}
or
Code:
public void addParcel(Parcel parcel) {
logger.debug(this.parcels); // This will trigger lazy load..
parcel.setProperty(this);
this.parcels.put(parcel.getObjectId(), parcel); // .. and this will add to collection
}
Code:
package se.lantmateriet.elips.poc.domain.model;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
@Entity
public class Property extends AbstractDomainEntity {
@OneToMany(mappedBy="property", cascade=CascadeType.ALL)
//@MapKey(name="objectId")
//private Map<UUID, Parcel> parcels;
private Set<Parcel> parcels;
public Property(UUID objectId, Integer objectVersion) {
super(objectId, objectVersion);
this.parcels = new HashSet<Parcel>();//new HashMap<UUID, Parcel>();
}
private Property() {
super();
}
public void addParcel(Parcel parcel) {
parcel.setProperty(this);
this.parcels.add(parcel);
//this.parcels.put(parcel.getObjectId(), parcel);
}
public void removeParcel(Parcel parcel) {
this.parcels.remove(parcel);//(parcel.getObjectId());
}
}