2

In the code below.. There are two Alias as Entity Object Reference. Sometimes "caseStage" as stage can be null in Database. When "caseStage" is null I want stage.name value as an empty String or something customized like "---" etc.

session.createCriteria(CaseMasterPO.class) .createAlias("branch", "br") // BranchPO.class .createAlias("caseStage", "stage") // CaseStagePO.class .setProjection(Projections.projectionList() .add(Projections.property("caseCode")) .add(Projections.property("br.zoneCode")) .add(Projections.property("stage.name")) // Problem, when stage == null ) .add(Restrictions.eq("caseCode", caseCode)).uniqueResult(); 
6
  • I usually use coalesce(stage.name, '---') in HQL, so is there any mechanism for Criteria Query ? Commented Nov 21, 2016 at 7:30
  • what is the problem you are facing .. please explain that ?? Commented Nov 21, 2016 at 8:13
  • How you display your objects on the screen shouldn't be decided and specified in a database query. Modify your presentation-layer code to display whatever you want in case the value is null. Commented Nov 21, 2016 at 9:22
  • Can't you just use Projections.sqlProjection instead? Commented Nov 21, 2016 at 9:24
  • @Zulfi, In the above code there is a Projections with property name stage.name. I do not get the rows whenstage is null. in database. Commented Nov 22, 2016 at 8:47

2 Answers 2

7

Set the CriteriaSpecification.LEFT_JOIN to alias function

.createAlias("branch", "br" , CriteriaSpecification.LEFT_JOIN) .createAlias("caseStage", "stage", CriteriaSpecification.LEFT_JOIN)

Sign up to request clarification or add additional context in comments.

1 Comment

Dear bro, is there any way to use CriteriaSpecification.LEFT_JOIN with DetachedCriteria ?
2

@Koti Eswar answer is right, but it's deprecated now.

It's should be done like this:

 join('branch', JoinType.LEFT) createAlias('branch', 'br') 

Tested in Grails 3.3.5

1 Comment

Currently I am using JoinType.LEFT_OUTER_JOIN

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.