【发布时间】:2017-05-01 09:21:17
【问题描述】:
我正在努力,
ChildTable child = new ChildTable();
clild.setParentTwoID(prentTwoID);
ParentOne parentOne = new ParentOne();
parentOne.getChildTableList().add(childTable);
parentReposetory.save(parentOne);
其中,ParentOne 位于 schema S1 中,ParentTwo 以及 ChildTable 位于 schema S2 中。
ParentOne.java
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "ParentOne", schema = "S1")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, scope = ParentOne.class)
public class ParentOne implements Serializable {
private static final long serialVersionUID = 7502273069461829133L;
@Id
@Column(name = "ParentOneID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer parentOneID;
@OneToMany(targetEntity = ChildTable.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "ParentOneID", nullable = false, insertable = false, updatable = false)
private List<ChildTable> childTableList;
}
ParentTwo.java
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "ParentTwo", schema = "S2")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, scope = ParentTwo.class)
public class ParentTwo implements Serializable {
private static final long serialVersionUID = 7502273069461829133L;
@Id
@Column(name = "ParentTwoID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer parentTwoID;
@OneToMany(targetEntity = ChildTable.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "ParentTwoID", nullable = false, insertable = false, updatable = false)
private List<ChildTable> childTableList;
}
ChildTable.java
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "ChildTable", schema = "S2")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, scope = ChildTable.class)
public class ChildTable implements Serializable {
private static final long serialVersionUID = -6331600489988183852L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ChildTableID", unique = true, nullable = false)
private Integer childTableID;
@Column(name = "ParentOneID")
private Integer parentOneID;
@Column(name = "ParentTwoID", nullable = false)
@NotNull(message = "mandatory_field")
private Integer parentTwoID;
}
这里 ParentTwo 已经被持久化了,我有它的 ID。我将只保留 ParentOne 以及不同模式的 ChildTable。当我这样做时,我会收到错误提示。
rg.springframework.dao.DataIntegrityViolationException: 不能 执行语句; SQL [不适用];约束[空];嵌套异常是 org.hibernate.exception.ConstraintViolationException:不能 执行语句
原因:com.microsoft.sqlserver.jdbc.SQLServerException: 不能 将值 NULL 插入列“ParentOneID”,表 'PT_Sample.S2.ChildTable';列不允许空值。插入失败。
SQL 错误:515,SQLState:23000
【问题讨论】:
-
该错误表明
ChildTable.ParentOneID列具有NOT NULL约束,并且正在发出INSERT,该列具有null值。由于您没有在代码中的任何位置设置该列的值,因此预计会出现错误。究竟是什么问题? -
@manish 因为 JPA 将插入 ParentOne 表,该表将创建一个 parentOneID,然后使用生成的 parentOneID 插入 ChildTable。但对我来说,这并没有发生。
标签: java sql-server hibernate spring-data-jpa jpa-2.0