14

I am using annoted Hibernate, and I'm wondering whether the following is possible.

I have to set up a series of interfaces representing the objects that can be persisted, and an interface for the main database class containing several operations for persisting these objects (... an API for the database).

Below that, I have to implement these interfaces, and persist them with Hibernate.

So I'll 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, this works fine, sort of. The trouble comes when I want nested entities. The interface of what I'd want is easy enough:

public interface HasData {
  public Data getSomeData();
  public void setSomeData(Data someData);
}

But when I implement the class, I can follow the interface, as below, and get an error from Hibernate saying it doesn't know the class "Data".

@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 would be to change the type from "Data" to "HbnData", but that would obviously break the interface implementation, and thus make the abstraction impossible.

Can anyone explain to me how to implement this in a way that it will work with Hibernate?

3
  • 2
    And what's the point of using interfaces for entities? What state does an interface have that needs to be persisted? Interfaces are about behavior, not state, and I don't see why you'd like to persist an interface. Commented May 9, 2010 at 22:12
  • 3
    I realize this... The intention was to create an abstraction over the persistence framework, to make it possible to implement it several times, using different (or no) frameworks. The application in question has been set up absolutely modular, in a way beyond my control, and I need a way to specify which messages the database should be able to respond to, and which properties the objects have... and so: interfaces. The idea is that one could implement the interfaces, and switch modules. However, implementing the interfaces gives some trouble with Hibernate... and hence my question. Commented May 9, 2010 at 22:47
  • "What state does an interface have that needs to be persisted?" Anything that can be accessed via the getters and setters defined by the interface? Commented Jul 25, 2018 at 12:14

2 Answers 2

21

Maybe OneToOne.targetEntity?:

@OneToOne(targetEntity = HbnData.class, cascade = CascadeType.ALL)
private Data someData;
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, that was actually what I was looking for. ^^
Same here! You are my hero!
I don't see how this solve the problem, it is still limited to one type. Replace Data by HbnData is the same no ?
@MilacH - that is not the reason to do this, the refactoring with this is a simple as changing the annotation, using the implementation of the interface ties you to that implementation and means changing it you have to change ALL the code that is dependant on that reference. That is the main reason to use Interfaces to being with. List is always preferable to ArrayList for the same reason.
4

The interface that I usually use is Data Access Object, or DAO. Using Java generics, I can write it just once; Hibernate makes it possible to write the implementation just once, too:

package persistence;

import java.io.Serializable;
import java.util.List;

public interface GenericDao<T, K extends Serializable>
{
    T find(K id);
    List<T> find();
    List<T> find(T example);
    List<T> find(String queryName, String [] paramNames, Object [] bindValues);

    K save(T instance);
    void update(T instance);
    void delete(T instance);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.