ConstraintViolationException 无法执行语句; SQL [n/a];约束 [id]
ConstraintViolationException could not execute statement; SQL [n/a]; constraint [id]
我有两个实体,fileVersion 和 fileEnvironment,它们具有多对多关系。我正在使用由 fileDeployment 实体建模的联结 table。
联结实体:
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(
name = "file_deployment"
)
public class FileDeploymentEntity {
@EmbeddedId
private FileDeploymentKey id;
@ToString.Exclude
@ManyToOne
@MapsId("fileVersionId")
@JoinColumn(name = "fileVersionId")
private FileVersionEntity fileVersion;
@ToString.Exclude
@ManyToOne
@MapsId("fileEnvironmentId")
@JoinColumn(name = "fileEnvironmentId")
private FileEnvironmentEntity fileEnvironment;
}
是复合键:
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder(toBuilder = true)
public class FileDeploymentKey implements Serializable {
@Column
private UUID fileVersionId;
@Column
private UUID fileEnvironmentId;
}
其 JPA 存储库:
@Repository
public interface FileDeploymentEntityRepository extends
JpaRepository<FileDeploymentEntity, FileDeploymentKey>,
JpaSpecificationExecutor<FileDeploymentEntity> {
}
联结实体为其捕获多对多关系的两个实体:
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(
name = "file_environment"
)
public class FileEnvironmentEntity {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "uuid2")
private UUID id;
@ToString.Exclude
@OneToMany(mappedBy = "fileEnvironment")
private List<FileDeploymentEntity> fileDeployments;
}
FileVersion 是另一个
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(
name = "file_version"
)
public class FileVersionEntity {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "uuid2")
private UUID id;
@ToString.Exclude
@OneToMany(mappedBy = "fileVersion")
private List<FileDeploymentEntity> fileDeployments;
}
以下代码执行良好:
var fileDeploymentEntity = FileDeploymentEntity.builder()
.id(FileDeploymentKey.builder()
.fileVersionId(existingFileVersion.get().getId())
.fileEnvironmentId(existingFileEnvironment.get().getId())
.build())
.deploymentTime(
Instant.now(clock))
.fileEnvironment(existingFileEnvironment.get())
.fileVersion(existingFileVersion.get())
.build();
var result = fileDeploymentEntityRepository.save(fileDeploymentEntity);
但是当最终 fileDeploymentEntityRepository.flush() 被调用时,我得到以下异常:
could not execute statement; SQL [n/a]; constraint [id]
org.hibernate.exception.ConstraintViolationException: could not
execute statement
org.postgresql.util.PSQLException: ERROR: null value in column "id"
violates not-null constraint Detail: Failing row contains
(7670fec3-3766-4c69-9598-d4e89b5d1845,
b9f6819e-af89-4270-a7b9-ccbd47f62c39, 2019-10-15 20:29:10.384987,
null, null, null, null).
如果我也为 2 个实体调用保存,它不会改变结果:
fileVersionEntityRepository
.save(existingFileVersion.get().addFileDeployment(fileDeploymentEntity));
fileEnvironmentEntityRepository
.save(existingFileEnvironment.get().addFileDeployment(fileDeploymentEntity));
如有任何帮助,我们将不胜感激!
对我来说,问题是我不小心用相同的 table 名称命名了另一个实体,这导致生成的架构与我认为的大不相同。
带走课程:
1) 检查有疑问时生成的模式。
var con = dataSource.getConnection();
var databaseMetaData = con.getMetaData();
ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[]{"TABLE"});
System.out.println("Printing TABLE_TYPE \"TABLE\" ");
System.out.println("----------------------------------");
while(resultSet.next())
{
//Print
System.out.println(resultSet.getString("TABLE_NAME"));
}
ResultSet columns = databaseMetaData.getColumns(null,null, "study", null);
while(columns.next())
{
String columnName = columns.getString("COLUMN_NAME");
//Printing results
System.out.println(columnName);
}
我有两个实体,fileVersion 和 fileEnvironment,它们具有多对多关系。我正在使用由 fileDeployment 实体建模的联结 table。
联结实体:
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(
name = "file_deployment"
)
public class FileDeploymentEntity {
@EmbeddedId
private FileDeploymentKey id;
@ToString.Exclude
@ManyToOne
@MapsId("fileVersionId")
@JoinColumn(name = "fileVersionId")
private FileVersionEntity fileVersion;
@ToString.Exclude
@ManyToOne
@MapsId("fileEnvironmentId")
@JoinColumn(name = "fileEnvironmentId")
private FileEnvironmentEntity fileEnvironment;
}
是复合键:
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder(toBuilder = true)
public class FileDeploymentKey implements Serializable {
@Column
private UUID fileVersionId;
@Column
private UUID fileEnvironmentId;
}
其 JPA 存储库:
@Repository
public interface FileDeploymentEntityRepository extends
JpaRepository<FileDeploymentEntity, FileDeploymentKey>,
JpaSpecificationExecutor<FileDeploymentEntity> {
}
联结实体为其捕获多对多关系的两个实体:
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(
name = "file_environment"
)
public class FileEnvironmentEntity {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "uuid2")
private UUID id;
@ToString.Exclude
@OneToMany(mappedBy = "fileEnvironment")
private List<FileDeploymentEntity> fileDeployments;
}
FileVersion 是另一个
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(
name = "file_version"
)
public class FileVersionEntity {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "uuid2")
private UUID id;
@ToString.Exclude
@OneToMany(mappedBy = "fileVersion")
private List<FileDeploymentEntity> fileDeployments;
}
以下代码执行良好:
var fileDeploymentEntity = FileDeploymentEntity.builder()
.id(FileDeploymentKey.builder()
.fileVersionId(existingFileVersion.get().getId())
.fileEnvironmentId(existingFileEnvironment.get().getId())
.build())
.deploymentTime(
Instant.now(clock))
.fileEnvironment(existingFileEnvironment.get())
.fileVersion(existingFileVersion.get())
.build();
var result = fileDeploymentEntityRepository.save(fileDeploymentEntity);
但是当最终 fileDeploymentEntityRepository.flush() 被调用时,我得到以下异常:
could not execute statement; SQL [n/a]; constraint [id]
org.hibernate.exception.ConstraintViolationException: could not execute statement
org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint Detail: Failing row contains (7670fec3-3766-4c69-9598-d4e89b5d1845, b9f6819e-af89-4270-a7b9-ccbd47f62c39, 2019-10-15 20:29:10.384987, null, null, null, null).
如果我也为 2 个实体调用保存,它不会改变结果:
fileVersionEntityRepository
.save(existingFileVersion.get().addFileDeployment(fileDeploymentEntity));
fileEnvironmentEntityRepository
.save(existingFileEnvironment.get().addFileDeployment(fileDeploymentEntity));
如有任何帮助,我们将不胜感激!
对我来说,问题是我不小心用相同的 table 名称命名了另一个实体,这导致生成的架构与我认为的大不相同。
带走课程: 1) 检查有疑问时生成的模式。
var con = dataSource.getConnection();
var databaseMetaData = con.getMetaData();
ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[]{"TABLE"});
System.out.println("Printing TABLE_TYPE \"TABLE\" ");
System.out.println("----------------------------------");
while(resultSet.next())
{
//Print
System.out.println(resultSet.getString("TABLE_NAME"));
}
ResultSet columns = databaseMetaData.getColumns(null,null, "study", null);
while(columns.next())
{
String columnName = columns.getString("COLUMN_NAME");
//Printing results
System.out.println(columnName);
}