First I would like to see if I could solve my problem with JPQL and fetching r.origin and r.destination as objects or understand why it is not possible to do what I am trying.
This is my model:
@Entity(name = "products")
@Table(
name = "products",
indexes = {
@Index(name = "fk_idx_product_receptacle", columnList = "receptacle", unique = false),
@Index(name = "fk_idx_product_shipment", columnList = "shipment", unique = false),
}
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_id_seq")
@SequenceGenerator(name = "product_id_seq", sequenceName = "product_id_seq")
private Long id;
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "receptacle")
private Receptacle receptacle;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shipment")
private Shipment shipment;
...
@Entity(name = "receptacles")
@Table(
name = "receptacles",
indexes = {
@Index(name = "fk_idx_receptacle_origin", columnList = "origin", unique = false),
@Index(name = "fk_idx_receptacle_destination", columnList = "destination", unique = false),
}
)
public class Receptacle {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "receptacle_id_seq")
@SequenceGenerator(name = "receptacle_id_seq", sequenceName = "receptacle_id_seq")
private Long id;
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "origin")
private Location origin;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "destination")
private Location destination;
...
@Transient
private long numProducts;
Please NOTE: I am not using @OneToMany mapping and I am not using Spring
I also tried passing r to the constructor:
em.createQuery("SELECT "
+ "new burro.model.Receptacle(r, COUNT(p.id)) "
+ "FROM products p "
+ "JOIN p.receptacle r "
+ "JOIN r.origin o "
+ "JOIN r.destination d "
+ "WHERE r.closeDate IS NOT NULL "
+ "AND r.dispatchDate IS NULL "
+ "AND r.action = 'RTS' "
+ "AND o.id = :origin "
+ "GROUP BY r.id", Receptacle.class)
.setParameter("origin", issuer.getId())
.getResultList();
That query is not throwing an Exception but the constructor gets an org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor@1641aeb object
and hibernate generates multiple queries which looks like I not doing things right:
select receptacle1_.id as col_0_0_, count(product0_.id) as col_1_0_ from products product0_ inner join receptacles receptacle1_ on product0_.receptacle=receptacle1_.id inner join locations location2_ on receptacle1_.origin=location2_.id inner join locations location3_ on receptacle1_.destination=location3_.id where (receptacle1_.closeDate is not null) and (receptacle1_.dispatchDate is null) and receptacle1_.action='RTS' and location2_.id=? group by receptacle1_.id , receptacle1_.connote , receptacle1_.portDestination , receptacle1_.merchant , receptacle1_.origin , receptacle1_.destination , receptacle1_.action , receptacle1_.dispatchDate , receptacle1_.masterCrossDock , receptacle1_.closeDate , receptacle1_.deadWeight , receptacle1_.volumeWeight , receptacle1_.completionDate , receptacle1_.notes , receptacle1_.meta , receptacle1_.creationDateTime
select ... from receptacles receptacle0_ where receptacle0_.id=?
select ... from receptacles receptacle0_ where receptacle0_.id=?
select ... from receptacles receptacle0_ where receptacle0_.id=?
Another issue with this approach is that the select query is neither fetching r.origin nor r.destination