1

I want to ignore default Join Restriction in createAlias. I have a OnetoOne relationship.

My Problem is Hibernate generates default restriction for the join relationship.

Pojo

Note : No column for diagnosticTemplate in charge table.

Charge.java

 @OneToOne(mappedBy = "charge")
 private DiagnosticTemplate diagnosticTemplate;

DiagnosticTemplate.java

@OneToOne
@JoinColumn(name = "charge")
@Exclude
private Charge charge;

Query

select 
  * 
from 
  charges c 
inner join diagnostic_template dt 
  on (dt.charge = c.id and dt.status=1) or (dt.status=0)

Criteria

Criteria criteria = getSession().createCriteria(Charge.class, "charge")
      .createAlias("charge.diagnosticTemplate", "diagnosticTemplate",
        JoinType.INNER_JOIN,
        Restrictions.or(
           Restriction.and(Restrictions.eqProperty("charge.id", 
                         "diagnosticTemplate.charge"), 
           Restrictions.eq("diagnosticTemplate.status",true)),
           Restrictions.eq("diagnosticTemplate.status",false)  ))

Hibernate Query

select 
  * 
from 
  charges c 
inner join diagnostic_template dt 
  on dt.charge = c.id and (dt.charge = c.id and dt.status=1) or (dt.status=0)

How to avoid this condition? or anything wrong with my relationship?

Please help..!

Aravinthan K
  • 1,763
  • 2
  • 19
  • 22

1 Answers1

0

When you join a charge with charge.diagnosticTemplate, it means that Hibernate will try to lookup a DiagnosticTemplate that is linked with this charge. Thus, the generated condition dt.charge = c.id does make sense.

Remember that you have define the relationship using foreign key. So, soft relation like dt.status=0 cannot be understood by Hibernate.

If you still wish to achieve you query, you can consider joining the two instance indirectly (not using association path). Refer to How to join Multiple table using hibernate criteria where entity relationship is not direct?.

Duong Nguyen
  • 830
  • 6
  • 10