2017-08-16 50 views
0

從控制器保存父實體時出現問題。子對象不保存到數據庫中。我從短語表中得到那個「subject_id」不能爲空。我在這裏做錯了什麼,什麼是適當的修復?我是否也需要使用DTO對象來實體類,或者我的方法很好?波紋管是表格,Java實體和控制器服務。請指教 !?休眠一對多創建方法

-- parent table 
CREATE TABLE subjects 
(
subject_id    Bigint NOT NULL AUTO_INCREMENT, 
name      Varchar(250) NOT NULL, 
weight     Int, 
color      Varchar(20), 
ontology_id    Bigint NOT NULL, 
created_by    Bigint NOT NULL, 
created_at    Datetime NOT NULL, 
updated_by    Bigint, 
updated_at    Datetime, 
PRIMARY KEY    (subject_id) 
); 

-- child table 
CREATE TABLE phrases 
(
phrase_id     Bigint NOT NULL AUTO_INCREMENT, 
name      Varchar(250) NOT NULL, 
weight     Int, 
color      Varchar(20), 
subject_id    Bigint NOT NULL, 
created_by    Bigint NOT NULL, 
created_at    Datetime NOT NULL, 
updated_by    Bigint, 
updated_at    Datetime, 
PRIMARY KEY    (phrase_id) 
); 

ALTER TABLE phrases ADD CONSTRAINT phrases_subjects_fk FOREIGN KEY (subject_id) REFERENCES subjects (subject_id); 


@JsonIgnoreProperties(ignoreUnknown = true) 
@Entity 
@Table(name = "subjects") 
public class Subject implements Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "subject_id") 
    private Long subjectId; 

    @NotEmpty 
    @Size(max = 250) 
    @Column(name = "name", length = 250, nullable = false) 
    private String name; 

    @NotNull 
    @Column(name = "weight") 
    private Integer weight; 

    @Size(max = 20) 
    @Column(name = "color", length = 20) 
    private String color; 

    @NotNull 
    @Column(name = "ontology_id") 
    private Long ontologyId; 

    @OneToMany(mappedBy = "subject", targetEntity = Phrase.class, fetch = FetchType.EAGER, cascade = {CascadeType.ALL}) 
    private Set<Phrase> phrases; 

    @Column(name = "created_by") 
    private Long createdBy; 

    @JsonIgnore 
    @CreationTimestamp 
    @Column(name = "created_at") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date createdAt; 

    @Column(name = "updated_by") 
    private Long updatedBy; 

    @JsonIgnore 
    @UpdateTimestamp 
    @Column(name = "updated_at") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date updatedAt; 

    public Subject() { 
     // empty constructor 
    } 

    public Subject(@JsonProperty(value = "subjectId") Long subjectId, 
      @JsonProperty(value = "name") String name, 
      @JsonProperty(value = "weight") Integer weight, 
      @JsonProperty(value = "color") String color, 
      @JsonProperty(value = "ontologyId") Long ontologyId, 
      @JsonProperty(value = "phrases") Set<Phrase> phrases, 
      @JsonProperty(value = "createdBy") Long createdBy, 
      @JsonProperty(value = "updatedBy") Long updatedBy) { 
     super(); 
     this.subjectId = subjectId; 
     this.name = name; 
     this.weight = weight; 
     this.color = color; 
     this.ontologyId = ontologyId; 
     this.phrases = phrases; 
     this.createdBy = createdBy; 
     this.updatedBy = updatedBy; 
    } 

    public Long getSubjectId() { 
     return subjectId; 
    } 

    public void setSubjectId(Long subjectId) { 
     this.subjectId = subjectId; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public Integer getWeight() { 
     return weight; 
    } 

    public void setWeight(Integer weight) { 
     this.weight = weight; 
    } 

    public String getColor() { 
     return color; 
    } 

    public void setColor(String color) { 
     this.color = color; 
    } 

    public Long getOntologyId() { 
     return ontologyId; 
    } 

    public void setOntologyId(Long ontologyId) { 
     this.ontologyId = ontologyId; 
    } 

    public Set<Phrase> getPhrases() { 
     return phrases; 
    } 

    public void setPhrases(Set<Phrase> phrases) { 
     this.phrases = phrases; 
    } 

    public Long getCreatedBy() { 
     return createdBy; 
    } 

    public void setCreatedBy(Long createdBy) { 
     this.createdBy = createdBy; 
    } 

    public Date getCreatedAt() { 
     return createdAt; 
    } 

    public void setCreatedAt(Date createdAt) { 
     this.createdAt = createdAt; 
    } 

    public Long getUpdatedBy() { 
     return updatedBy; 
    } 

    public void setUpdatedBy(Long updatedBy) { 
     this.updatedBy = updatedBy; 
    } 

    public Date getUpdatedAt() { 
     return updatedAt; 
    } 

    public void setUpdatedAt(Date updatedAt) { 
     this.updatedAt = updatedAt; 
    } 





} 

package com.gda.wControl.domain; 

import java.io.Serializable; 
import java.util.Date; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.Table; 
import javax.persistence.Temporal; 
import javax.persistence.TemporalType; 
import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Size; 

import org.hibernate.annotations.CreationTimestamp; 
import org.hibernate.annotations.UpdateTimestamp; 
import org.hibernate.validator.constraints.NotEmpty; 

import com.fasterxml.jackson.annotation.JsonIgnore; 
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 
import com.fasterxml.jackson.annotation.JsonManagedReference; 
import com.fasterxml.jackson.annotation.JsonProperty; 

@JsonIgnoreProperties(ignoreUnknown = true) 
@Entity 
@Table(name = "phrases") 
public class Phrase implements Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = -5778951738523949512L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "phrase_id") 
    private Long phraseId; 

    @NotEmpty 
    @Size(max = 250) 
    @Column(name = "name", length = 250, nullable = false) 
    private String name; 

    @NotNull 
    @Column(name = "weight") 
    private Integer weight; 

    @Size(max = 20) 
    @Column(name = "color", length = 20) 
    private String color; 

    @ManyToOne 
    @JoinColumn(name = "subject_id", referencedColumnName = "subject_id") 
    private Subject subject; 

    @Column(name = "created_by") 
    private Long createdBy; 

    @JsonIgnore 
    @CreationTimestamp 
    @Column(name = "created_at") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date createdAt; 

    @Column(name = "updated_by") 
    private Long updatedBy; 

    @JsonIgnore 
    @UpdateTimestamp 
    @Column(name = "updated_at") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date updatedAt; 

    public Phrase() { 
     // empty constructor 
    } 

    public Phrase(@JsonProperty(value = "phraseId") Long phraseId, 
      @JsonProperty(value = "name") String name, 
      @JsonProperty(value = "weight") Integer weight, 
      @JsonProperty(value = "color") String color, 
      @JsonProperty(value = "subject") Subject subject, 
      @JsonProperty(value = "createdBy") Long createdBy, 
      @JsonProperty(value = "updatedBy") Long updatedBy) { 
     super(); 
     this.phraseId = phraseId; 
     this.name = name; 
     this.weight = weight; 
     this.color = color; 
     this.subject = subject; 
     this.createdBy = createdBy; 
     this.updatedBy = updatedBy; 
    } 

    public Long getPhraseId() { 
     return phraseId; 
    } 

    public void setPhraseId(Long phraseId) { 
     this.phraseId = phraseId; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public Integer getWeight() { 
     return weight; 
    } 

    public void setWeight(Integer weight) { 
     this.weight = weight; 
    } 

    public String getColor() { 
     return color; 
    } 

    public void setColor(String color) { 
     this.color = color; 
    } 

    public Subject getSubject() { 
     return subject; 
    } 

    public void setSubject(Subject subject) { 
     this.subject = subject; 
    } 

    public Long getCreatedBy() { 
     return createdBy; 
    } 

    public void setCreatedBy(Long createdBy) { 
     this.createdBy = createdBy; 
    } 

    public Date getCreatedAt() { 
     return createdAt; 
    } 

    public void setCreatedAt(Date createdAt) { 
     this.createdAt = createdAt; 
    } 

    public Long getUpdatedBy() { 
     return updatedBy; 
    } 

    public void setUpdatedBy(Long updatedBy) { 
     this.updatedBy = updatedBy; 
    } 

    public Date getUpdatedAt() { 
     return updatedAt; 
    } 

    public void setUpdatedAt(Date updatedAt) { 
     this.updatedAt = updatedAt; 
    } 




} 

@ApiOperation(value = "add a new subject") 
@RequestMapping(value = "/add", method = RequestMethod.POST) 
public Subject addSubject(@Valid @RequestBody Subject Subject) { 
    return subjectService.createSubject(Subject); 
} 

public Subject createSubject(Subject subject) { 

    Subject existingSubject = subjectRepository.findByNameAndOntologyId(
      subject.getName().toLowerCase(), subject.getOntologyId()); 

    if (existingSubject != null) { 
     throw new ValidationException(String.format(SUBJECT_ALREADY_EXIST, 
       subject.getName(), subject.getOntologyId())); 
    } 

    return subjectRepository.saveAndFlush(subject); 
} 

我用getter和setter更新了這個類。當我傳給@RequestBody以下JSON我得到

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列 'subject_id' 不能爲空

JSON

{ 

    "name": "string444", 
    "weight": 0, 
    "color": "string", 
    "ontologyId": 1, 
    "phrases": [ 
    {  
     "name": "string", 
     "weight": 0, 
     "color": "string", 
     "createdBy": 1 
    } 
    ], 
    "createdBy": 1 
} 
+0

問題是關係主題<>短語是雙向的,因此需要設置關係的兩側,例如,在Code中你需要做'phrase.setSubject(subject); subject.addPhrase(phrase); repo.save(subject);'如果沒有第一個語句,它將不起作用。 –

+0

你們能給我一個工作的例子嗎?我沒有按照如何使用雙向關係創建createSubject方法 –

回答

0

它看起來像短語(子)對象尚未在主題(父)對象中設置。這聽起來像一個API調用。無論誰傳遞這些信息並嘗試添加主題都不會設置客戶端對象。

我還注意到setPhrases settter在Subject實體中不可用。

請確保你有setter方法來設置子對象,並確保誰通過主題對象已設置子對象。

+0

我用getter和setter更新了類實體,並得到相同的錯誤:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列'subject_id'不能null –

+0

你知道是否在父對象中設置了子對象嗎?你能分享創建主題對象的代碼嗎? – SaAn

+0

主題作爲控制器中的參數傳遞。我將這部分代碼添加到了初始文章中 –