How to work with interfaces and JPA

Niklas Norin picture Niklas Norin · Jan 22, 2012 · Viewed 14k times · Source

I should start out by saying that I am fairly new to Java EE and that I do not have a strong theoretical background in Java yet.

I'm having trouble grasping how to use JPA together with interfaces in Java. To illustrate what I find hard I created a very simple example.

If I have two simple interfaces Person and Pet:

public interface Person
{
    public Pet getPet();
    public void setPet(Pet pet);
}

public interface Pet
{
    public String getName();
}

And an Entity PersonEntity which implements Person as well as a PetEntity which implements Pet:

@Entity
public class PersonEntity implements Person
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private PetEntity pet;

    @Override
    public void setPet(Pet pet)
    {
        /* How do i solve this? */
    }
}

@Entity
public class PetEntity implements Pet
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private String name;

    /* Getters and Setters omitted */

}

How do I properly handle the case in the setPet method in which I want to persist the relationships between the two entities above?

The main reason I want to use interfaces is because I want to keep dependencies between modules/layers to the public interfaces. How else do I avoid getting a dependency from e.g. my ManagedBean directly to an Entity?

If someone recommends against using interfaces on entities, then please explain what alternatives methods or patterns there are.

Answer

Mairbek Khadikov picture Mairbek Khadikov · Jan 22, 2012

You can use targetEntity property in the relationship annotation.

@Entity
public class PersonEntity implements Person {
    private Long id;

    private Pet pet;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    @OneToOne(targetEntity = PetEntity.class)
    public Pet getPet() {
        return pet;
    }        

    public void setPet(Pet pet) {
        this.pet = pet;
    }
}