-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate ManyToOne association and join tables in SQL query
PostPosted: Mon Jul 09, 2012 8:48 am 
Newbie

Joined: Sat Jan 21, 2012 10:54 am
Posts: 2
I have a question about Hibernate ManyToOne association.

I have a Product entity mapped like this:


Code:
public class Product {
    @Id
    @DocumentId
    @Column(name = "PRODUCT_ID", columnDefinition = "integer")
    @Index(name = "PRODUCT_ID_IDX")
    private Long id;

    @Column(name = "SALE_PRICE")
    @Index(name = "PRODUCT_PRICE_IDX")
    private Double salePrice;
    ...
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CATEGORY_ID", nullable = true)
    @Index(name = "PRODUCT_ID_CATEGORY_IDX")
    private ProductCategory category;
    }


So how can you see we have relationship with ProductCategory table. And FK we store in Product table.
When I create query which select Product by specific category and price I have next SQL:
Code:
select
        this_.PRODUCT_ID as PRODUCT1_9_1_,
        this_.BUY_URL as BUY2_9_1_,
        this_.CATEGORY_ID as CATEGORY15_9_1_,
        this_.CURRENCY as CURRENCY9_1_,
        this_.IMAGE_URL as IMAGE4_9_1_,
        this_.INSTOCK as INSTOCK9_1_,
        this_.LONG_DESCRIPTION as LONG6_9_1_,
        this_.NAME as NAME9_1_,
        this_.RATING as RATING9_1_,
        this_.RETAIL_PRICE as RETAIL9_9_1_,
        this_.SALE_PRICE as SALE10_9_1_,
        this_.SHIPPING as SHIPPING9_1_,
        this_.SHORT_DESCRIPTION as SHORT12_9_1_,
        this_.UPC as UPC9_1_,
        this_.VIEWS as VIEWS9_1_,
        cat1_.CATEGORY_ID as CATEGORY1_10_0_,
        cat1_.NAME as NAME10_0_,
        cat1_.RATING as RATING10_0_,
        cat1_.VIEWS as VIEWS10_0_,
        cat1_.VISIBLE as VISIBLE10_0_
    from
        PRODUCT this_
    inner join
        PRODUCT_CATEGORY cat1_
            on this_.CATEGORY_ID=cat1_.CATEGORY_ID
    where
        (
            this_.SALE_PRICE between ? and ?
            and cat1_.CATEGORY_ID in (
                ?
            )
        )
    order by
        this_.NAME asc limit ? offset ?


My question is next: How do I can avoid inner join of two tables: Product and ProductCategory?

My Criteria DAO code:
Code:
criteria.createAlias("category", "cat");
    criteria.add(Restrictions.conjunction()
    .add(Restrictions.between("salePrice", priceLow, priceHigh))
    .add(Restrictions.in("cat.id", categoryIds)));


Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.