我如何使用@ManyToMany 在另一个类别中获得价值类别和类别产品 [英] How i get value category and categories product in another class with using @ManyToMany
本文介绍了我如何使用@ManyToMany 在另一个类别中获得价值类别和类别产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 3 节课.
产品类别
package com.eziz.warehouse;导入 com.eziz.clients.Clients;导入 org.springframework.format.annotation.DateTimeFormat;导入 javax.persistence.*;@实体@Table(名称=产品")公共类产品{@Id@GeneratedValue(策略 = GenerationType.IDENTITY)私人长产品ID;@Column(可为空 = 假,长度 = 20)私人字符串产品名称;@ManyToOne@JoinColumn(name = "category_id")私人产品类别产品类别;@Column(可为空 = 假,长度 = 10)私人 int productCount;@DateTimeFormat(pattern = "yyyy-MM-dd")@Column(可为空 = 假)私人字符串 productDateCreated;@Column(可为空 = 假)私人双品采购价格;公共长 getProductId() {返回产品ID;}public void setProductId(Long productId) {this.productId = productId;}公共字符串 getProductName() {返回产品名称;}public void setProductName(String productName) {this.productName = productName;}公共 int getProductCount() {返回产品数量;}public void setProductCount(int productCount) {this.productCount = productCount;}公共字符串 getProductDateCreated() {返回 productDateCreated;}public void setProductDateCreated(String productDateCreated) {this.productDateCreated = productDateCreated;}公共双 getProductPurchasePrice() {返回产品购买价格;}public void setProductPurchasePrice(double productPurchasePrice) {this.productPurchasePrice = productPurchasePrice;}公共产品类别 getProductCategory() {返回产品类别;}public void setProductCategory(ProductCategory productCategory) {this.productCategory = productCategory;}@覆盖公共字符串 toString() {返回 this.productName;}}
ProductCategory 类.
package com.eziz.warehouse;导入 javax.persistence.*;导入 java.util.HashSet;导入 java.util.Set;@实体@Table(name = "productcategory")公共类产品类别{@Id@GeneratedValue(策略 = GenerationType.IDENTITY)私人 Long productCategoryId;@Column(可为空 = 假,长度 = 30)私人字符串产品类别名称;//@ManyToOne//@JoinColumn(name = "productCategory")//私有产品产品;@OneToMany(mappedBy = "productCategory")私人集<产品>产品 = 新 HashSet<>();公共长 getProductCategoryId() {返回 productCategoryId;}public void setProductCategoryId(Long productCategoryId) {this.productCategoryId = productCategoryId;}公共字符串 getProductCategoryName() {返回产品类别名称;}public void setProductCategoryName(String productCategoryName) {this.productCategoryName = productCategoryName;}//公共产品 getProduct() {//返回产品;//}////public void setProduct(Product product) {//this.product = 产品;//}公共集<产品>获取产品(){退货产品;}public void setProducts(Set products) {this.products = 产品;}}
和请求类.
package com.eziz.requestForWarehouse;导入 com.eziz.requests.Requests;导入 com.eziz.user.User;导入 com.eziz.warehouse.Product;导入 com.eziz.warehouse.ProductCategory;导入 org.springframework.format.annotation.DateTimeFormat;导入 javax.persistence.*;导入 java.util.HashSet;导入 java.util.Set;@实体@Table(name = "requestWH")公共类请求仓库{@Id@GeneratedValue(策略 = GenerationType.IDENTITY)私人长请求WHId;@Column(可为空 = 假,长度 = 10)私人 int requestWHCount;@Column(可为空 = 假,长度 = 15)私人字符串请求WHStatus;@DateTimeFormat(pattern = "yyyy-MM-dd")@Column(可为空 = 假)私人字符串 requestWHDateCreated;@DateTimeFormat(pattern = "yyyy-MM-dd")@Column(可为空 = 假)私人字符串请求WHExpiryTime;@ManyToMany@JoinColumn(name = "requestWHProductId")私人集productCategory = new HashSet<>();@ManyToOne@JoinColumn(name = "requestWHRequestId")私人请求请求;公共长 getRequestWHId() {返回请求WHId;}public void setRequestWHId(Long requestWHId) {this.requestWHId = requestWHId;}公共 int getRequestWHCount() {返回请求WHCount;}public void setRequestWHCount(int requestWHCount) {this.requestWHCount = requestWHCount;}公共字符串 getRequestWHStatus() {返回请求WHStatus;}public void setRequestWHStatus(String requestWHStatus) {this.requestWHStatus = requestWHStatus;}公共字符串 getRequestWHDateCreated() {返回请求WHDateCreated;}public void setRequestWHDateCreated(String requestWHDateCreated) {this.requestWHDateCreated = requestWHDateCreated;}公共字符串 getRequestWHExpiryTime() {返回请求WHExpiryTime;}public void setRequestWHExpiryTime(String requestWHExpiryTime) {this.requestWHExpiryTime = requestWHExpiryTime;}//公共产品类别 getProductCategory() {//返回产品类别;//}////public void setProductCategory(ProductCategory productCategory) {//this.productCategory = productCategory;//}公共集获取产品类别(){返回产品类别;}public void setProductCategory(Set productCategory) {this.productCategory = productCategory;}公共请求 getRequests() {退货要求;}公共无效setRequests(请求请求){this.requests = 请求;}}
我需要在请求中使用它,首先选择类别,然后使用选择选项选择类别产品.
html 代码:
<p对齐=中心"class=font-aziz">Avadanlıq/Detal Kateqoriya</p><select th:field="*{productCategory}";class="form-control un";id=产品"需要><th:block th:each="pro : ${productCategoryList}"><option th:text="${pro.productCategoryName}";th:value=${pro.productCategoryId}"/></th:block></选择><!-- <div class="form-group">--><!-- <p align="center";class=font-aziz">Avadanlıq/Detal</p>--><!--
那我怎么解决这个问题?我需要得到产品.但使用类别.我需要在请求中使用它,首先选择类别,然后选择带有选择选项的类别产品.
解决方案
<p对齐=中心"class="font-aziz">Avadanlıq/Detal</p><select th:field="*{productCategory}";class="form-control un";id=产品1"需要><span th:each=pro : ${productCategoryList}"><th:block th:each="pro1 : ${pro.products}"><option th:text="${pro1.productName}";th:value=${requestWH.productCategory}"/></th:block></span></选择>
这行得通,但我不明白.
I have 3 class.
Product Class
package com.eziz.warehouse;
import com.eziz.clients.Clients;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long productId;
@Column(nullable = false, length = 20)
private String productName;
@ManyToOne
@JoinColumn(name = "category_id")
private ProductCategory productCategory;
@Column(nullable = false, length = 10)
private int productCount;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(nullable = false)
private String productDateCreated;
@Column(nullable = false)
private double productPurchasePrice;
public Long getProductId() {
return productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getProductCount() {
return productCount;
}
public void setProductCount(int productCount) {
this.productCount = productCount;
}
public String getProductDateCreated() {
return productDateCreated;
}
public void setProductDateCreated(String productDateCreated) {
this.productDateCreated = productDateCreated;
}
public double getProductPurchasePrice() {
return productPurchasePrice;
}
public void setProductPurchasePrice(double productPurchasePrice) {
this.productPurchasePrice = productPurchasePrice;
}
public ProductCategory getProductCategory() {
return productCategory;
}
public void setProductCategory(ProductCategory productCategory) {
this.productCategory = productCategory;
}
@Override
public String toString() {
return this.productName;
}
}
ProductCategory Class.
package com.eziz.warehouse;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "productcategory")
public class ProductCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long productCategoryId;
@Column(nullable = false, length = 30)
private String productCategoryName;
// @ManyToOne
// @JoinColumn(name = "productCategory")
// private Product product;
@OneToMany(mappedBy = "productCategory")
private Set<Product> products = new HashSet<>();
public Long getProductCategoryId() {
return productCategoryId;
}
public void setProductCategoryId(Long productCategoryId) {
this.productCategoryId = productCategoryId;
}
public String getProductCategoryName() {
return productCategoryName;
}
public void setProductCategoryName(String productCategoryName) {
this.productCategoryName = productCategoryName;
}
// public Product getProduct() {
// return product;
// }
//
// public void setProduct(Product product) {
// this.product = product;
// }
public Set<Product> getProducts() {
return products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
}
and Request Class.
package com.eziz.requestForWarehouse;
import com.eziz.requests.Requests;
import com.eziz.user.User;
import com.eziz.warehouse.Product;
import com.eziz.warehouse.ProductCategory;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "requestWH")
public class RequestWarehouse {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long requestWHId;
@Column(nullable = false, length = 10)
private int requestWHCount;
@Column(nullable = false, length = 15)
private String requestWHStatus;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(nullable = false)
private String requestWHDateCreated;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(nullable = false)
private String requestWHExpiryTime;
@ManyToMany
@JoinColumn(name = "requestWHProductId")
private Set<ProductCategory> productCategory = new HashSet<>();
@ManyToOne
@JoinColumn(name = "requestWHRequestId")
private Requests requests;
public Long getRequestWHId() {
return requestWHId;
}
public void setRequestWHId(Long requestWHId) {
this.requestWHId = requestWHId;
}
public int getRequestWHCount() {
return requestWHCount;
}
public void setRequestWHCount(int requestWHCount) {
this.requestWHCount = requestWHCount;
}
public String getRequestWHStatus() {
return requestWHStatus;
}
public void setRequestWHStatus(String requestWHStatus) {
this.requestWHStatus = requestWHStatus;
}
public String getRequestWHDateCreated() {
return requestWHDateCreated;
}
public void setRequestWHDateCreated(String requestWHDateCreated) {
this.requestWHDateCreated = requestWHDateCreated;
}
public String getRequestWHExpiryTime() {
return requestWHExpiryTime;
}
public void setRequestWHExpiryTime(String requestWHExpiryTime) {
this.requestWHExpiryTime = requestWHExpiryTime;
}
// public ProductCategory getProductCategory() {
// return productCategory;
// }
//
// public void setProductCategory(ProductCategory productCategory) {
// this.productCategory = productCategory;
// }
public Set<ProductCategory> getProductCategory() {
return productCategory;
}
public void setProductCategory(Set<ProductCategory> productCategory) {
this.productCategory = productCategory;
}
public Requests getRequests() {
return requests;
}
public void setRequests(Requests requests) {
this.requests = requests;
}
}
I need use this in request, first select category and after categoryproducts with select option.
html code:
<div class="form-group">
<p align="center" class="font-aziz">Avadanlıq / Detal Kateqoriya</p>
<select th:field="*{productCategory}" class="form-control un" id="product" required>
<th:block th:each="pro : ${productCategoryList}">
<option th:text="${pro.productCategoryName}"
th:value="${pro.productCategoryId}"/>
</th:block>
</select>
</div>
<!-- <div class="form-group">-->
<!-- <p align="center" class="font-aziz">Avadanlıq / Detal</p>-->
<!-- <select th:field="*{productCategory}" class="form-control un" id="product1" required>-->
<!-- <th:block th:each="pro : ${productCategoryList.products}">-->
<!-- <option th:text="${pro.productName}"-->
<!-- th:value="${productCategory}"/>-->
<!-- </th:block>-->
<!-- </select>-->
<!-- </div>-->
and so how i fix this? i am need get products. but using category. I need use this in request, first select category and after categoryproducts with select option.
解决方案
<div class="form-group">
<p align="center" class="font-aziz">Avadanlıq / Detal</p>
<select th:field="*{productCategory}" class="form-control un" id="product1" required>
<span th:each="pro : ${productCategoryList}">
<th:block th:each="pro1 : ${pro.products}">
<option th:text="${pro1.productName}"
th:value="${requestWH.productCategory}"/>
</th:block>
</span>
</select>
</div>
this worked, but i dont get all .
这篇关于我如何使用@ManyToMany 在另一个类别中获得价值类别和类别产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文