I am trying to use Specification to build an SQL which "should" be very similar to this query below:
select p1_0.* from plans p1_0 inner join plans_vehicle_models p2_0 on p2_0.plan_id = p1_0.plan_id where p2_0.vehicle_model_id in (select v1_0.model_id from vehicle_models v1_0 where v1_0.model_name = 'ModelName')
Having tried everything on head, the resulting output returns:
select p1_0.* from plans p1_0 join plans_vehicle_models p2_0 on p1_0.plan_id=p2_0.plan_id where p1_0.plan_id in((select v1_0.model_id from vehicle_models v1_0 where v1_0.model_name=?)).
So in brief, I want the where
clause to be p2_0.vehicle_model_id
instead of p1_0.plan_id
Below are my codes
Spec Class
@Component
public class PlanSpecification {
public static Specification<Plan> vehicleModelNameExactlyIgnoringCase(String vehicleModelName) {
return ((root, query, criteriaBuilder) -> {
if (vehicleModelName != null) {
Subquery<Long> subquery = query.subquery(Long.class);
Root<VehicleModel> subqueryRoot = subquery.from(VehicleModel.class);
subquery.select(subqueryRoot.get("id"));
subquery.where(criteriaBuilder.equal(criteriaBuilder.upper(subqueryRoot.get("modelName")), vehicleModelName.toUpperCase()));
Join<Plan, PlanToVehicleModelAssignment> plansVehicleModelsJoin = root.join(Plan_.PLANS_VEHICLE_MODELS, JoinType.INNER);
return criteriaBuilder.in(root.get(PlanToVehicleModelAssignment_.ID)).value(subquery);
} else {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "`vehicle_model_name` parameter passed to filter resultset on Plan cannot be null");
}
});
}
}
Plan Model class
@Data
@Entity
@Table(name = "plans")
public class Plan {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "plan_id", nullable = false, unique = true, columnDefinition = "BIGINT")
protected Long id;
@Column(name = "plan_code", unique = true, nullable = false)
protected String code;
@OneToMany(mappedBy = "plan", fetch = FetchType.LAZY)
protected List<PlanToVehicleModelAssignment> planToVehicleModelAssignmentList = new ArrayList<>();
}
PlanToVehicleModelAssignment Model Class
@Table(name = "plans_vehicle_models")
public class PlanToVehicleModelAssignment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "plan_vehicle_model_id", columnDefinition = "BIGINT")
private Long id;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "plan_id", referencedColumnName = "plan_id")
private Plan plan;
@OneToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "vehicle_model_id", referencedColumnName = "model_id", unique = true)
private VehicleModel vehicleModel;
}
Plan_ MetaModel class
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Plan.class)
public abstract class Plan_ {
public static volatile SingularAttribute<Plan, Long> id;
public static volatile SingularAttribute<Plan, String> code;
public static volatile ListAttribute<Plan, PlanToVehicleModelAssignment> planToVehicleModelAssignmentList;
public static final String ID = "id";
public static final String CODE = "code";
public static final String PLANS_VEHICLE_MODELS = "planToVehicleModelAssignmentList";
}
PlanToVehicleModelAssignment_ metamodel class
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(PlanToVehicleModelAssignment.class)
public abstract class PlanToVehicleModelAssignment_ {
public static volatile SingularAttribute<PlanToVehicleModelAssignment, Long> id;
public static volatile SingularAttribute<PlanToVehicleModelAssignment, Plan> plan;
public static volatile SingularAttribute<PlanToVehicleModelAssignment, VehicleModel> vehicleModel;
public static final String ID = "id";
public static final String PLAN = "plan";
public static final String VEHICLE_MODEL = "vehicleModel";
}
I was able to solve the issue by replacing the return line on the Spec class above with return criteriaBuilder.and(criteriaBuilder.in(plansVehicleModelsJoin.get(PlanToVehicleModelAssignment_.VEHICLE_MODEL)).value(subquery));
Working Spec class code:
@Component
public class PlanSpecification {
public static Specification<Plan> vehicleModelNameExactlyIgnoringCase(String vehicleModelName) {
return ((root, query, criteriaBuilder) -> {
if (vehicleModelName != null) {
Subquery<Long> subquery = query.subquery(Long.class);
Root<VehicleModel> subqueryRoot = subquery.from(VehicleModel.class);
subquery.select(subqueryRoot.get("id"));
subquery.where(criteriaBuilder.equal(criteriaBuilder.upper(subqueryRoot.get("modelName")), vehicleModelName.toUpperCase()));
Join<Plan, PlanToVehicleModelAssignment> plansVehicleModelsJoin = root.join(Plan_.PLANS_VEHICLE_MODELS, JoinType.INNER);
return criteriaBuilder.and(criteriaBuilder.in(plansVehicleModelsJoin.get(PlanToVehicleModelAssignment_.VEHICLE_MODEL)).value(subquery));
} else {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "`vehicle_model_name` parameter passed to filter resultset on Plan cannot be null");
}
});
}
}