Hello all,
I have the following interface and class layout:
Code:
public interface InstanceManager {
public ManagedInstance getManagedInstance(String key);
public void addManagedInstance(String key, ManagedInstance instance);
public void removeManagedInstance(String key);
}
public interface ManagedInstance {
public InstanceManager getOwner();
// ... other methods of the managed instance
}
@Entity
public class InstanceManagerImpl implements InstanceManager {
private long id;
private Map<String, ManagedInstance> instances;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
@CollectionOfElements
public Map<String, ManagedInstance> getInstances() {
return this.instances;
}
public void setInstances(Map<String, ManagedInstance> instances) {
this.instances = instances;
}
// other methods not important here
}
@MappedSuperclass
public abstract class AbstractManagedInstance implements ManagedInstance {
private InstanceManager owner;
@Target(InstanceManagerImpl.class)
public InstanceManager getOwner() {
return this.owner;
}
public void setOwner(InstanceManager owner) {
this.owner = owner;
}
}
@Entity
public class FirstManagedInstance extends AbstractManagedInstance {
// ...
}
@Entity
public class SecondManagedInstance extends AbstractManagedInstance {
// ...
}
@Entity
public class JustAnotherManagedInstance extends AbstractManagedInstance {
// ...
}
There is a factory bean creating a suitable ManagedInstance and assigning it to the InstanceManager. The problem here is that I cannot use the @Target annotation as the concrete ManagedInstance's are mixed in the container instance, only sharing a common interface.
I know that the annotations here are not working and not complete, they are just to demonstrate the whole idea.
My questions are clear:
(1) Is this scenario possible to implement with Hibernate?
(2) If yes, how would a corrent mapping (preferrably using annotations) be done?
Thanks for any pointers you all may have!
Regards, Ernest