2

场景如下(表格所示)

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 

交付和渠道实体之间基本上是一对一的关系,但由于每个具体渠道(传真、电子邮件)都有不同的成员,我想在两个实体之间创建一个通用接口(渠道)并将其用于@OneToOne 关系。在我看来,这是一个简单的场景,你们中的很多人可能已经经历过,但我无法成功。我试着把那个 targetEntity 东西放了,但没有用。仍然说“交付引用了一个未知实体”

有任何想法吗?提前致谢

4

1 回答 1

1

使用abstract超类ChannelTABLE_PER_CLASS继承策略怎么样?像这样的东西:

@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;

    // ...
}
于 2010-05-01T00:11:00.380 回答