how to make openjpa 2.2.0 not persist foreign key

↘锁芯ラ 提交于 2019-12-06 02:47:54

The issue you are having isn't caused by foreign keys. What is happening is that your Asset object contains an Accessibility object that is not being managed by OpenJPA. How to fix this depends on the state of the Accessibility record:

  1. Does the Accessibility record already exist in the database? If so, load it using getEntityManager.find(Accessibility.class, uid) first, and then set it onto your Asset object before you try to persist the Asset object.

  2. If the Accessibility record hasn't already been persisted, then need to persist it first using "getEntityManager.persist(accessibility)" and then set it onto your Asset object before you try to persist it. Or, your other option is to change the cascade type for your accessibility collection to allow persisting, as follows:

@ManyToOne(cascade=cascade={ CascadeType.PERSIST, CascadeType.REMOVE },fetch=FetchType.LAZY)
@JoinColumn(name="ACCESSIBILITY_ID")
@ForeignKey
private Accessibility accessibilityId;

If you make the cacade change, then any unpersisted Accessibility object on your Asset object will be persisted automatically when you persist the Asset object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!