为什么 hibernate 试图在数据库中查找实体而不是创建它? [英] Why is hibernate trying to find an entity in the database instead of creating it?

查看:32
本文介绍了为什么 hibernate 试图在数据库中查找实体而不是创建它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体.

@Entity
public class Parent {
    @Id
    private UUID id;
    
    private String someValue;
    
    @OneToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "child_id", referencedColumnName = "id")
    private Child child;
}

@Entity
public class Child {
    @Id
    @GeneratedValue
    private UUID id;
    
    private String someVariable;
    
    private Integer someValue;
}

实体持久化逻辑非常简单:

And the entity persistence logic is pretty simple:

@Override
public ParentDto create(ParentDto parentDto) {
    if (repository.existsById(parentDto.getId())){
        throw new ParentWithIdAlreadyExistsException(parentDto.getId());
    }
    return mapper.toDto(repository.save(mapper.toDomain(parentDto)));
}

但是当我尝试保留父实体时 - 休眠抛出异常

But when i try to persist the Parent entity - hibernate throw exception

嵌套异常是org.springframework.orm.jpa.JpaObjectRetrievalFailureException:无法找到带有 id 的 .child6d387565-0e7c-4b0d-85ba-c21cdbb39871;嵌套异常是javax.persistence.EntityNotFoundException:无法找到.id 为 6d387565-0e7c-4b0d-85ba-c21cdbb39871] 的孩子根本原因

nested exception is org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find .child with id 6d387565-0e7c-4b0d-85ba-c21cdbb39871; nested exception is javax.persistence.EntityNotFoundException: Unable to find .Child with id 6d387565-0e7c-4b0d-85ba-c21cdbb39871] with root cause

为什么 hibernate 试图在数据库中查找 Child 实体而不是创建它?

Why is hibernate trying to find the Child entity in the database instead of creating it?

推荐答案

您的班级名称有拼写错误.类名不能以方括号()开头.

There were typo in your class names. Classname cannot start with brackets ().

这篇关于为什么 hibernate 试图在数据库中查找实体而不是创建它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆