Cheng
Cheng

Reputation: 770

ConstraintViolationException could not execute statement; SQL [n/a]; constraint [id]

I have two entities, fileVersion and fileEnvironment, which have a many to many relationship. I'm using a junction table, modeled by fileDeployment entity.

The junction entity:

@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;
}

It's composite key:

@Embeddable
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder(toBuilder = true)
public class FileDeploymentKey implements Serializable {

  @Column
  private UUID fileVersionId;

  @Column
  private UUID fileEnvironmentId;
}

Its JPA repository:

@Repository
public interface FileDeploymentEntityRepository extends
    JpaRepository<FileDeploymentEntity, FileDeploymentKey>,
    JpaSpecificationExecutor<FileDeploymentEntity> {
}

The two entities for which the junction entity is capturing the many-to-many relationship for:

@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 is the other

@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;
}

The following code executes fine:

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);

But when eventually fileDeploymentEntityRepository.flush() is called I get the following exception:

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).

If I also call save for the 2 entities it doesn't change the result:

fileVersionEntityRepository
    .save(existingFileVersion.get().addFileDeployment(fileDeploymentEntity));
fileEnvironmentEntityRepository
    .save(existingFileEnvironment.get().addFileDeployment(fileDeploymentEntity));

Any help would be greatly appreciated!

Upvotes: 0

Views: 2261

Answers (1)

Cheng
Cheng

Reputation: 770

For me the issue was that I named another entity with the same table name by accident which caused the schema that was generated to be very different from what I thought it was.

Take away lesson: 1) Check the schema that is generated when in doubt.

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);
} 

Upvotes: 1

Related Questions