What would be the best practice for implementing the following situation with Hibernate.
We define an abstract class that will form the base of any object that we want to store in our database. It contains an identifier, etc. Etc.
public abstract class ModelObject {
protected int id;
...
}
Now we will subclass our base class for special cases when more than one object will have similar fields.
public abstract class ModelObjectWithNose extends ModelObject {
...
protected Nose nose;
}
Now for all the classes we want to have Nose:
public class Person extends ModelObjectWithNose { ... }
public class Animal extends ModelObjectWithNose { ... }
The real problem that we have now is that this relationship must be bi-directional. Each particular class must know which one Nosebelongs to them, but each Nosemust also know which object it belongs to.
public class Nose {
...
private ModelObjectWithNose owner;
}
@OneToOne, Person Nose, Nose Person.
:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class ModelObjectWithNose extend ModelObject { ... }
:
@Entity
public class Person extends ModelObjectWithNose { ... }
... .. .. ..
1) Nose ModelObjectWithNose? :
@Entity
public class Nose {
...
// this causes: 'Basic' attribute should not be 'Persistence Entity'
ModelObjectWithNose owner;
}
2) / ? ?
!
, , , , . Inheritance , , . , , ( ) Nose object id, , ModelObjectWithNose, .