当前位置: 首页 > 知识库问答 >
问题:

Hibernate Crertira返回重复项

凤扬
2023-03-14

我购买了一张桌子,DDL为:

CREATE TABLE purchase
(
  idpurchase serial NOT NULL,
  code character varying(50),
  date timestamp without time zone,
  totalht double precision,
  tva double precision,
  totalttc double precision,
  CONSTRAINT purchase_pkey PRIMARY KEY (idpurchase)
)

Purchase表与purchaseproduct有一对多关系,purchaseproduct的DDL为:

CREATE TABLE purchaseproduct
(
  idpurchaseproduct serial NOT NULL,
  idpurchase integer,
  idproduct integer,
  qty double precision,
  price double precision,
  CONSTRAINT purchaseproduct_pkey PRIMARY KEY (idpurchaseproduct),
  CONSTRAINT purchaseproduct_idproduct_fkey FOREIGN KEY (idproduct)
      REFERENCES product (idproduct) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT purchaseproduct_idpurchase_fkey FOREIGN KEY (idpurchase)
      REFERENCES purchase (idpurchase) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

以下是我的POJO:

购买:@Entity@Table(name=“Purchase”)@Access(AccessType.Property)公共类购买{private LongProperty IDPurchase;private StringProperty CodePurchase;private ObjectProperty DatePurchase;private DoubleProperty TotalHt;private DoubleProperty TVA;private DoubleProperty

    private Set<LineCommand> lineItems = new HashSet<LineCommand>(0);

    public Purchase() {
        this.idPurchase = new SimpleLongProperty();
        this.codePurchase = new SimpleStringProperty();
        this.datePurchase = new SimpleObjectProperty<>();
        this.totalHt = new SimpleDoubleProperty();
        this.tva = new SimpleDoubleProperty();
        this.totalTTC = new SimpleDoubleProperty();


    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "purchase_seq_gen")
    @SequenceGenerator(name = "purchase_seq_gen", sequenceName = "purchase_idpurchase_seq", initialValue = 1, allocationSize = 1)
    @Column(name = "idpurchase", unique = true, nullable = false)
    public long getIdPurchase() {
        return idPurchase.get();
    }

    public LongProperty idPurchaseProperty() {
        return idPurchase;
    }

    public void setIdPurchase(long idPurchase) {
        this.idPurchase.set(idPurchase);
    }

    @Column(name = "code")
    public String getCodePurchase() {
        return codePurchase.get();
    }

    public StringProperty codePurchaseProperty() {
        return codePurchase;
    }

    public void setCodePurchase(String codePurchase) {
        this.codePurchase.set(codePurchase);
    }

    @Column(name = "date")
    @Convert(converter = LocalDatePersistanceConverter.class)
    public LocalDate getDatePurchase() {
        return datePurchase.get();
    }

    public ObjectProperty<LocalDate> datePurchaseProperty() {
        return datePurchase;
    }

    public void setDatePurchase(LocalDate datePurchase) {
        this.datePurchase.set(datePurchase);
    }

    @Column(name = "totalHt")
    public double getTotalHt() {
        return totalHt.get();
    }

    public DoubleProperty totalHtProperty() {
        return totalHt;
    }

    public void setTotalHt(double totalHt) {
        this.totalHt.set(totalHt);
    }

    @Column(name = "tva")
    public double getTva() {
        return tva.get();
    }

    public DoubleProperty tvaProperty() {
        return tva;
    }

    public void setTva(double tva) {
        this.tva.set(tva);
    }

    @Column(name = "totalTTC")
    public double getTotalTTC() {
        return totalTTC.get();
    }

    public DoubleProperty totalTTCProperty() {
        return totalTTC;
    }

    public void setTotalTTC(double totalTTC) {
        this.totalTTC.set(totalTTC);
    }


    @OneToMany(mappedBy = "purchase", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public Set<LineCommand> getLineItems() {
        return this.lineItems;
    }

    public void setLineItems(Set<LineCommand> lineItems) {
        this.lineItems = lineItems;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        } else {

            if (this.idPurchase.getValue() == ((Purchase) obj).getIdPurchase())
                return true;
            else
                return false;
        }
    }

}

线路通信:

@Entity
@Table(name = "purchaseProduct")
@Access(AccessType.PROPERTY)
public class LineCommand implements Serializable {


    private LongProperty idPurchaseProduct;
    private Product product;
    private Purchase purchase;
    private DoubleProperty sellPrice = new SimpleDoubleProperty();
    private DoubleProperty qty = new SimpleDoubleProperty();
    private DoubleProperty subTotal = new SimpleDoubleProperty();


    public LineCommand() {
        this.idPurchaseProduct = new SimpleLongProperty();
        this.product = new Product();
        this.purchase = new Purchase();

        this.sellPrice = new SimpleDoubleProperty();
        this.qty = new SimpleDoubleProperty();
        this.subTotal = new SimpleDoubleProperty();

        NumberBinding subTotalBinding = Bindings.multiply(this.qty, this.sellPrice);
        subTotal.bind(subTotalBinding);

    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "purchase_seq_gen")
    @SequenceGenerator(name = "purchase_seq_gen", sequenceName = "purchaseproduct_idpurchaseproduct_seq", initialValue = 1, allocationSize = 1)
    @Column(name = "idpurchaseproduct ", unique = true, nullable = false)
    public long getIdPurchaseProduct() {
        return idPurchaseProduct.get();
    }

    public LongProperty idPurchaseProductProperty() {
        return idPurchaseProduct;
    }

    public void setIdPurchaseProduct(long idPurchaseProduct) {
        this.idPurchaseProduct.set(idPurchaseProduct);
    }

    @ManyToOne
    @JoinColumn(name = "idproduct")
    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    @ManyToOne
    @JoinColumn(name = "idpurchase")
    public Purchase getPurchase() {
        return purchase;
    }

    public void setPurchase(Purchase purchase) {
        this.purchase = purchase;
    }


    @Column(name = "price")
    public double getSellPrice() {
        return sellPrice.get();
    }

    public DoubleProperty sellPriceProperty() {
        return sellPrice;
    }

    public void setSellPrice(double sellPrice) {
        this.sellPrice.set(sellPrice);
    }

    @Column(name = "qty")
    public double getQty() {
        return qty.get();
    }

    public DoubleProperty qtyProperty() {
        return qty;
    }

    public void setQty(double qty) {
        this.qty.set(qty);
    }

    @Transient
    public double getSubTotal() {
        return subTotal.get();
    }

    public DoubleProperty subTotalProperty() {
        return subTotal;
    }

    public void setSubTotal(double subTotal) {
        this.subTotal.set(subTotal);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        } else {
            Product product = ((LineCommand) obj).getProduct();
            Purchase purchase = ((LineCommand) obj).getPurchase();

            if (this.product.equals(product) && this.purchase.equals(purchase))
                return true;
            else
                return false;
        }
    }
}

从数据库获取所有采购结果后,我注意到在采购中有两个条目,在purchaseproduct表中有重复。

以下是我从数据库中检索购买清单的方法:

public ObservableList<Purchase> findAllByDate(LocalDate from, LocalDate to) {
        try {
            if (!session.isOpen())
                session = DatabaseUtil.getSessionFactory().openSession();
            session.beginTransaction();

            Criterion ctr1 = Restrictions.le("datePurchase", to);
            Criterion ctr2 = Restrictions.ge("datePurchase", from);
            List<Purchase> resultList = session.createCriteria(Purchase.class).add(Restrictions.and(ctr1, ctr2)).list();

            ObservableList<Purchase> list =
                    FXCollections.observableList(resultList);
            session.getTransaction().commit();
            session.close();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

共有1个答案

袁凌
2023-03-14

就像他提到的“提普苏丹”,她。解决方案将是

使用:

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)

在我的情况下,代码将是:

public ObservableList<Purchase> findAllByDate(LocalDate from, LocalDate to) {
        try {
            if (!session.isOpen())
                session = DatabaseUtil.getSessionFactory().openSession();
            session.beginTransaction();

            Criterion ctr1 = Restrictions.le("datePurchase", to);
            Criterion ctr2 = Restrictions.ge("datePurchase", from);

            List<Purchase> resultList = session.createCriteria(Purchase.class)
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                    .add(Restrictions.and(ctr1, ctr2)).list();

            ObservableList<Purchase> list = FXCollections.observableList(resultList);
            session.getTransaction().commit();
            session.close();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 类似资料:
  • 问题内容: 考虑到此代码,我是否可以 绝对确定 该块始终执行,无论它是什么? 问题答案: 是的,将在执行或代码块后调用。 唯一不会被调用的时间是: 如果您调用 如果您调用 如果JVM首先崩溃 如果JVM在或块中达到了无限循环(或其他不间断,不终止的语句) 操作系统是否强行终止了JVM进程;例如,在UNIX上 如果主机系统死机;例如,电源故障,硬件错误,操作系统崩溃等 如果该块将由守护程序线程执行并

  • 我试图将三列作为一个字符串,但由于某些原因,我只得到NULL。 以下是代码: places表包含name、city和country三列,它不是空的,但在某些列中可以包含NULL。 结果是: 我试着从这篇帖子中得到答案,如果任何字段包含NULL,MySQL CONCAT将返回NULL,但它对我不起作用。

  • 问题内容: 这个问题已经在这里有了答案 : java InetAddress.getLocalHost(); 返回127.0.0.1…如何获得REAL IP? (11个答案) 7年前关闭。 我的问题类似于这个问题。我想获取机器的真实IP(不是127.0.0.1),但是很奇怪,Ubuntu中的以下代码返回了127.0.1.1。 以下是我的完整代码,最初在此处发布在SO中 上面的代码返回127.0.1

  • 问题内容: 每次我运行mysql_fetch_array时,返回带有重复值的数组, 例如 但我只想要数组中的单个结果,我尝试使用 但这没有区别。 问题答案: 这是的预期功能。如果您不希望有“重复项”,而只是具有关联数组,请改用。 例: