简体   繁体   中英

Interface between two related JPA entities

The scenario is as below (tables shown)

Delivery table
------
id  channelId   type
10  100         fax
20  200         email

Fax table
----
id   number
100  1234567
101  1234598

Email table
-----
id   email
200  a@a.com
201  b@b.com 

basically a one to one relationship between the delivery and the channel entity but since each concrete channel(fax, email) has different members I want to create a generic interface (channel) between the two entities and use it for the @OneToOne relationship. Seems to me a simple scenario where lot of you might have already gone through but I'm unable to succeed. I tried putting that targetEntity thing but no use. Still says "delivery references an unknown entity"

Any ideas? thanks in advance

What about using an abstract super class for the Channel and a TABLE_PER_CLASS inheritance strategy? Something like this:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Channel {
    @Id
    @GeneratedValue
    private Long id;

    // ...
}

@Entity
public class Fax extends Channel {
}

@Entity
public class Email extends Channel {
}

@Entity
public class Delivery {
    @Id
    @GeneratedValue
    private Long id;

    @OneToOne
    private Channel channel;

    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM