Short answer: you cannot do this.
But here is a solution that may be suitable for your problem:
Object A still has a relation to object B as a list (or even better, like Set, so the same B cannot be contained more than once).
@OneToMany(mappedBy="a")
private Set<B> bs;
, getter setter as.
, " ":
private transient Map<String, B> bsMappedByCName;
public Map<String, B> getBsMappedByCName() {
if(bsMappedByCName == null) {
bsMappedByCName = new HashMap<String, B>();
for(B b : bs) {
mapB(b);
}
}
return Collections.unmodifiableMap(bsMappedByCName);
}
private void mapB(B b) {
if(bsMappedByCName.put(b.getC().getName(), b) != null) {
}
}
- B . , A:
public void addB(B b) {
bs.add(b);
mapB(b);
}
public void removeB(B b) {
bs.remove(b);
bsMappedByCName.remove(b.getC().getName());
}
- return Collections.unmodifiableMap(...) ( ObservaleCollection apache):
return new Map<String, B>() {
public B put(String name, B b) {
if(!b.getC().getName().equals(name)) {
}
B oldB = get(name);
mapB(b);
return oldB;
}
public B get(String name) {
return bsMappedByCName.get(name);
}
};