2017-01-22 9 views
0

hibernateメソッドを使用してクライアントにコマンドを追加する際に問題があります。 私はクライアントを登録して、私は次のエラーを取得コマンド:ここ非ヌルプロパティは、Hibernateの双方向関係でヌルまたは一時値を参照します

The picture error getting

が私のコードです:

クライアントモデル

package ma.commande.model; 



import java.io.Serializable; 
import java.util.*; 

import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.EmbeddedId; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 


import com.fasterxml.jackson.annotation.JsonIgnore; 




@Entity 
@Table(name = "client") 
public class Client implements Serializable { 

    @Id 
    @GeneratedValue 
    @Column(name = "id_client",nullable=false) 
    private Integer id_client; 

    @Column(name = "nomComplet") 
    private String nomComplet; 

    @Column(name = "email") 
    private String email; 

    @Column(name = "tel") 
    private String tel; 

    @Column(name = "sexe") 
    private String sexe; 

    @JsonIgnore 
    @OneToMany(cascade = CascadeType.ALL,mappedBy="client") 
    private Set<Commande> commandes = new HashSet(); 



    public Client() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    public Client(String nomComplet, String email, String tel, String sexe) { 
     super(); 
     this.nomComplet = nomComplet; 
     this.email = email; 
     this.tel = tel; 
     this.sexe = sexe; 
    } 

    public Integer getId_client() { 
     return id_client; 
    } 

    public void setId_client(Integer id_client) { 
     this.id_client = id_client; 
    } 

    public String getNomComplet() { 
     return nomComplet; 
    } 

    public void setNomComplet(String nomComplet) { 
     this.nomComplet = nomComplet; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getTel() { 
     return tel; 
    } 

    public void setTel(String tel) { 
     this.tel = tel; 
    } 

    public String getSexe() { 
     return sexe; 
    } 

    public void setSexe(String sexe) { 
     this.sexe = sexe; 
    } 



    public Set<Commande> getCommandes() { 
     return commandes; 
    } 

    public void setCommandes(Set<Commande> commandes) { 
     this.commandes = commandes; 
    } 



    public void addCommande(Commande commande){ 
     commandes.add(commande); 
     commande.setClient(this); 
    } 



} 

Commandeモデル

package ma.commande.model; 



import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.EmbeddedId; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
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.validation.constraints.NotNull; 




@Entity 
@Table(name = "commande") 
public class Commande { 

    @Id 
    @GeneratedValue 
    @Column(name = "id_Commande",nullable=false) 
    private Integer id_Commande; 
    @Column(name = "dateComm") 
    private String dateComm; 
    @Column(name = "dateLiv") 
    private String dateLiv; 
    @Column(name = "adresse") 
    private String adresse; 
    @Column(name = "statutCommande") 
    private String statutCommande; 
    @ManyToOne 
    @JoinColumn(name="id_client",nullable = false,columnDefinition="Integer") 
    private Client client; 


    public Client getClient() { 
     return client; 
    } 

    public void setClient(Client client) { 
     this.client = client; 

    } 



    public Commande() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    public Commande(String statutCommande, String dateComm, String dateLiv, String adresse) { 
     super(); 
     this.dateComm = dateComm; 
     this.dateLiv = dateLiv; 
     this.adresse = adresse; 
     this.statutCommande = statutCommande; 
    } 





    public int getId_Commande() { 
     return id_Commande; 
    } 

    public void setId_Commande(Integer id_Commande) { 
     this.id_Commande = id_Commande; 
    } 

    public String getDateComm() { 
     return dateComm; 
    } 

    public void setDateComm(String dateComm) { 
     this.dateComm = dateComm; 
    } 

    public String getDateLiv() { 
     return dateLiv; 
    } 

    public void setDateLiv(String dateLiv) { 
     this.dateLiv = dateLiv; 
    } 

    public String getAdresse() { 
     return adresse; 
    } 

    public void setAdresse(String adresse) { 
     this.adresse = adresse; 
    } 

    public String getStatutCommande() { 
     return statutCommande; 
    } 

    public void setStatutCommande(String statutCommande) { 
     this.statutCommande = statutCommande; 
    } 



} 

ClientDAO

package ma.commande.dao; 

import java.util.HashSet; 
import java.util.List; 
import java.util.Set; 

import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.AnnotationConfiguration; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 

import ma.commande.model.Client; 
import ma.commande.model.Commande; 


@Repository 

    public class ClientDAO implements IClientDAO { 

     @Override 
     public void addClient(Client client) { 
      Commande commande = new Commande(); 
      Set<Commande> commandes = new HashSet<Commande>() ; 
      commandes.add(commande); 
      client.setCommandes(commandes); 
      commande.setClient(client); 
      getCurrentSession().persist(client); 

     } 
    } 

CommandeDAO

package ma.commande.dao; 


import java.util.List; 


import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.AnnotationConfiguration; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 

import ma.commande.model.Client; 
import ma.commande.model.Commande; 

@Repository 
public class CommandeDAO implements ICommandeDAO { 



    @Override 
    public void addCommande(Commande commande) { 
     Client client= new Client(); 
     client.addCommande(commande); 
     getCurrentSession().persist(commande); 
    } 

dataPersist

package ma.commande.alimentationData; 

import java.util.List; 


import ma.commande.model.Client; 
import ma.commande.model.Commande; 
import ma.commande.service.ClientService; 
import ma.commande.service.CommandeService; 

public class GestionCommandeExemple { 
    public static void main(String[] args) { 

     ClientService clientService = new ClientService(); 

     /**** Insertion des clients ********/ 
     Client client1 = new Client("client1", "[email protected]", "060000000", "H"); 
     Client client2 = new Client("client2", "[email protected]", "060000000", "F"); 
     Client client3 = new Client("client3", "[email protected]", "060000000", "F"); 

     System.out.println("*** Persist - start ***"); 
     clientService.addClient(client1); 
     clientService.addClient(client2); 
     clientService.addClient(client3); 
     List<Client> clients1 = clientService.findAllClients(); 
     System.out.println("Clients Persisted are :"); 
     for (Client c : clients1) { 
      System.out.println("-" + c.toString()); 
     } 
     System.out.println("*** Persist - end ***"); 

     CommandeService commandeService = new CommandeService(); 


     /**** Insertion des commandes ********/ 
     Commande commande1 = new Commande("15/07/2016", "16/07/2016", "25 rue 1","Annulee"); 
     Commande commande2 = new Commande("15/07/2016", "16/07/2016", "25 rue 1","En Cours"); 
     Commande commande3 = new Commande("15/07/2016", "16/07/2016", "25 rue 1","Livree"); 

     System.out.println("*** addCommande - start ***"); 

     commandeService.addCommande(commande1); 
     commandeService.addCommande(commande2); 
     commandeService.addCommande(commande3); 
     List<Commande> commandes1 = commandeService.findAllCommandes(); 
     System.out.println("commandes addCommanded are :"); 
     for (Commande c : commandes1) { 
      System.out.println("-" + c.toString()); 
     } 
     System.out.println("*** addCommande - end ***"); 




     System.exit(0); 
    } 
} 

ClientController

@RequestMapping(value = "/client/", method = RequestMethod.POST) 
    public ResponseEntity<Void> addClient(@RequestBody Client client, UriComponentsBuilder builder) { 
     clientService.addClient(client); 
     HttpHeaders headers = new HttpHeaders(); 
     headers.setLocation(builder.path("/client/{id}").buildAndExpand(client.getId_client()).toUri()); 
     return new ResponseEntity<Void>(headers, HttpStatus.CREATED); 
    } 

CommandeController

@RequestMapping(value = "/commande/", method = RequestMethod.POST) 
    public ResponseEntity<Void> addCommande(@RequestBody Commande commande,UriComponentsBuilder builder) { 
     commandeService.addCommande(commande); 
     HttpHeaders headers = new HttpHeaders(); 
     headers.setLocation(builder.path("/commande/{id}").buildAndExpand(commande.getId_Commande()).toUri()); 
     return new ResponseEntity<Void>(headers, HttpStatus.CREATED); 
    } 

答えて

0

あなたはヨーヨーはクライアントなしCommadeを持続しようとしています。 永続化する前にクライアントにコマンドを設定します。

... 
commande1.setClient(client1); 
commandeService.addCommande(commande1); 
... 
+0

私はSpring MVCコントローラを使用するとエラーが再び表示されます:/ –

+0

CommandeController?同じエラーの場合は、コントローラを呼び出す前にCommandeにクライアントを設定してみてください。 – jonhid

+0

どのように私はコントローラでこれを行うことができます、私を助けてください –