Quantcast
Channel: Java Programming Forum - Learn Java Programming - Hibernate
Viewing all articles
Browse latest Browse all 76

JPA design problem: Referencing entities which implement a specific interface

$
0
0
Hello all,

currently I am making my first steps regarding JPA / Hibernate. The basics are pretty clear to me, but now I encountered a design problem I could not solve.
The problem is about the table design and/or the proper JPA/Hibernate annotations. I hope you can help me. :)

This is the class diagram of the entities I want to persist:

I want to keep track of the Flights of the two completely different classes: Duck (which is an Animal) and Plane, which both implement the interface Flyable.
A Flight has a reference to a single Flyable object. A Flyable has references to a set of Flights.

So I thought of the following tables:

My problem is, how do I reference a Flyable entity? I cannot use a base table "flyable" for Ducks and Planes, because Duck already has a parent table: "animal".

My classes with the JPA annotations so far look like this:
Code:

@Entity
@Table(name = "flight")
public class Flight
{
        @Id
        @GeneratedValue
        @Column(name = "id")
        private long        id;

        // ???
        private Flyable        flyable;
}

EDIT: added the missing method (see this post: http://www.java-forums.org/hibernate...tml#post349880)
Code:

public interface Flyable
{
        public Set<Flight> getFlights();
}

Code:

@Entity
@Table(name = "animal")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Animal
{
        @Id
        @GeneratedValue
        @Column(name = "id")
        private long                                id;
}

Code:

@Entity
@Table(name = "duck")
public class Duck extends Animal implements Flyable
{

}

Code:

@Entity
@Table(name = "plane")
public class Plane implements Flyable
{
        @Id
        @GeneratedValue
        @Column(name = "id")
        private long                                id;
}

What is the correct annotation for the "flyable" attribute of the Flight class? Or is there another solution to persist my classes?
I would really appreciate any help you can offer me!

Regards,
NegroManus

Viewing all articles
Browse latest Browse all 76

Trending Articles