spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
3.02k stars 1.42k forks source link

Upgraded to Hibernate 6.2.7 along with Spring 3 - Inner joins added along with Left join #3577

Closed PreethiAsipu closed 3 months ago

PreethiAsipu commented 3 months ago

After upgrading to Hibernate 6.2.7, Inner joins are added along with left joins

**Entity classes:**

public class AAContractStaging {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "aa_contract_staging_id")
private Long aaContractStagingId;

@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "job_role_id")
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private JobRole jobRole;

@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "contract_status_id", referencedColumnName = "contract_status_id")
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private ContractStatus contractStatus;
}

public class ContractStatus {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "contract_status_id")
private Long contractStatusId;

@Column(name = "contract_status_name")
private @NotNull String contractStatusName;

}

public class JobRole{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "job_role_id")
private Long roleId;

@Column(name = "job_role_description")
private String roleName;
}

**Code implementation:**

@Override
public Predicate toPredicate(final Root root, final CriteriaQuery<?> query,
final CriteriaBuilder cb) {
final Collection predicates = new ArrayList<>();
final List order = new ArrayList<>();
final Join<AAContractStaging, JobRole> jobRoleJoin = root.join(JOB_ROLE, JoinType.LEFT);
final Join<AAContractStaging, ContractStatus> contractStatusJoin = root.join(CONTRACT_STATUS,
JoinType.LEFT);
predicates.add(jobRoleJoin.get(ROLE_ID).in(roleIds));
predicates.add(contractStatusJoin.get(CONTRACT_STATUS_ID).in(contractStatusIds));

final Order[] orders = new Order[order.size()];
query.orderBy(order.toArray(orders));
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };

Hibernate 5 :

select aacontract0_.aa_contract_staging_id as aa_contr12, aacontract0_.contract_status_id as contrac272, aacontract0_.job_role_id as job_rol302 from AssociateManagement.aa_contractstaging aacontract0 left outer join job_roleallowed jobrole3 on aacontract0_.job_roleid=jobrole3.job_role_id left outer join int_domain.contractstatus contractst4 on aacontract0_.contract_statusid=contractst4.contract_statusid where aacontract0.job_roleid in (406) and (aacontract0.contract_status_id in (2))

Response : Getting expected Output

Hibernate 6.2.7:

1. Inner joins are added along with left joins select as1_0.aa_contract_staging_id,as1_0.contract_status_id,as1_0.job_role_id from AssociateManagement.aa_contract_staging as1_0 left join job_role_allowed jr1_0 on jr1_0.job_role_id=as1_0.job_role_id left join int_domain.contract_status cs1_0 on cs1_0.contract_status_id=as1_0.contract_status_id join job_role_allowed jr2_0 on jr2_0.job_role_id=as1_0.job_role_id join int_domain.contract_status cs2_0 on cs2_0.contract_status_id=as1_0.contract_status_id where jr1_0.job_role_id in (406) and cs1_0.contract_status_id in (2)

No changes in entity or the code logic, just upgrade Spring to 3 and Hibernate to 6.2.7 version where Left joins are not working as expected.

Would be helpful if any insights are provided.

Thanks in advance

PreethiAsipu commented 3 months ago

Working as expected