【发布时间】:2018-05-26 16:38:16
【问题描述】:
我有这种情况:
父实体:
@Entity
public class Address {
@OneToOne( optional = false, targetEntity = User.class, fetch = FetchType.LAZY, orphanRemoval = true )
@JoinColumn( name = "id_user", referencedColumnName = "id" )
private User user;
}
子实体:
@Entity
public class User {
@OneToOne( optional = false, targetEntity = Address.class, mappedBy = "user", fetch = FetchType.LAZY, orphanRemoval = true )
private Address address;
}
如您所见,双方没有任何级联操作。但是当我保存用户时:
userRepository.save( new User() );
它抛出这个异常:
org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : org.company.models.User.address; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : org.company.models.User.address
.............
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value : org.company.models.User.address
我只想坚持孩子,应该可以,因为@JoinColumn在父母中,所以我不需要在保存之前设置它。
有人可以澄清我这个问题吗?
非常感谢
【问题讨论】:
标签: hibernate jpa spring-data-jpa one-to-one bidirectional