-->
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.  [ 2 posts ] 
Author Message
 Post subject: HQL Join
PostPosted: Wed Jan 27, 2010 2:16 am 
Newbie

Joined: Mon Dec 14, 2009 5:38 am
Posts: 13
Hello,
I have two annotated entities like below:
Code:
public class Item implements Serializable {
  private Long id;
  private String name;
  private ItemBrand item_brand;
  ....
  @ManyToOne(targetEntity=ItemBrand.class)
  @NotFound(action=NotFoundAction.IGNORE)
  @JoinColumn(name="item_brand")
  public ItemBrand getItem_brand()...
  public void setItem_brand(ItemBrand item_brand)...
}

public class ItemBrand implements Serializable {
  private Long id;
  private String item_brand;
  ....
  public void setItem_brand(String item_brand)...
  public String getItem_brand()...
}


And the table contents is like this:

    Item
    ------------------------------------
    id name item_brand
    ------------------------------------
    1 Mouse 10
    2 Monitor 11

    ItemBrand
    ------------------------
    id item_brand
    ------------------------
    10 Microsoft
    11 Samsung

usually, if I want to fetch all the Item data, I use this:

Code:
List list = HibernateUtil.getSessionFactory().getCurrentSession().createQuery("from Item");


it will return me:

1 Mouse Microsoft
2 Monitor Samsung

but now I want to find Item of specific ItemBrand, so if I want to get data where the ItemBrand is Samsung, for instance.

how can I do this using createQuery() ?


Top
 Profile  
 
 Post subject: Re: HQL Join
PostPosted: Wed Jan 27, 2010 3:07 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
For example:

Code:
from Item i where i.item_brand.item_brand = 'Samsung'


Read http://docs.jboss.org/hibernate/stable/ ... yhql-joins for more examples


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

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.