views:

64

answers:

1

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  [email protected]
201  [email protected] 

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

+1  A: 

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;

    // ...
}
Pascal Thivent
Pascal - thanks. This is what I thought after posting the question and while driving home :) I will come back and post what succeed or if I still have some issues. thanks again
OpenSource
I just created concrete superclass (Delivery) and made Fax and Email as sub classes. It works for my situation. If needed I can still make them implment an interface for some polymorphism later on. Thanks.
OpenSource