ConstraintViolationException无法执行语句; SQL [n / a];约束[id]

时间:2019-10-16 03:45:07

标签: postgresql hibernate jpa spring-data-jpa jpa-2.0

我有两个实体 fileVersion fileEnvironment ,它们之间存在多对多关系。我正在使用由 fileDeployment 实体建模的联结表。

联结实体:

@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()时,出现以下异常:

  

无法执行语句; SQL [n / a];约束[id]

     

org.hibernate.exception.ConstraintViolationException:无法   执行语句

     

org.postgresql.util.PSQLException:错误:列“ id”中的值为空   违反非空约束详细信息:失败行包含   (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));

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

对我来说,问题是我偶然用相同的表名命名了另一个实体,这导致生成的架构与我认为的非常不同。

上课: 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);
}