Hibernate OneToOne BiDirectional Optional Relationship:在没有可选对象的情况下插入时工作,在使用新的可选对象更新时中断

Hibernate OneToOne BiDirectional Optional Relationship: Works when inserted without optional object, Breaks when updated with new optional object

我在两个对象 ChecklistItem 和 ButtonAction 之间建立了以下 OneToOne 关系设置(如下面的代码片段所示)。我想这是一种独特的设置。它是双向的,但从 ChecklistItem 方面来看是可选的,ButtonAction 是所有者,将 ChecklistItem 的外键存储为其主键。

清单项Class:

@Entity
@Table(name = "CHECKLIST_ITEM", schema = "CHKL_APP")
public class ChecklistItem implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CHECKLIST_ITEM_ID_SEQ")
    @SequenceGenerator(name = "CHECKLIST_ITEM_ID_SEQ", sequenceName = "CHKL_APP.CHECKLIST_ITEM_ID_SEQ")
    private Long id;

    @OneToOne(optional = true, mappedBy = "checklistItem", cascade = CascadeType.ALL)
    private ButtonAction button;

    //...
}

ButtonActionClass:

@Entity
@Table(name = "ACTION_BUTTON", schema = "CHKL_APP")
public class ButtonAction implements Serializable {
    @Id
    @Column(name = "checklist_item_id", unique = true, nullable = false, insertable = true, updatable = false)
    @GenericGenerator(name = "generate-from-checklist-item", strategy = "foreign", parameters = @Parameter(name = "property", value = "checklistItem"))
    @GeneratedValue(generator = "generate-from-checklist-item")
    private Long checklistItemId;

    @OneToOne
    @PrimaryKeyJoinColumn
    @JsonIgnore
    private ChecklistItem checklistItem;

    //...
}

我正在使用 SpringBoot,所以我有一个扩展 SpringBoot 的 CrudRepository 的 ChecklistItemRepository 接口:

public interface ChecklistItemRepository extends CrudRepository<ChecklistItem, Long> {}

在我的 ChecklistItem 服务中,我将保存方法配置为如下工作:

@Service
@Transactional
public class ChecklistItemServiceImpl implements ChecklistItemService {

    @Override
    public ChecklistItem saveChecklistItem(ChecklistItem checklistItem) {
        processButtonAction(checklistItem);
        return checklistItemRepository.save(checklistItem);
    }

    private void processButtonAction(ChecklistItem checklistItem,String username) {
        ButtonAction button = checklistItem.getButton();

        if(button != null) {
            button.setChecklistItem(checklistItem);

            if(checklistItem.getId() != null){
                button.setChecklistItemId(checklistItem.getId());
            }
        }
    }

    //...
}

因此,无论何时保存 ChecklistItem(通过 POST 或 PUT),它都会更新 ButtonAction(当用户 selected 以包含一个时)引用 ChecklistItem,及其调用保存前的 ID(如果不为空)。

这是我的问题... 当用户 PUTs 带有新 ButtonAction 的 ChecklistItem(用户最初 POSTed 没有 ButtonAction 的 ChecklistItem ), 我收到以下错误:

org.springframework.orm.jpa.JpaSystemException: attempted to assign id from null one-to-one property [com.me.chklapp.checklistitem.action.ButtonAction.checklistItem];
nested exception is org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.me.chklapp.checklistitem.action.ButtonAction.checklistItem]

我在网上发现的每个 'answer' 都说需要设置关系,但我已经在我的服务中这样做了。我已经通过调试和检查每个对象是否具有对另一个对象的非空引用来验证它正在这样做。另外,我找不到其他人遇到与我相同的问题,在某些情况下,它可以保存,而在其他情况下,它会破坏;在那些情况下是全有或全无。

当我打开更详细的休眠日志记录时,我唯一能看到的可疑之处是就在抛出错误之前,它在 cheklistitem 的按钮操作 table 上执行 select身份证匹配。我猜 Hibernate 这样做是为了确定它是否需要对按钮操作 table 进行插入或更新。但也许它然后使用那个空行而不是我的 ChecklistItem 上的 ButtonAction 对象?

感谢您的帮助!

考虑在所有者 ChecklistItem 首次创建时创建一个新的 ButtonAction。然后根据需要更新 ButtonAction。

我不太确定,但我认为稍后尝试派生 ID 时会出现一些 Hibernate 问题。

同时创建 ButtonAction 和 ChecklistItem 应该可行。

老实说,我仍然不确定为什么会出现我原来的问题或为什么这个解决方案有效,所以如果有人可以阐明这些事情,请发表评论;我希望更好地理解。

感谢@CarlitosWay 和@Matthew 与我一起努力寻找解决方案。当 CarlitosWay 评论说不需要 GeneratedValue 和 GenericGenerator 时,他说到点子上了。我不能只是删除它们; 我需要一些方法来告诉 Hibernate 从哪里获取 ButtonAction 的 ID ,但这让我开始沿着轨道寻找替代配置。我发现了一个看起来很有希望的:MapsId。我查看了一些示例并仔细研究了一些东西,看看哪些会起作用。如果不出意外,这似乎证实了我的设置有些独特,因为我的解决方案与 MapsId 的用途的常见示例不同。

我在下面发布了我的结果代码,但同样,我仍然不完全确定 Hibernate 如何处理所有这些,所以我可能在这里有一些多余的注释。如果可能,请告诉我如何清理它。

ButtonActionClass:

@Entity
@Table(name = "ACTION_BUTTON", schema = "CHKL_APP")
public class ButtonAction implements Serializable {
    @Id
    @Column(name = "checklist_item_id", unique = true, nullable = false, insertable = true, updatable = false)
    private Long checklistItemId;

    @OneToOne
    @MapsId
    @PrimaryKeyJoinColumn
    @JsonIgnore
    private ChecklistItem checklistItem;

    //...
}

基本上,我将 checklistItemId 上的 GenericGenerator 和 GeneratedValue 注释交换为 checklistItem 上的 MapsId 注释。在我见过的大多数其他示例中,MapsId 注释在另一个 class 上(在这种情况下将是 ChecklistItem),但我在想,因为 ButtonAction 是关联的所有者以及 ID 的来源from,它需要在本机对象上。 ChecklistItem、ChecklistItemRepository 和 ChecklistItemServiceImpl 都与我问题中的原始代码没有变化。

理论上,我的原始代码是执行此 JPA 等效操作的 Hibernate 方法。但由于它们的行为不同,我一定是误解了什么,如果你知道原因,请回复!