I have an interface IExample, for my class Example, which is a Hibernate entity, because I want my app to allow other devs to change the implementation of my model layer. My interface is as follows:
public interface IExample {
public int getId ();
public void setId (int id);
public IOtherClass getOtherClass ();
public void setOtherClass (IOtherClass otherClass);
}
IOtherClass is the interface for another Hibernate entity OtherClass.
My class is:
@Entity
@Table(name="example")
public class Example implements IExample {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@OneToOne(targetEntity=OtherClass.class)
@JoinColumn(name="otherclass")
private IOtherClass otherClass;
}
As you can see, I'm using the interface IOtherClass and mapping it to a Hibernate entity with
targetEntity=OtherClass.class
This works fine, but the problem comes when I try to use DAOs. As I'm working with IOtherClass in my Example, I would like to be able to tell my hibernate that when I call:
Example example = new Example();
IOtherClass otherClass = example.getOtherClass(); // Returns IOtherClass
getCurrentSession().save(otherClass);
I'm not trying to save the interface but my OtherClass entity. If I try to do this, actually, I get the exception:
Unable to locate persister: IOtherClass
One option would be to explicitly cast every IOtherClass entity my DAO gets to OtherClass, but that is unsafe, and doesn't look like the best way to code this.
My question is: Is there a way (through annotations or something like that) to tell my DAOs what class to look for when they receive an instance of IOtherClass?