2017-12-11 17 views
1

데코레이터에 캐스케이드 = CascadeType.ALL을 추가하려고합니다. 스프링에서 내 버전 modele을 여러 필드에 하나씩 추가하려고합니다. 모든 하이퍼 매개 변수를 업데이트 할 때마다 업데이트합니다. 아래에서 볼 수 있습니다. 내가 JSON을 업데이트하고 2에서 필드가 parameterValue의 값을 변경 .I 그것과 풋을 시도hibernate/Spring/Jpa @oneToMany 캐스케이드 업데이트

@Entity 
@Table(name = "hyper_parameter") 
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
public class HyperParameter implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id; 

    @NotNull 
    @Column(name = "parameter_value", nullable = false) 
    private String parameterValue; 

    @ManyToOne(optional = false) 
    @NotNull 
    @JsonIgnoreProperties({"resultExecutions", "hyperParameters", "data", "modelConfiguration"}) 
    private Version version; 
public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 

public String getParameterValue() { 
    return parameterValue; 
} 

public HyperParameter parameterValue(String parameterValue) { 
    this.parameterValue = parameterValue; 
    return this; 
} 

public void setParameterValue(String parameterValue) { 
    this.parameterValue = parameterValue; 
} 

public Version getVersion() { 
    return version; 
} 

public HyperParameter version(Version version) { 
    this.version = version; 
    return this; 
} 

public void setVersion(Version version) { 
    this.version = version; 
} 

public HyperParameterType getHyperParameterType() { 
    return hyperParameterType; 
} 

public HyperParameter hyperParameterType(HyperParameterType hyperParameterType) { 
    this.hyperParameterType = hyperParameterType; 
    return this; 
} 

public void setHyperParameterType(HyperParameterType hyperParameterType) { 
    this.hyperParameterType = hyperParameterType; 
} 
// jhipster-needle-entity-add-getters-setters - Jhipster will add getters and setters here, do not remove 

@Override 
public boolean equals(Object o) { 
    if (this == o) { 
     return true; 
    } 
    if (o == null || getClass() != o.getClass()) { 
     return false; 
    } 
    HyperParameter hyperParameter = (HyperParameter) o; 
    if (hyperParameter.getId() == null || getId() == null) { 
     return false; 
    } 
    return Objects.equals(getId(), hyperParameter.getId()); 
} 

@Override 
public int hashCode() { 
    return Objects.hashCode(getId()); 
} 

@Override 
public String toString() { 
    return "HyperParameter{" + 
     "id=" + getId() + 
     ", parameterValue='" + getParameterValue() + "'" + 
     "}"; 
} 

}

:

@Entity 
    @Table(name = "version") 
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
    public class Version implements Serializable { 

     private static final long serialVersionUID = 1L; 

     @Id 
     @GeneratedValue(strategy = GenerationType.IDENTITY) 
     private Long id; 

     @NotNull 
     @Column(name = "num", nullable = false) 
     private Integer num; 

     @Column(name = "creation_date") 
     private ZonedDateTime creationDate; 

     @Column(name = "execution_date") 
     private ZonedDateTime executionDate; 

     @Column(name = "weights_uri") 
     private String weightsURI; 

     @OneToMany(mappedBy = "version", fetch = FetchType.EAGER, orphanRemoval = true) 
     @JsonIgnoreProperties({"version", "metricsType"}) 
     @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
     private Set<ResultExecution> resultExecutions = new HashSet<>(); 

     @OneToMany(mappedBy = "version", fetch = FetchType.EAGER, orphanRemoval = true, cascade = CascadeType.ALL) 
     @JsonIgnoreProperties({"version"}) 
     @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
     private Set<HyperParameter> hyperParameters = new HashSet<>(); 
    public Long getId() { 
     return id; 
    } 

     public void setId(Long id) { 
      this.id = id; 
     } 

     public Integer getNum() { 
      return num; 
     } 

     public Version num(Integer num) { 
      this.num = num; 
      return this; 
     } 

     public void setNum(Integer num) { 
      this.num = num; 
     } 

     public ZonedDateTime getCreationDate() { 
      return creationDate; 
     } 

     public Version creationDate(ZonedDateTime creationDate) { 
      this.creationDate = creationDate; 
      return this; 
     } 

     public void setCreationDate(ZonedDateTime creationDate) { 
      this.creationDate = creationDate; 
     } 

     public ZonedDateTime getExecutionDate() { 
      return executionDate; 
     } 

     public Version executionDate(ZonedDateTime executionDate) { 
      this.executionDate = executionDate; 
      return this; 
     } 

     public void setExecutionDate(ZonedDateTime executionDate) { 
      this.executionDate = executionDate; 
     } 

     public String getWeightsURI() { 
      return weightsURI; 
     } 

     public Version weightsURI(String weightsURI) { 
      this.weightsURI = weightsURI; 
      return this; 
     } 

     public void setWeightsURI(String weightsURI) { 
      this.weightsURI = weightsURI; 
     } 

     public Set<ResultExecution> getResultExecutions() { 
      return resultExecutions; 
     } 

     public Version resultExecutions(Set<ResultExecution> resultExecutions) { 
      this.resultExecutions = resultExecutions; 
      return this; 
     } 

     public Version addResultExecution(ResultExecution resultExecution) { 
      this.resultExecutions.add(resultExecution); 
      resultExecution.setVersion(this); 
      return this; 
     } 

     public Version removeResultExecution(ResultExecution resultExecution) { 
      this.resultExecutions.remove(resultExecution); 
      resultExecution.setVersion(null); 
      return this; 
     } 

     public void setResultExecutions(Set<ResultExecution> resultExecutions) { 
      this.resultExecutions = resultExecutions; 
     } 

     public Set<HyperParameter> getHyperParameters() { 
      return hyperParameters; 
     } 

     public Version hyperParameters(Set<HyperParameter> hyperParameters) { 
      this.hyperParameters = hyperParameters; 
      return this; 
     } 

     public Version addHyperParameter(HyperParameter hyperParameter) { 
      this.hyperParameters.add(hyperParameter); 
      hyperParameter.setVersion(this); 
      return this; 
     } 

     public Version removeHyperParameter(HyperParameter hyperParameter) { 
      this.hyperParameters.remove(hyperParameter); 
      hyperParameter.setVersion(null); 
      return this; 
     } 

     public void setHyperParameters(Set<HyperParameter> hyperParameters) { 
      this.hyperParameters = hyperParameters; 
     } 

     public Set<Data> getData() { 
      return data; 
     } 

     public Version data(Set<Data> data) { 
      this.data = data; 
      return this; 
     } 

     public Version addData(Data data) { 
      this.data.add(data); 
      data.getVersions().add(this); 
      return this; 
     } 

     public Version removeData(Data data) { 
      this.data.remove(data); 
      data.getVersions().remove(this); 
      return this; 
     } 

     public void setData(Set<Data> data) { 
      this.data = data; 
     } 

     public ModelConfiguration getModelConfiguration() { 
      return modelConfiguration; 
     } 

     public Version modelConfiguration(ModelConfiguration modelConfiguration) { 
      this.modelConfiguration = modelConfiguration; 
      return this; 
     } 

     public void setModelConfiguration(ModelConfiguration modelConfiguration) { 
      this.modelConfiguration = modelConfiguration; 
     } 
     // jhipster-needle-entity-add-getters-setters - Jhipster will add getters and setters here, do not remove 

     @Override 
     public boolean equals(Object o) { 
      if (this == o) { 
       return true; 
      } 
      if (o == null || getClass() != o.getClass()) { 
       return false; 
      } 
      Version version = (Version) o; 
      if (version.getId() == null || getId() == null) { 
       return false; 
      } 
      return Objects.equals(getId(), version.getId()); 
     } 

     @Override 
     public int hashCode() { 
      return Objects.hashCode(getId()); 
     } 

     @Override 
     public String toString() { 
      return "Version{" + 
       "id=" + getId() + 
       ", num='" + getNum() + "'" + 
       ", creationDate='" + getCreationDate() + "'" + 
       ", executionDate='" + getExecutionDate() + "'" + 
       ", weightsURI='" + getWeightsURI() + "'" + 
       "}"; 
     } 
    } 

내 hyperParameter 모델은 다음과 같습니다 삼 .

{ 
    "id": 1, 
    "num": 1, 
    "creationDate": "2017-05-11T00:00:00+02:00", 
    "executionDate": null, 
    "weightsURI": "tests/scripts/sequential/weights/weights_le_net_5.h5py", 
    "resultExecutions": [ 
     { 
     "id": 1, 
     "metricValues": "", 
     "executionType": "TRAIN", 
     "numPrediction": null 
     }, 
     { 
     "id": 2, 
     "metricValues": "", 
     "executionType": "TRAIN", 
     "numPrediction": null 
     } 
    ], 
    "hyperParameters": [ 
     { 
     "id": 1, 
     "parameterValue": "2", 
     "hyperParameterType": { 
      "id": 1, 
      "name": "epochs", 
      "parameterType": "INTEGER", 
      "parameterDefaultValue": "0", 
      "isRequired": true 
     } 
     }, 
     { 
     "id": 2, 
     "parameterValue": "32", 
     "hyperParameterType": { 
      "id": 2, 
      "name": "batch_size", 
      "parameterType": "INTEGER", 
      "parameterDefaultValue": "32", 
      "isRequired": true 
     } 
     } 
    ], 
    "modelConfiguration": { 
     "id": 1, 
     "name": "Modele LeNet5", 
     "creationDate": "2017-05-11T00:00:00+02:00", 
     "updateDate": "2017-05-11T00:00:00+02:00", 
     "saveURI": "tests/scripts/sequential/models/le_net_5.json" 
    } 
    } 

하지만 나는 500 회의 내부 서버 오류가 발생하고 null 제약 조건 위반과 관련된 하나의 추적 추적을 수행합니다.

<!-- What do you expect the result to be? --> 

<!-- What is the actual result you get? (Please include any errors.) --> 
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction 
     at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:526) 


[1:59] 
t org.hibernate.internal.ExceptionConverterImpl.convertCommitException(ExceptionConverterImpl.java:75) 
     at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:71) 
     at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517) 
     ... 153 common frames omitted 
Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [com.saagie.picsaagie2017_frontend.domain.HyperParameter] during update time for groups [javax.validation.groups.Default, ] 
List of constraint violations:[ 
     ConstraintViolationImpl{interpolatedMessage='can not be null ', propertyPath=version, rootBeanClass=class com.saagie.picsaagie2017_frontend.domain.HyperParameter, messageTemplate='{javax.validation.constraints.NotNull.message}'} 
] 

이 오류가 발생하여 버전을 업데이트 할 때 어떻게 내 hyperParameters를 업데이트 할 수 있습니까?

+0

데이터베이스 스키마를 게시하지 않았습니다. – Saheed

+0

또한 업데이트를 수행하는 코드를 추가하십시오. 전체 트랜잭션 메소드가 가장 좋을 것입니다. –

+0

@MaciejKowalski 방금 필요한 설정 도구로 게시물을 업데이트했습니다. –

답변

1

사용 @PreUpdate 등 같은 HyperParameter 클래스의 @PrePersist 콜백 :

@PreUpdate 
@PrePersist 
public void setChildObjects() { 
    if (getVersions() != null) 
    { 
     for (Version version : getVersions()) 
     { 
      version.setHyperParameters (this); 
     } 
    } 
} 

자세한 내용은이 참조 : http://docs.jboss.org/hibernate/core/3.3/reference/en/html/tutorial.html#tutorial-associations-usingbidir

+0

나는 당신이 내가 다른 방법으로 썼던 onetoMany 관계의 방향을 잘못 생각했다고 생각합니다. 그리고 작동하지 않는 것 같습니다.이 오류가 있습니다. org.springframework.dao.DataIntegrityViolationException : 명령문을 실행할 수 없습니다. SQL [n/a]; 제약 조건 [null]; nested exception은 org.hibernate.exception.ConstraintViolationException : org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException에서 문장 을 실행할 수 없습니다 (HibernateJpaDialect.java:278) –

0

당신은 유효성 검사 오류가 있습니다. null 값을 사용하여 interpolatedMessage를 넣으려고합니다. Ok. 그러나이 필드에서 @NotNull 어노테이션을 먼저 제거하십시오.

1

두 솔루션 모두 실제로 해당 항목에서 설명한 내용 때문에 두 번째 오류가 발생했습니다 : Spring data rest one to many cascade all. 난 단지 @JsonManagedReference와 @JsonBackReference를 추가해야했는데 왜냐하면 나는 계단식으로 내 HyperParameters를 업데이트하려고 할 때 버전의 참조에서 마샬링 문제를 가졌기 때문이다.