2010-12-06 8 views
3

私はオークションサイトを開発中です。問題は、私が使用する3つの事業体である:多くの追加の不要なクエリを引き起こすHibernateクエリ

  • 製品(ゼロまたは多くのProductBidを持っている)
  • ProductBid(0または1 ProductBidRejectionを持っている)
  • 私は休止状態クエリを使用ProductBidRejection

入札を得る:

select pb from ProductBid pb left join pb.rejection pbr where pbr is null and pb.product = :product order by pb.amount desc

This generates this query (via console):

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=?

But for each bid it gets it also generates:

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=?

These are my entities:

ProductBid


@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; 
} 

ProductBidRejection

 
@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; 

}

答えて

2

ProductBidに@Fetch(FetchMode.JOIN)があるためです。 したがって、取得するProductBidRejectionのそれぞれについて、ProductBidも読み込まれます。

UPDATE

この文字列で検索してください。それは、PBの異なる取得し、熱心にPBR

select distinct pb from ProductBid pb left join fetch pb.rejection pbr where pbr is null and pb.product = :product order by pb.amount desc

+0

ProductBidの@Fetch(FetchMode.JOIN)を削除すると、クエリから 'productbid1'が削除されます。しかし、それは各入札のためにまだ選択を使用します。 –

+0

あなたの答えをありがとう。別のクエリが働いた:)それは "別個の"は、通常のT - SQL/MySQLとわずかに異なる作品と思われる? –

+0

喜んで:) –

0
代わりにHQLの

使用基準は、あなたの問題は

session.createCriteria(ProductBid.class).add(Restrictions.eq("product",yourproduct)).list(); 

を解決し、ProductBidエンティティークラスにProductBidRejection

にEAGER LYに参加するためにアノテーションを使用しますがフェッチされます
関連する問題