how to write Hibernate Criteria to take nested objects by Projection List?

前端 未结 3 806
名媛妹妹
名媛妹妹 2020-12-30 07:32

I want to take Nested object values in Hibernate Projection List. I having Pojo \'Charge\' and \'Tariff\' class with OneToMany and ManyToOne relations.

My sample cod

3条回答
  •  醉话见心
    2020-12-30 08:08

    My solution is very basic. It's not as clean as a proper result transformer but it's useful when you just need to do a quick projection for a few properties.

    Instead of .add(Projections.property("tariff.amount"),"amount")) type .add(Projections.property("tariff.amount"),"tariffAmount"))

    Then, just add a setter on your root object "setTariffAmount".

    public void setTariffAmount(String tariffAmount) {
      this.tariff = (this.tariff==null) ? new Tariff() : tariff;
      tariff.setAmount(tariffAmount);
    }
    

    The drawback is that it "dirties" your object with extra methods.

提交回复
热议问题