简体   繁体   English

在使用Spring JPA在两个实体之间进行多对多映射的情况下,在表中保存带有嵌入式键的行的同时获取ConstraintViolationException

[英]Getting ConstraintViolationException while saving a row with embedded key in the table with many-to-many mapping between two entities using Spring JPA

In our spring boot Restful WebService, we have two master tables with many-to-many relationship between them. 在我们的春季启动Restful WebService中,我们有两个主表,它们之间具有多对多关系。 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. 但是在事务表中,我们需要一个额外的字段(current_time)作为嵌入键的一部分,而不是两个表的主键。 Now, we've created a separate class for defining embedded primary key using @Embeddable. 现在,我们创建了一个单独的类,用于使用@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. 现在,在使用Spring JPA将一个事务行插入事务表的同时,我手动在相应实体中设置主键,并在相应存储库上调用save方法。 But It is giving me ConstraintViolationException as the current_time is going with null value even if I have manually set it. 但这给了我ConstraintViolationException,因为即使我手动设置了current_time,它的值为null。 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. 这是因为@JoinTable不包含日期,并且跳过了插入。 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) 在插入或更新期间,仅包含通过@JoinTable映射的列(映射时默认为true)。

Either include the date time column in the Project class or use association without @JoinTable . Project类中包括日期时间列,或者使用不带@JoinTable关联。

I'm editing via mobile. 我正在通过手机进行编辑。 So please ignore typos if any. 因此,请忽略错别字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM