The schema: (psuedocode)
I have a bean, called BaseEntity...
@Entity class BaseEntity { @OneToMany @CascadeType.ALL List [Property] properties; //the use angled braces ommited for the stackoverflow editor to show up properly }
Property is another bean...
@Entity class Property { @ManyToOne Category category; @OneToOne Value value; }
Value is actually an abstract class with Inheritence.SingleTable and subclasses like NumericalValue and DateValue, etc, as well as (in the abstract Value class) a @ManyToOne BaseType type.
The goal:
I am trying to write a query that selects BaseEntity objects that have a Property that has a Category of a certain name, and select several of them, getting objects that have any of the given properties and getting null in fields that don't exist.
The attempt:
select entity.id as id, foo as foo, bar as bar from BaseEntity entity, Value foo, Value bar where foo in (select p.value from Property p where p in elements(entity.properties) and p.category.name = 'FOO') or bar in (select p.value from Property p where p in elements(entity.properties) and p.category.name = 'BAR')
This query DOES run. Currently there is one BaseEntity in the database that matches and I get it many times over with with the correct result for foo, which it does contain, but that same entitiy over and over again with many values in the bar field.
Also, it takes like FIVE MINUTES to run and holds up everyone else using the database.
Ideas:
Of course I have considered just using some kind of distinct, but that doesn't address the extreme time it takes to run, and I just don't quite understand what's going on.
I was hoping you, my peers, could suggest a better query approach. Thank you so much! Joshua