외래 키가없는 데이터베이스에서 엔티티를 만들고 나중에 외래 키를 업데이트하려고합니다.hibernate : 외래 키가없는 엔티티를 저장할 때의 예외
@Entity
public class Business {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int businessId;
@Column
private String businessName;
@ManyToOne()
@JoinColumn(name="billTypeId", nullable=true)
@NotFound(action=NotFoundAction.IGNORE)
private BillType billType;
// getters and setters
}
billType 속성은 null 값을 가지고 있는데 다음과 같은 예외가 점점 오전 : 나는 비즈니스 개체를 유지하고 billTypeId 필드에 0의 값을 가질 수있는 방법
Caused by: java.sql.SQLIntegrityConstraintViolationException: (conn:54)
Column 'billTypeId' cannot be null
at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:152)
at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.getException(ExceptionMapper.java:118)
의 billType 멤버를 is null
귀하의 코드는 데이터베이스에 의해 시행되는 제약 조건과 모순됩니다. NotFound 주석 (일치하지 않는 데이터베이스를 처리하기위한 해킹)을 제거하고 엔터티를 저장하기 전에 청구서 유형을 설정하기 만하면됩니다. –
데이터베이스에 의해 강제되는 제약 조건이 없습니다. billType을 설정하여 제대로 작동시킬 수 있다는 것을 알고 있습니다. 그러나 먼저 BillType없이 Business 개체를 저장하고 이후 단계에서 설정해야합니다. 내 질문은 어떻게? –
그건 예외가 아니라 SQLIntegrityConstraintViolationException입니다. 당신은 거의 제약이 위반되었다고 생각할 것입니다. – Kayaman