问题
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