2017-08-16 9 views
0

コントローラから親エンティティを保存するときに問題があります。子オブジェクトはデータベースに保存されません。私はフレーズテーブルからその "subject_id"を取得することはできません。私がここで間違っていて、適切な修正は何ですか?また、エンティティクラスにDTOオブジェクトを使用する必要がありますか、私のアプローチは良いですか?ベローはテーブル、Javaエンティティ、コントローラサービスです。ご意見をお聞かせください !?Hibernate One To Manyメソッドを作成する

-- 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); 
} 

ゲッターとセッターでクラスを更新しました。それが見えます

JSON

{ 

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

問題はSubject <>フレーズが双方向であるため、関係の両側を設定する必要があります。コードでは 'phrase.setSubject(subject);を実行する必要があります。 subject.addPhrase(phrase); repo.save(subject); '最初のステートメントがなければ動作しません。 –

+0

あなたは私に実例を教えてくれますか?私は双方向の関係を持つcreateSubjectメソッドを作る方法に従っていません –

答えて

0

nullにすることはできません列 'subject_id':私は、次のJSONを@RequestBodyに渡すとき、私は

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException取得しますフレーズ(子)オブジェクトのように、サブジェクト(親)オブジェクトに設定されていません。それはAPIコールのように聞こえる。この情報を渡していて、サブジェクトを追加しようとしている人は、クライアントオブジェクトを設定していません。

私は、setPhrasesセッターがSubjectエンティティで使用できないことにも気付きました。

子オブジェクトを設定する設定メソッドがあることを確認し、対象オブジェクトを渡している人が誰でも子オブジェクトを設定していることを確認してください。

+0

gettersとsettersでクラスエンティティを更新し、同じエラーを取得しました:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 'subject_id'列は使用できませんnull –

+0

親オブジェクトに子オブジェクトが設定されているかどうかは分かりますか?対象オブジェクトが作成された場所でコードを共有できますか? – SaAn

+0

件名はコントローラのパラメータとして渡されます。私は最初の投稿にその部分を追加しました –