Java 休眠查询导致大量额外的不必要查询

Java 休眠查询导致大量额外的不必要查询,java,sql,hibernate,jpa,Java,Sql,Hibernate,Jpa,我正在开发一个拍卖网站。问题在于我使用的3个实体: 产品(具有零个或多个ProductBid) ProductBid(零个或一个ProductBid拒绝) 产品拒绝 我使用hibernate查询来获取出价: ProductBidRejection } 这是因为您在ProductBid上有@Fetch(FetchMode.JOIN)。 因此,对于您检索到的每个ProductBidRejections,它也会加载一个ProductBid 更新 尝试此查询。它将获得不同的pb并急切地获取PBR

我正在开发一个拍卖网站。问题在于我使用的3个实体:

  • 产品(具有零个或多个ProductBid)
  • ProductBid(零个或一个ProductBid拒绝)
  • 产品拒绝
我使用hibernate查询来获取出价:


ProductBidRejection

}

这是因为您在ProductBid上有
@Fetch(FetchMode.JOIN)
。 因此,对于您检索到的每个ProductBidRejections,它也会加载一个ProductBid

更新

尝试此查询。它将获得不同的pb并急切地获取PBR


从ProductBid pb left join fetch pb.rejection pbr中选择不同的pb,其中pbr为null,pb.product=:产品订单按pb.amount desc

使用标准而不是HQL您的问题将得到解决


在ProductBid实体类中,使用注释将ProductBidRejection加入到ProductBidRejection中

删除ProductBid上的@Fetch(FetchMode.join)会从查询中删除“productbid1”。然而,它仍然使用为每个出价选择。谢谢你的回答。distinct查询有效:)看起来“distinct”的工作原理与普通的T-SQL/MySQL略有不同? select pb from ProductBid pb left join pb.rejection pbr where pbr is null and pb.product = :product order by pb.amount desc
select productbid0_.id as id4_, productbid0_.amount as amount4_, productbid0_.bid_by as bid4_4_, productbid0_.date as date4_, productbid0_.product_id as product5_4_ from product_bids productbid0_ left outer join product_bid_rejections productbid1_ on productbid0_.id=productbid1_.product_bid_id where ( productbid1_.id is null ) and productbid0_.product_id=? select productbid0_.id as id3_1_, productbid0_.date_rejected as date2_3_1_, productbid0_.product_bid_id as product4_3_1_, productbid0_.reason as reason3_1_, productbid0_.rejected_by as rejected5_3_1_, productbid1_.id as id4_0_, productbid1_.amount as amount4_0_, productbid1_.bid_by as bid4_4_0_, productbid1_.date as date4_0_, productbid1_.product_id as product5_4_0_ from product_bid_rejections productbid0_ inner join product_bids productbid1_ on productbid0_.product_bid_id=productbid1_.id where productbid0_.product_bid_id=?

@Entity
@Table(name = "product_bids")
public class ProductBid
{
    @Column(name = "id", nullable = false)
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @JoinColumn(name = "product_id", nullable = false)
    @Index(name="product")
    @ManyToOne(fetch = FetchType.LAZY)
    private Product product;

    @Column(name = "amount", nullable = false)
    private BigDecimal amount;

    @JoinColumn(name = "bid_by", nullable = false)
    @Index(name="bidBy")
    @ManyToOne(fetch = FetchType.LAZY)
    @Fetch(FetchMode.JOIN)
    private User bidBy;

    @Column(name = "date", nullable = false)
    @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
    private DateTime date;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "productBid")
    private ProductBidRejection rejection;
}
@Entity @Table(name = "product_bid_rejections") public class ProductBidRejection { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", nullable = false) private long id;

@Column(name = "reason", nullable = false, columnDefinition = "TEXT")
private String reason;

@Column(name = "date_rejected", nullable = false)
@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime dateRejected;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "rejected_by", nullable = false)
private User rejectedBy;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_bid_id", nullable = false)
@Fetch(FetchMode.JOIN)
private ProductBid productBid;
session.createCriteria(ProductBid.class).add(Restrictions.eq("product",yourproduct)).list();