-->
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.  [ 5 posts ] 
Author Message
 Post subject: Localization technique
PostPosted: Mon Dec 22, 2008 11:07 pm 
Newbie

Joined: Mon Dec 22, 2008 10:53 pm
Posts: 3
We have the following schema:
A product table that has locale independent data and a productDescription table that has a productId foreign key and locale field. We are using the criteria query below to fetch products for a specific locale:

Criteria criteria = getSession().createCriteria(Product.class);
Criteria detailsCriteria = criteria.createCriteria("productDetails");
detailsCriteria.add(Restrictions.eq("locale", locale));
return (List<Family>) criteria.list();

However the list returned has all productDetails irrespective of the locale. We tried adding a detailsCriteria.setFetchMode("family", FetchMode.JOIN); to force an Eager fetch, but that doesn't seem to help. Any ideas on how to go about this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 2:53 pm 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
I'm a little confused about what you want. You have a product table and you also have a productDescription table with a productId foreign key and a locale field. Simple enough, though I don't know how family comes into play.

Now what exactly do you want to retrieve? All products which have a productDescription in a specified locale?

You can use a query:
Code:
Query query = getSession().createQuery("SELECT product FROM Product product join product.productDetails productDetails where productDetails.locale = :locale");
query.setString("locale", locale);
return query.list();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 4:55 pm 
Newbie

Joined: Mon Dec 22, 2008 10:53 pm
Posts: 3
Sorry about the 'Family' confusion - bad copy paste combo.

I am trying to retrieve all products with a product description under a locale.

Your query returns an array of product/productDetails objects. The productDetails objects are for the specified locale. However, if I do a product.getProductDetails on a product object that's returned, I get all productDetails objects regardless of locale. It seems like Hibernate is doing a second query when I fetch productDetails under product, but the second one does not pass in a locale, it just does a join on the id.

Here're the entity objects for Product and ProductDetails.

Code:
@Entity
@Table(name = "Products")
public class Product {

   @Id
   @Column(name = "ProductID")
   @GeneratedValue(strategy = GenerationType.AUTO)
   private int id;

   @OneToMany(mappedBy = "product")
   private List<ProductDetails> productDetails;

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public List<ProductDetails> getProductDetails() {
      return productDetails;
   }

   public void setProductDetails(List<ProductDetails> productDetails) {
      this.productDetails = productDetails;
   }

}

@Entity
@Table(name = "ProductDetails")
public class ProductDetails {

   @Id
   @Column(name = "ProductDetailID")
   @GeneratedValue(strategy = GenerationType.AUTO)
   private int id;

   @ManyToOne
   @JoinColumn(name = "ProductID")
   private Product product;

   @Column(name = "Locale")
   private String locale;

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public Product getProduct() {
      return product;
   }

   public void setProduct(Product product) {
      this.product = product;
   }

   public String getLocale() {
      return locale;
   }

   public void setLocale(String locale) {
      this.locale = locale;
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 6:01 pm 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
If I understand you correctly, you want a product returned where when calling its getProductDetails() method sees only the 1 productDetails object with the matching locale? I don't think that's possible as it goes against the intuitive meaning of getProductDetails().

What about a query returning all productDetails' that have your specified locale and then retrieving the product from the productDetails object?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2008 10:00 am 
Newbie

Joined: Mon Dec 22, 2008 10:53 pm
Posts: 3
That might be my only way out for now. Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.