Ниже приведена моя схема.
create table if not exists product_details
(
product_code text not null
constraint product_details_pkey
primary key,
is_active boolean
);
create table if not exists pricing_details
(
pricing_id text not null
constraint pricing_details_pkey
primary key,
is_active text,
is_expired text,
is_archive text,
price int,
product_code text
constraint pricing_details_product_code_fkey
references product_details
);
для этого сгенерирована следующая сущность:
@Entity
@Table(name = "product_details", schema = "public", catalog = "postgres")
public class ProductDetailsEn {
private String productCode;
private String programCode;
private Boolean isActive;
private Collection<PricingDetailsEn> pricingDetailsByProductCode;
@Id
@Column(name = "product_code")
public String getProductCode() {
return productCode;
}
public void setProductCode(String productCode) {
this.productCode = productCode;
}
@Basic
@Column(name = "program_code")
public String getProgramCode() {
return programCode;
}
public void setProgramCode(String programCode) {
this.programCode = programCode;
}
@Basic
@Column(name = "is_active")
public Boolean getActive() {
return isActive;
}
public void setActive(Boolean active) {
isActive = active;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProductDetailsEn that = (ProductDetailsEn) o;
return Objects.equals(productCode, that.productCode) &&
Objects.equals(programCode, that.programCode) &&
Objects.equals(isActive, that.isActive);
}
@Override
public int hashCode() {
return Objects.hash(productCode, programCode, isActive);
}
@OneToMany(mappedBy = "productDetailsByProductCode")
public Collection<PricingDetailsEn> getPricingDetailsByProductCode() {
return pricingDetailsByProductCode;
}
public void setPricingDetailsByProductCode(Collection<PricingDetailsEn> pricingDetailsByProductCode) {
this.pricingDetailsByProductCode = pricingDetailsByProductCode;
}
}
@Entity
@Table(name = "pricing_details", schema = "public", catalog = "postgres")
public class PricingDetailsEn {
private String pricingId;
private String isActive;
private String isExpired;
private String isArchive;
private Integer price;
private ProductDetailsEn productDetailsByProductCode;
@Id
@Column(name = "pricing_id")
public String getPricingId() {
return pricingId;
}
public void setPricingId(String pricingId) {
this.pricingId = pricingId;
}
@Basic
@Column(name = "is_active")
public String getIsActive() {
return isActive;
}
public void setIsActive(String isActive) {
this.isActive = isActive;
}
@Basic
@Column(name = "is_expired")
public String getIsExpired() {
return isExpired;
}
public void setIsExpired(String isExpired) {
this.isExpired = isExpired;
}
@Basic
@Column(name = "is_archive")
public String getIsArchive() {
return isArchive;
}
public void setIsArchive(String isArchive) {
this.isArchive = isArchive;
}
@Basic
@Column(name = "price")
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PricingDetailsEn that = (PricingDetailsEn) o;
return Objects.equals(pricingId, that.pricingId) &&
Objects.equals(isActive, that.isActive) &&
Objects.equals(isExpired, that.isExpired) &&
Objects.equals(isArchive, that.isArchive) &&
Objects.equals(price, that.price);
}
@Override
public int hashCode() {
return Objects.hash(pricingId, isActive, isExpired, isArchive, price);
}
@ManyToOne
@JoinColumn(name = "product_code", referencedColumnName = "product_code")
public ProductDetailsEn getProductDetailsByProductCode() {
return productDetailsByProductCode;
}
public void setProductDetailsByProductCode(ProductDetailsEn productDetailsByProductCode) {
this.productDetailsByProductCode = productDetailsByProductCode;
}
}
Я хочу получить продукт с кодом продукта из product_details в виде prd1, а priceingId равен 4, Я использовал следующие критерии:
Criteria criteria = session.createCriteria(ProductDetailsEn.class, "prd");
criteria.add(Restrictions.eq("prd.productCode", "PRD1"));
criteria.createAlias("prd.pricingDetailsByProductCode", "prc");
criteria.add(Restrictions.eq("prc.pricingId", "4"));
List<ProductDetailsEn> list = criteria.list();
, но это дает мне все записи таблицы цен, ограничение не применяется к этому должным образом. Может кто-нибудь подсказать, что в этом плохого.