Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I query Hibernate for object where property may be null or specific value?
    primarykey
    data
    text
    <p>I have the following objects I'm working with:</p> <p>RawRead RawRead.Checkpoint</p> <p>Checkpoint.EndCustomer</p> <p>Guard</p> <p>Where Checkpoint and Guard are properties of RawRead, EndCustomer is a property of Checkpoint. All are objects.</p> <hr> <p>My current Hibernate gubbins:</p> <pre><code>Criteria crit = sess.createCriteria(RawRead.class); crit.add( Restrictions.or( Restrictions.eq("checkpoint", null), Restrictions.in("checkpoint.parentEndCustomer",collectionOfEndCustomers) ) ); </code></pre> <p>So Checkpoint can be null, but if it is there I only want the RawRead objects where the parentEndCustomer object is in the checkpoint.parentEndCustomer property.</p> <p>I hope that makes sense.</p> <hr> <p>My guesstimate above produces an error that (to me) suggests that my criteria are incorrectly specified:</p> <pre><code>[Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property: checkpoint.parentEndCustomer of: uk.co.romar.guardian.objects.RawRead] with root cause org.hibernate.QueryException: could not resolve property: checkpoint.parentEndCustomer of: uk.co.romar.guardian.objects.RawRead at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81) at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:96) at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:62) at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1457) at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:483) </code></pre> <p>Relevant bits of RawRead:</p> <pre><code>@ManyToOne @JoinColumn(name="CHECKPOINT_OID") @NotFound(action=NotFoundAction.IGNORE) public Checkpoint checkpoint = null; public void setCheckpoint(Checkpoint in) {this.checkpoint = in;} public Checkpoint getCheckpoint() {return this.checkpoint;} @ManyToOne @JoinColumn(name="GUARD_OID") @NotFound(action=NotFoundAction.IGNORE) private Guard guard = null; public void setGuard(Guard in) {this.guard = in;} public Guard getGuard() {return this.guard;} </code></pre> <p>And from Checkpoint:</p> <pre><code> @ManyToOne @JoinColumn(name="ENDCUSTOMER_OID") @NotFound(action=NotFoundAction.IGNORE) private EndCustomer parentEndCustomer = null; public EndCustomer getParentEndCustomer() {return this.parentEndCustomer;} public void setParentEndCustomer(EndCustomer ownerCustomer) {this.parentEndCustomer = ownerCustomer;} </code></pre> <hr> <h2>EDIT Follows after implementing first answer from below.</h2> <p>If I have some data like this in the database (I hope the notation makes sense!):</p> <pre><code>RawRead { ID=1 checkpoint={id=1,parentEndCustomer={ID=1}} } RawRead { ID=2 checkpoint={id=4,parentEndCustomer={ID=4}} } RawRead { ID=3 checkpoint={id=7,parentEndCustomer={ID=31}} } RawRead { ID=4 checkpoint={null} } </code></pre> <p>and the collectionOfEndCustomers given in the Restriction is like this: EndCustomer={ID=31}</p> <p>The I would want to retrieve RawReads 3 and 4 only. RawRead 1 &amp; 2 are rejected because the parentEndCustomer of the child checkpoint property does't match the one passed in to the restriction in collectionOfEndCustomers.</p> <p>RawRead.3 is should be selected because the parentEndCustomer matches one in the collection passed in. RawRead.4 should be selected because the checkpoint is null.</p> <p>Following the guidance in the first answer below results in all of the above RawReads being returned rather than the subset I'm after.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload