我有这样的类层次结构:
public abstract class CommunicationCampaign {...}
public class EmailCampaign extends CommunicationCampaign {...}
public class SmsCampaign extends CommunicationCampaign {...}
public class Trigger {...}
CommunicationCampaign 定义应将哪种消息分发给我们的客户端,触发器定义何时应分发通信。 Campaign应指向两个触发器,测试触发器和生产触发器。这个代码看起来像这样:
public abstract class CommunicationCampaign {
...
@OneToOne(targetEntity = Trigger.class, fetch = FetchType.EAGER)
@PrimaryKeyJoinColumn(name = "TEST_TRIGGER_ID")
private Trigger testTrigger;
@OneToOne(targetEntity = Trigger.class, fetch = FetchType.EAGER)
@PrimaryKeyJoinColumn(name = "TRIGGER_ID")
private Trigger trigger;
...
}
我的问题是我应该如何在触发器侧上映射此关系?这是我第一次遇到这样的问题,所以我很感激任何建议。也许我不应该使用 @OneToOne 映射来解决这个问题,但是我想避免使用 @OneToMany 映射解决方案,因为那时我必须向触发,指示它是否为测试触发。无论是否用作特定广告系列的测试或常规触发器,触发器行为都应完全相同。此外,Trigger与数据库中的其他实体相关,我希望尽可能少地修改它。
public class Trigger {
...
/*How to map relation to SmsCampaign and EmailCampaign?*/
...
}
我正在寻找类似的东西:
public class Trigger {
...
@OneToOne(mappedBy = "testTrigger or trigger", fetch = FetchType.LAZY)
private EmailCampaign emailCampaign;
@OneToOne(mappedBy = "testTrigger or trigger", fetch = FetchType.LAZY)
private SmsCampaign smsCampaign;
...
}