2017-12-12 6 views
0

私は春のブートとバネのデータ(CrudRepository)を使ってフォームで渡すエンティティを永続化していますが、ProductとStatutProduit(Produitエンティティの外部キーとしてのidStatutProduit)の間にManyToOneの関係があります。他のオブジェクトに依存するオブジェクトを持っていることをコントローラーに伝えてください。そうでなければ、statusProductをロードするコンボボックスを使って、確実に製品クラスのためにthymeleafを使ってwフォームを作成する必要があります。CrudRepository(Spring Dataとthymeleaf)を使用してManyToOne関係を実現するにはどうすればよいですか?

Produitクラス(:

public class Produits implements Serializable { 
    private static final long serialVersionUID = 1L; 
    // @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation 
    @Id 
    @Basic(optional = false) 
    @NotNull 
    @Column(name = "ID_PRODUIT") 
    private BigDecimal idProduit; 
    @Column(name = "ID_OPERATEUR") 
    private BigInteger idOperateur; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 45) 
    @Column(name = "CODE") 
    private String code; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 45) 
    @Column(name = "LIBELLE") 
    private String libelle; 
    @Column(name = "POIDS") 
    private BigInteger poids; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 5) 
    @Column(name = "INDICE") 
    private String indice; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 10) 
    @Column(name = "CREE_PAR") 
    private String creePar; 
    @Column(name = "DATE_CREATION") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date dateCreation; 
    @Size(max = 10) 
    @Column(name = "MAJ_PAR") 
    private String majPar; 
    @Column(name = "DATE_MAJ") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date dateMaj; 
    @JoinColumn(name = "ID_STATUT_PRODUIT", referencedColumnName = "ID_STATUT_PRODUIT") 
    @ManyToOne(optional = false) 
    private StatutProduits idStatutProduit; 
    public Produits(BigDecimal idProduit, String code, String libelle, 
    String indice, String creePar) { 
    this.idProduit = idProduit; 
    this.code = code; 
    this.libelle = libelle; 
    this.indice = indice; 
    this.creePar = creePar; 
} 

StatusProductクラス:

public class StatutProduits implements Serializable { 
    private static final long serialVersionUID = 1L; 
    fields consider using these annotations to enforce field validation 
    @Id 
    @Basic(optional = false) 
    @NotNull 
    @Column(name = "ID_STATUT_PRODUIT") 
    private BigDecimal idStatutProduit; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 45) 
    @Column(name = "CODE") 
    private String code; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 45) 
    @Column(name = "LIBELLE") 
    private String libelle; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 10) 
    @Column(name = "CREE_PAR") 
    private String creePar; 
    @DateTimeFormat(pattern = "yyyy-MM-dd") 
    @Column(name = "DATE_CREATION") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date dateCreation; 
    @Size(max = 10) 
    @Column(name = "MAJ_PAR") 
    private String majPar; 
    @DateTimeFormat(pattern = "yyyy-MM-dd") 
    @Column(name = "DATE_MAJ") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date dateMaj; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idStatutProduit") 
    private List<Produits> produitsList; 

    public StatutProduits() { 
    } 

    public StatutProduits(BigDecimal idStatutProduit) { 
     this.idStatutProduit = idStatutProduit; 
    } 

    public StatutProduits(BigDecimal idStatutProduit, String code, String libelle, String creePar) { 
     this.idStatutProduit = idStatutProduit; 
     this.code = code; 
     this.libelle = libelle; 
     this.creePar = creePar; 
    } 

    public BigDecimal getIdStatutProduit() { 
     return idStatutProduit; 
    } 

    public void setIdStatutProduit(BigDecimal idStatutProduit) { 
     this.idStatutProduit = idStatutProduit; 
    } 

    public String getCode() { 
     return code; 
    } 

    public void setCode(String code) { 
     this.code = code; 
    } 

    public String getLibelle() { 
     return libelle; 
    } 

    public void setLibelle(String libelle) { 
     this.libelle = libelle; 
    } 

    public String getCreePar() { 
     return creePar; 
    } 

    public void setCreePar(String creePar) { 
     this.creePar = creePar; 
    } 

    public Date getDateCreation() { 
     return dateCreation; 
    } 

    public void setDateCreation(Date dateCreation) { 
     this.dateCreation = dateCreation; 
    } 

    public String getMajPar() { 
     return majPar; 
    } 

    public void setMajPar(String majPar) { 
     this.majPar = majPar; 
    } 

    public Date getDateMaj() { 
     return dateMaj; 
    } 

    public void setDateMaj(Date dateMaj) { 
     this.dateMaj = dateMaj; 
    } 

    public List<Produits> getProduitsList() { 
     return produitsList; 
    } 

    public void setProduitsList(List<Produits> produitsList) { 
     this.produitsList = produitsList; 
    } 

ProduitServiceクラス:

@Service 
public class ProduitService { 

    @Autowired 
    private ProduitRepository produitrepository ; 

    public void addProduit(Produits p) { 



    } 

} 
+0

は'すべてのフィールドに、私は 'NamingStrategy'を構成します。 'Hibernate'を使っていますか? –

+0

はい、これは、休止状態が問題なく動作している問題ではありません。 –

+1

私はそれが話題ではないことを知っています。しかし、それは(https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/html/howto-data-access.html# [設定Hibernateは戦略を命名]あなたのクラスの多くは整然となり、時間を節約できますhowto-configure-hibernate-naming-strategy)を参照してください。 は 'COLUMN' @ n回 –

答えて

-1

は、このページhttps://spring.io/guides/gs/accessing-data-rest/を見てみましょうここでは、この作品を見ることができます。コード:

package hello; 

import java.util.List; 

import org.springframework.data.repository.PagingAndSortingRepository; 
import org.springframework.data.repository.query.Param; 
import org.springframework.data.rest.core.annotation.RepositoryRestResource; 

@RepositoryRestResource(collectionResourceRel = "people", path = "people") 
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> { 

    List<Person> findByLastName(@Param("name") String name); 

} 

リポジトリを作成する場合はこの方法の春は、あなたの@Entityに合わせてコントローラおよびサービスを作成します。代わりに、 `@Column(名前= "CREE_PAR")を使用しての

+0

を定める対1つのプロパティを設定し、私は残りの部分で作業していないよ場合は? –

+0

OPが特にRESTコントローラではなくMVCコントローラを要求したため、Downvoted。 – Synch

関連する問題