SpringData多对多保存和DataIntegrityViolationException

时间:2018-08-10 15:01:17

标签: spring-boot spring-data-jpa spring-data

我在保存链接数据到春季数据时遇到问题。

我将尝试简要描述我使用的模型:

@Entity
class BusinessOperation {
  @Id
  @BeanProperty
  var mnemonic: String = _

  @NotNull
  @BeanProperty
  @Column(length = 255)
  @Size(max = 255)
  var name: String = _

  @BeanProperty
  @ManyToMany(targetEntity = classOf[BusinessProcess], fetch = FetchType.EAGER, cascade = Array(CascadeType.MERGE))
  @Fetch(value = FetchMode.SUBSELECT)
  @JoinTable(name = "business_operation_process_link",
    joinColumns = Array(new JoinColumn(name = "business_operation_mnemonic")),
    inverseJoinColumns = Array(new JoinColumn(name = "business_process_mnemonic")))
  var businessProcessSet: util.Set[BusinessProcess] = new util.HashSet[BusinessProcess]()


  def canEqual(other: Any): Boolean = other match {
    case _: BusinessOperation => true
    case _                    => false
  }

  override def equals(other: Any): Boolean = other match {
    case that: BusinessOperation =>
      (that canEqual this) &&
        mnemonic == that.mnemonic &&
        name == that.name &&
        businessProcessSet == that.businessProcessSet
    case _                       => false
  }

  override def hashCode(): Int = {
    Objects.hash(mnemonic, name, businessProcessSet)
  }
}

第二个实体是:

@Entity
class BusinessStep {

  @Id
  @BeanProperty
  var mnemonic: String = _

  @NotNull
  @BeanProperty
  @Column(length = 255)
  @Size(max = 255)
  var name: String = _

  @BeanProperty
  @ManyToMany(targetEntity = classOf[BusinessOperation], fetch = FetchType.EAGER, cascade = Array(CascadeType.MERGE))
  @Fetch(value = FetchMode.SUBSELECT)
  @JoinTable(name = "business_step_operation_link",
    joinColumns = Array(new JoinColumn(name = "business_step_mnemonic")),
    inverseJoinColumns = Array(new JoinColumn(name = "business_operation_mnemonic")))
  var businessOperationSet: util.Set[BusinessOperation] = new util.HashSet[BusinessOperation]()


  @BeanProperty
  @OneToMany(targetEntity = classOf[EventDescription], mappedBy = "businessStep", fetch = FetchType.LAZY)
  @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
  var eventDescriptionSet: util.Set[EventDescription] = _

  def canEqual(other: Any): Boolean = other match {
    case _: BusinessStep => true
    case _               => false
  }

  override def equals(other: Any): Boolean = other match {
    case that: BusinessStep =>
      (that canEqual this) &&
        mnemonic == that.mnemonic &&
        name == that.name &&
        businessOperationSet == that.businessOperationSet &&
      version == that.version
    case _                  => false
  }

  override def hashCode(): Int = {
    Objects.hash(mnemonic, name, businessOperationSet)
  }
}

这是businessStep和BusinessOperation之间的多对多链接,并且此链接有一个表:

<changeSet id="Create link table for business_step to business_operation" author="MartemyanovIV">
  <createTable tableName="business_step_operation_link">
    <column name="business_operation_mnemonic" type="VARCHAR(255)">
      <constraints nullable="false" />
    </column>
    <column name="business_step_mnemonic" type="VARCHAR(255)">
      <constraints nullable="false" />
    </column>
  </createTable>
  <addForeignKeyConstraint baseTableName="business_step_operation_link"
                           baseColumnNames="business_operation_mnemonic"
                           constraintName="Business_Operation_Step_Link_FK"
                           referencedTableName="business_operation"
                           referencedColumnNames="mnemonic" />
  <addForeignKeyConstraint baseTableName="business_step_operation_link"
                           baseColumnNames="business_step_mnemonic"
                           constraintName="Business_Step_Link_FK"
                           referencedTableName="business_step"
                           referencedColumnNames="mnemonic" />
  <addPrimaryKey tableName="business_step_operation_link"
                 columnNames="business_operation_mnemonic,business_step_mnemonic"
                 constraintName="BusinessStepOperationLink_AK" />
</changeSet>

我如何添加实体: 首先,我创建BusinessProcess,将其保存到业务流程存储库中,然后创建BusinessOperation,将我的业务流程添加到businessProcessSet中,然后也保存到业务操作存储库中。 之后,我创建BusinessStep,设置先前的业务操作,然后尝试将其保存到业务步骤存储库中。

但是当我将业务步骤保存到他们的仓库中时-有时我会遇到错误:

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [BUSINESSSTEPOPERATIONLINK_AK]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

我用独特的助记符保存业务-为什么我捕获到DataIntegrityViolationException?

我听不懂。

我将使用2.0.0.RELEASE版本的spring-boot-starter-data-jpa。

很抱歉,有很多代码和文本,但是我无法解决我的问题,也不知道出了什么问题。

如果您能帮助我-我会很高兴的!

0 个答案:

没有答案