我有这个ER模型
Message 0..1 <--> 0..1 MessageDetail
PK:ID_MESSAGE PK: ID_DETAIL
NAME DETAIL
FK: ID_MESSAGE
相对的Object映射是:
class OnlineEventMessage {
@Id
@Column(name = "ID_EVENT_MESSAGE")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ")
private Long idEventMessage;
@OneToOne(mappedBy="onlineEventMessage", cascade=CascadeType.PERSIST)
private EventMessageAnagrafica eventMessageAnagrafica;
}
public class EventMessageAnagrafica {
@Id
@Column(name = "ID_EVENT_MESSAGE_ANAG")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ")
private Long idEventMessageAnagrafica;
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name = "FK_ID_EVENT_MESSAGE")
private OnlineEventMessage<?> onlineEventMessage;
}
此测试显示了我希望如何处理对象:
@Test
public void testSaveItem() {
EntityManager entityManager = factoryCont0.createEntityManager();
entityManager.getTransaction().begin();
OnlineEventMessage<String> eventMessage = new OnlineEventMessage<String>(EventType.ONLINE_REIMPIANTO_CONTRATTO);
EventMessageAnagrafica eventMessageAnagrafica = new EventMessageAnagrafica(multichannelId);
eventMessage.setEventMessageAnagrafica(eventMessageAnagrafica);
entityManager.persist(eventMessage);
entityManager.getTransaction().commit();
entityManager.close();
}
当我在eventMessageAnagrafica上持有eventMessage时,它不会保存FK。 保存底层关联的两种方法是:
1)添加以下代码行:eventMessageAnagrafica.setOnlineEventMessage(eventMessage);
并保存子对象:entityManager.persist(eventMessageAnagrafica);
2)更改父设置器如下:
public void setEventMessageAnagrafica(EventMessageAnagrafica eventMessageAnagrafica) {
this.eventMessageAnagrafica = eventMessageAnagrafica;
if (eventMessageAnagrafica != null) {
eventMessageAnagrafica.setOnlineEventMessage(this);
}
}
还有其他干净的方法可以做到这一点吗? 附:最初FK在父表Message上,但DBA告诉我这不是一个好的ER设计。
亲切的问候 马西莫
答案 0 :(得分:2)
维护内存中对象之间双向关系的一致性是您的责任。保存关系时,JPA提供程序会查看关系的拥有方,即没有mappedBy
的方面。
我认为第二种方法是最干净的,因为它会自动保持一致性,因此您不能忘记这样做。或者,您可以创建一个特殊函数来关联除setter之外的这些实体,并限制对setter的访问。
答案 1 :(得分:0)
entityManager.getTransaction().begin();
OnlineEventMessage<String> eventMessage = new OnlineEventMessage<String>(EventType.ONLINE_REIMPIANTO_CONTRATTO);
EventMessageAnagrafica eventMessageAnagrafica = new EventMessageAnagrafica(multichannelId);
eventMessage.setEventMessageAnagrafica(eventMessageAnagrafica);
//add this line
eventMessageAnagrafica.setEventMessage(eventMessage);
entityManager.persist(eventMessage);