gpt4 book ai didi

java - Spring框架关系映射

转载 作者:行者123 更新时间:2023-11-30 00:55:54 25 4
gpt4 key购买 nike

我的类类别需要有多个产品,当您单击jsp中的类别时,我需要显示产品...我尝试了这样的操作:

@Entity
public class Categorie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int categorieId;
private String categorieName;
@OneToMany()
@JoinColumn(name = "CategorieNr")
private Set<Product> products;

我的产品类别:

@Entity
@Table(name = "CentricProduct")
public class Product implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int productId;
private int calories, productRow;
private String description;
private double price;

@Override
public boolean equals(Object object) {
if (!(object instanceof Product)) {
return false;
}

Product other = (Product) object;
return description.equals(other.description);
}

@Override
public int hashCode() {
return description.hashCode();
}

这是我用来获取其产品类别的存储库:

@Repository
public class CategoryRepository implements ICategoryRepository
{

@PersistenceContext
private EntityManager em;

public CategoryRepository() {
}

public CategoryRepository(EntityManager em)
{
this.em = em;
}

public void setEntityManager(EntityManager e)
{
this.em = e;
}

@Transactional(readOnly = true)
@Override
public List<Categorie> findAll()
{
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Categorie.class));
return em.createQuery(cq).getResultList();
}

在我的jsp中:

  <h1 class="listtitle">Productenlijst</h1>
<div id="leftlist" class="list" >
<c:forEach items="${products}" var="item" >
<form:form method="POST" action="shoppinglist.htm" modelAttribute="products">
<input type="submit" value= "${item.categorieName}" class="productlistbtn" alt="Submit" input path="${item}" >
</form:form>
</c:forEach>
</div>

最佳答案

您的类Product需要声明为@Entity。 (您不能嵌入一对多关系,特别是因为您的 Product 包含它自己的 ID)

关于java - Spring框架关系映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20569357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com