【问题标题】:How can I realize a ManyToOne relation using the CrudRepository (Spring Data and thymeleaf )?如何使用 CrudRepository (Spring Data 和 thymeleaf )实现 ManyToOne 关系?
【发布时间】:2018-05-26 03:08:47
【问题描述】:

我正在使用 Spring Boot 和 Spring Data (CrudRepository) 来保存通过表单传递的实体,但是我在 Product 和 StatutProduit 之间存在多对一关系(idStatutProduit 作为 Produit 实体中的外键),我不知道如何解决的问题对控制器说我有一个依赖于另一个对象的对象...否则我必须使用 thymeleaf 为产品类创建 w 表单,确保使用组合框加载 statusProduct。

产品类(:

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

状态产品类:

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 ) {



    }

}

【问题讨论】:

  • 我不会在每个字段上使用@Column(name = "CREE_PAR"),而是配置NamingStrategy。你在用Hibernate吗?
  • 是的,但这不是 hibernate 正常工作的问题。
  • 我知道这是题外话。但它让你的课堂更整洁,节省你的时间Configure Hibernate Naming Strategy。设置一个属性与定义@Column n-times

标签: java spring spring-mvc spring-boot thymeleaf


【解决方案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);

}

如果您以这种方式创建存储库,Spring 将创建控制器和服务以适应您的 @Entity

【讨论】:

  • 投反对票,因为 OP 专门要求 MVC 控制器,而不是 REST 控制器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 2015-05-31
  • 1970-01-01
  • 2015-06-19
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多