Java / Hibernate uses interfaces on entities
I'm using annotated hibernate, and I wonder if it's possible
I have to set up a series of interfaces representing objects that can be persisted, as well as an interface of the main database class, which contains several operations for persisting these objects (... Database API)
In this case, I must implement these interfaces and maintain them with hibernate
So I have, for example:
public interface Data { public String getSomeString(); public void setSomeString(String someString); } @Entity public class HbnData implements Data,Serializable { @Column(name = "some_string") private String someString; public String getSomeString() { return this.someString; } public void setSomeString(String someString) { this.someString = someString; } }
Now, that's good When I want to nest entities, trouble comes The interface I want is easy:
public interface HasData { public Data getSomeData(); public void setSomeData(Data someData); }
However, when I implement this class, I can operate according to the following interface and get an error from hibernate that it does not know the "data" class
@Entity public class HbnHasData implements HasData,Serializable { @OneToOne(cascade = CascadeType.ALL) private Data someData; public Data getSomeData() { return this.someData; } public void setSomeData(Data someData) { this.someData = someData; } }
The simple change is to change the type from "data" to "hbndata", but this will obviously destroy the interface implementation and make abstraction impossible
Anyone can explain to me how to use hibernate to achieve this?
Solution
Maybe onetoone targetEntity ?:
@OneToOne(targetEntity = HbnData.class,cascade = CascadeType.ALL) private Data someData;