-->
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.  [ 6 posts ] 
Author Message
 Post subject: OneToMany through association
PostPosted: Wed Nov 13, 2013 9:26 pm 
Newbie

Joined: Thu Oct 17, 2013 8:18 pm
Posts: 13
We have 3 tables. Items, ItemImages and ItemImageColors.

Table: Items
Column: id

Table: ItemImages
Column: id
Column: item_id

Table: ItemImageColors
Column: id
Column: item_image_id
Column: color_value

Now I want to get all image color values for a particular item. How can this be accomplished? In rails I can easily create an association through another association. Is this possible in Hibernate?


Top
 Profile  
 
 Post subject: Re: OneToMany through association
PostPosted: Thu Nov 14, 2013 6:15 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
I'm not familiar with rails but
assuming your entities look something like these:
Code:
@Entity
class Items {
    @Id
    long id
}

@Entity
class ItemImages {

    @ManyToOne
    Items item;

}

@Entity
class ItemImageColors {
    String colorValue

    @ManyToOne
    ItemImages image;
}


Using HQL the query should look like this:
Code:
SELECT
    color.colorValue
FROM
    ItemImageColors color
        JOIN color.image image
        JOIN image.item item
WHERE
    item.id = :id


I hope this answer your question.

Cheers,
Davide


Top
 Profile  
 
 Post subject: Re: OneToMany through association
PostPosted: Thu Nov 14, 2013 1:37 pm 
Newbie

Joined: Thu Oct 17, 2013 8:18 pm
Posts: 13
Thanks but Im actually already joining on multiple tables so I would hate to rewrite that all in HQL.

.createCriteria(Item.class)
.add(Restrictions.eq("id", id))
.setFetchMode("association1", FetchMode.JOIN)
.setFetchMode("association2", FetchMode.JOIN)
.setFetchMode("association3", FetchMode.JOIN)
.setFetchMode("association4", FetchMode.JOIN)
.setFetchMode("association5", FetchMode.JOIN)

Is there a way I can prepend some HQL to the above Criteria?


Top
 Profile  
 
 Post subject: Re: OneToMany through association
PostPosted: Thu Nov 14, 2013 9:10 pm 
Newbie

Joined: Thu Oct 17, 2013 8:18 pm
Posts: 13
I also want something like this if possible..

Code:
@Entity
class Items {
    @Id
    long id

    @OneToMany(..????)
    Set<ItemImageColors> itemImageColors;
   
}


Is there anyway I can have that association on the Item class?


Top
 Profile  
 
 Post subject: Re: OneToMany through association
PostPosted: Fri Nov 15, 2013 6:00 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Abut the mapping, this part of the documentation should explain how to do that: http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch07.html#example.collection.mapping.annotations

Looking at the documentation, it should be something like this:

Code:
sess.createCriteria(Item.class)
    .add( Restrictions.like("id", id) )
    .createAlias("itemImageColors", "color")
    .setProjection( Projections.property("color.colorValue"), "color" )
    .list();


These are the part of the documentation I've used:
- http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch17.html#querycriteria-associations
- http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch17.html#querycriteria-projection


Top
 Profile  
 
 Post subject: Re: OneToMany through association
PostPosted: Fri Nov 15, 2013 3:29 pm 
Newbie

Joined: Thu Oct 17, 2013 8:18 pm
Posts: 13
That would work for a join table but this isnt a typical join table.

In my use case I want to join on ItemImages where ItemImage.item_id = Item.id and then join on ItemImageColors where ItemImageColor.item_image_id = ItemImage.id

I can somewhat get the behavior I'm looking for by setting eager load true on all associations...

Code:
class Item {
  @OneToMany(mappedBy = "item", fetch = FetchType.EAGER)
  private Set<ItemImage> itemImages;
}

class ItemImage {
  @OneToMany(mappedBy = "itemImage", fetch = FetchType.EAGER)
  private Set<ItemImageColor> itemImageColors;
}

class ItemImageColor {
  @ManyToOne
  @JoinColumn(name = "item_image_id")
  private ItemImage itemImage;
}


I then need to loop through all images and their colors to get the final result which is less than ideal. I would like to simply do the following

Code:
  for (ItemImageColor color : item.getItemImageColors) {
    ....
  }



davided80 wrote:
Abut the mapping, this part of the documentation should explain how to do that: http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch07.html#example.collection.mapping.annotations
Thanks but this is for a join table where the join table has a reference to the other tables.. ie ItemImage has a reference to Item and ItemImageColor.

In my use case the table ItemImage has a reference to Item and ItemImageColor has a reference to ItemImage.

Looking at the documentation, it should be something like this:

Code:
sess.createCriteria(Item.class)
    .add( Restrictions.like("id", id) )
    .createAlias("itemImageColors", "color")
    .setProjection( Projections.property("color.colorValue"), "color" )
    .list();


These are the part of the documentation I've used:
- http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch17.html#querycriteria-associations
- http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch17.html#querycriteria-projection


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.