Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java/Hibernate using interfaces over the entities

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?

like image 258
wen Avatar asked May 09 '10 22:05

wen


People also ask

Can JPA entity implements interface?

If you want to implement your entities in a JPA-compliant way, you will not be able to design a nice and clean fluent interface API. You can only add the fluent methods to your entity class, and keep the getter and setter methods.

Is Hibernate an interface?

Introducing and configuring Hibernate. The Session interface is the primary interface used by Hibernate applications. An instance of Session is lightweight and is inexpensive to create and destroy. This is important because your application will need to create and destroy sessions all the time, perhaps on every request ...

What are the important interfaces used in Hibernate?

The core interfaces of Hibernate framework are: Configuration. SessionFactory. Session.

What is the use of @entity in Hibernate?

Hibernate will scan that package for any Java objects annotated with the @Entity annotation. If it finds any, then it will begin the process of looking through that particular Java object to recreate it as a table in your database! Before Hibernate will start it's scan, not only do we need to invoke the sessionFactory.


2 Answers

Maybe OneToOne.targetEntity?:

@OneToOne(targetEntity = HbnData.class, cascade = CascadeType.ALL)
private Data someData;
like image 80
Sam Donnelly Avatar answered Oct 04 '22 11:10

Sam Donnelly


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);
}
like image 39
duffymo Avatar answered Oct 04 '22 13:10

duffymo