In our spring boot Restful WebService, we have two master tables with many-to-many relationship between them. But in the transaction table, we want one extra field (current_time) as part of the embedded key other than the primary keys of the two tables. Now, we’ve created a separate class for defining embedded primary key using @Embeddable. Now, while inserting one transaction row to transaction table using Spring JPA, I am manually setting the primary keys in the corresponding entity and calling the save method on corresponding repository. But It is giving me ConstraintViolationException as the current_time is going with null value even if I have manually set it. Any help would be highly appreciated.
First Entity is as follows :
@Entity
@Table(name = "project")
public class Project {
@Id
@GenericGenerator(name = "projectid", strategy = "com.sample.upload.entity.ProjectIDGenerator")
@GeneratedValue(generator = "projectid")
@Column(name = "projectid")
private String projectID;
@Column(name = "project_name")
private String projectName;
@Column(name = "project_descr")
private String projectDesc;
@Column(name = "project_input_path")
private String projectPath;
@Column(name = "project_creation_time")
private Calendar projectCreationTime;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "project_migration", joinColumns = @JoinColumn(name = "projectid", referencedColumnName = "projectid"), inverseJoinColumns = @JoinColumn(name = "migratorid", referencedColumnName = "migratorid"))
private List<Migrator> migrators;
@Column(name = "account_name")
private String accountName;
@Column(name = "account_group")
private String accountGroup;
public String getProjectID() {
return projectID;
}
public void setProjectID(String projectID) {
this.projectID = projectID;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getAccountGroup() {
return accountGroup;
}
public void setAccountGroup(String accountGroup) {
this.accountGroup = accountGroup;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getProjectDesc() {
return projectDesc;
}
public void setProjectDesc(String projectDesc) {
this.projectDesc = projectDesc;
}
public String getProjectPath() {
return projectPath;
}
public void setProjectPath(String projectPath) {
this.projectPath = projectPath;
}
public Calendar getProjectCreationTime() {
return projectCreationTime;
}
public void setProjectCreationTime(Calendar projectCreationTime) {
this.projectCreationTime = projectCreationTime;
}
public List<Migrator> getMigrators() {
return migrators;
}
public void setMigrators(List<Migrator> migrators) {
this.migrators = migrators;
}
}
Second Entity :
@Entity
@GenericGenerator(name = "generatorName", strategy = "increment")
@Table(name = "migrator")
public class Migrator {
@Id
@GeneratedValue(generator = "generatorName")
@Column(name = "migratorid")
private String migratorId;
@Column(name = "src_tech_name")
private String srcTechName;
@Column(name = "dest_tech_name")
private String destTechName;
@Column(name = "migrator_name")
private String migratorName;
@Column(name = "migrator_type")
private String migratorType;
public String getMigratorId() {
return migratorId;
}
public void setMigratorId(String migratorId) {
this.migratorId = migratorId;
}
public String getSrcTechName() {
return srcTechName;
}
public void setSrcTechName(String srcTechName) {
this.srcTechName = srcTechName;
}
public String getDestTechName() {
return destTechName;
}
public void setDestTechName(String destTechName) {
this.destTechName = destTechName;
}
public String getMigratorName() {
return migratorName;
}
public void setMigratorName(String migratorName) {
this.migratorName = migratorName;
}
public String getMigratorType() {
return migratorType;
}
public void setMigratorType(String migratorType) {
this.migratorType = migratorType;
}
@Override
public String toString() {
return "Technology [migratorId=" + migratorId + ", srcTechName=" + srcTechName + ", destTechName="
+ destTechName + ", migratorName=" + migratorName + ", migratorType=" + migratorType + "]";
}
}
The join (transaction) table's entity :
@Entity
@Table(name = "project_migration")
public class ProjectMigration {
@EmbeddedId
private ProjectMigrationID migrationId;
@Column(name ="migration_finish_time")
private Calendar migrationFinishTime;
@Column(name ="time_in_millis_for_migration")
private long timeInMillisForMigration;
@Column(name ="migration_status")
private String migrationStatus;
@Column(name ="migrated_codebase_path")
private String migratedCodeBasePath;
The embedded Primary Key class is as follows:
@Embeddable
public class ProjectMigrationID implements Serializable {
private static final long serialVersionUID = -3623993529011381924L;
@Column(name = "projectid")
private String projectId;
@Column(name = "migratorid")
private String migratorId;
@Column(name = "migration_start_time")
private Calendar migrationStartTime;
public ProjectMigrationID() {
}
public ProjectMigrationID(String projectId, String migratorId, Calendar migrationStartTime) {
this.projectId = projectId;
this.migratorId = migratorId;
this.migrationStartTime = migrationStartTime;
}
The snippet from service Class :
for (String migratorId : data.getMigratorIds()) {
Migrator migrator = migratorRepository.findByMigratorId(migratorId);
migrators.add(migrator);
}
if (projectId != null) {
project = projectRepository.findByProjectID(projectId);
System.out.println(project==null);
project.setMigrators(migrators);
System.out.println("I am here");
if (project != null) {
//project.setMigrationStatus("In Progress");
ProjectMigrationID pmId = new ProjectMigrationID();
pmId.setProjectId(project.getProjectID());
pmId.setMigratorId(project.getMigrators().get(0).getMigratorId());
pmId.setMigrationStartTime(new GregorianCalendar());
ProjectMigration pm = new ProjectMigration();
pm.setMigrationId(pmId);
pm.setMigrationStatus("Pending");
projectMigrationRepository.save(pm);
That's because of the @JoinTable
where the date is not included and it skips the insertion. If you include a column with all the primary keys needed, it will work as expected.
Only the columns mapped via @JoinTable
will be included during insertion or update (defaults to true when mapped)
Either include the date time column in the Project
class or use association without @JoinTable
.
I'm editing via mobile. So please ignore typos if any.