Why is hibernate trying to find an entity in the database instead of creating it? [closed]

风流意气都作罢 提交于 2021-01-05 07:21:22

问题


I have two entities.

@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

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

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


回答1:


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



来源:https://stackoverflow.com/questions/65459248/why-is-hibernate-trying-to-find-an-entity-in-the-database-instead-of-creating-it

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