2016-09-26 9 views
0

誰かが間違っていることを教えてもらえますか?org.hibernate.exception.ConstraintViolationException:列 'pacientId'をnullにすることはできません

enter image description here

とエンティティ:

@Entity 
@NamedQuery(name = "Pacients.findAll", query = "SELECT p FROM Pacients p") 
public class Pacients implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private int pacientId; 

    private String name; 

    private String surname; 

    // bi-directional many-to-one association to DetaliiPacient 
    @OneToOne(mappedBy = "pacient") 
    @Cascade(value = { CascadeType.ALL}) 
    private PacientsDetails pacientDetail; 

} 

@Entity 
@Table 
@NamedQuery(name = "PacientsDetails.findAll", query = "SELECT d FROM PacientsDetails d") 
public class PacientsDetails implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private int pacientDetailsId; 

    private String address; 

    @OneToOne 
    @JoinColumn(name = "pacientId",referencedColumnName="pacientId") 
    @Cascade(value = { CascadeType.ALL}) 
    @JsonBackReference 
    private Pacients pacient; 
} 

"pacients" テーブルからフィールド "pacientId" はAUTO_INCREMENTであると私はmysqlデータベースを使用している私は、次のテーブルを持っています。ログに

sessionFactory.getCurrentSession().persist(pacient); 

私はHibernateがやっていることを参照してください:私がやっているとき

タイトルからエラーが表示されます

Hibernate: insert into Pacients (doctorId, name, surname) values (?, ?, ?) 
Hibernate: insert into PacientsDetails (address, pacientId) values (?, ?) 

が、問題は事実である、第2のクエリでpacientId(最初のクエリによって生成された新しいもの)の値は認識されません。

誰かが私に行方不明を教えてもらえますか? ありがとうございます!

+0

まず(医師のクライアント)のようになります。第2:2つのエンティティを保存する場所にコードを投稿してください。 – Alexander

+0

あなたの答えをありがとう。最初に私はルーマニアの言語でエンティティを持っていました。英語を使用するためのリファクタリングを行った後、私は "Pacient"エンティティを忘れてしまった:とにかく技術的な観点から私は以下のように問題を解決した。 – Aditzu

答えて

1

私はあなたがこのようなあなたのエンティティを作成していると仮定します(私はフィールドがプライベートであることを知っているが、さんはそうでないふりをしましょう)

pacient = new Pacients(); 
// set name & surname 
pacient.pacientDetail = new PacientsDetails(); 
// set address 

その場合は、あなたのmappedByによると、pacientsDetailsは関係をマッピングしているため、同様pacientsDetailsでpacientsフィールドを設定する必要が

@OneToOne(mappedBy = "pacient") 

ので、「患者」ではなく、「pacient」書かれている全ての患者のこの

pacient = new Pacients(); 
// set name & surname 
pacient.pacientDetail = new PacientsDetails(); 
// set address 
pacient.pacientDetail.pacient = pacient; 
+0

私の入力はjsonから自動的にマップされます。とにかく、私は正しいトラックに私を入れているので、正しいものとしてあなたの答えをマークしました。私は以下の方法でセッターを作成しなければなりませんでした(filac "pacient"を "pacientDetail"から設定することによって):\t public void setPacientDetail(PacientsDetails pacientDetail){ \t \t this.pacientDetail = pacientDetail; \t \t this.pacientDetail.setPacient(this); \t} – Aditzu

+0

多くのありがとう@SergeiBednar! – Aditzu

0

が持続した後)(フラッシュ行う()

sessionFactory.getCurrentSession().persist(patient); 
sessionFactory.getCurrentSession().flush(); 
... 
patientDetails.setPatientId(patient.getId()); 
... 
sessionFactory.getCurrentSession().persist(patientDetails); 
+0

ありがとう、問題は@SergeiBednarが提案したものでした。 – Aditzu

関連する問題