Problem with Hibernate/JPA Query and categories -


i have hibernate/jpa data model lets me place objects (myobj) various categories (mycategory). each category may have 0 or more subcategories, , categories not instead have classification (myclassification) assigned them. data model looks this:

public class myobj {      …      protected mycategory category = null;      … }  public class mycategory {      …      protected myclassification classification=null;      protected list<mycategory> childcategories=null;      protected mycategory parentcategory=null;      …  }  public class myclassification {      …  } 

i'd able query myobj instances based on classification, category, or subcategory. example, if have 3 classifications (classa, classb, , classc), , 6 categories (e.g. categorya1, categorya2, categoryb1, etc. name corresponds classification) , each of categories have 3 subcategories (e.g. subcata1, subcata2, subcata3, subcatb1, etc.) i'd queries this:

  • all myobj instances in classification classa (regardless of category or subcategory)
  • all myobj instances in category categorya1 (regardless of subcategory)

i've created couple of namedqueries thought accomplish this. work fine instances myobj instance has been assigned subcategory. however, if place myobj instance in category (no subcategory) , query based on class, don't see it. see myobj instances inside of subcategory. queries this:

myobj.findbyclass = "select distinct o myobj o (o.category.classification = :classification or o.category.parentcategory.classification = :classification)"  myobj.findbycategory = "select distinct o myobj o (o.category = :category or o.category.parentcategory = :category)" 

can tell me error in logic these queries? there better way accomplish i'm after?

the problem query o.category.parentcategory written inner join category cat on cat.id=o.parent_category, , if parentcategory null won't result.

so dot-navigation implies inner joins causes problems or statements. must use explicit left joins or union queries.

on sidenote, querydsl allows write statements this:

from(qcategory.category) .where(qcategory.category.classification.eq(classification).or(qcategory.category.left().parentcategory().left().classification.eq(classification)) .listdisticnt(qcategory.category) 

select distinct o myobj o (o.category.classification = :classification or o.category.parentcategory.classification = :classification)"


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -