JPA Query Left Join with A non-declared relation - jpa

I need to query an Entity with a left join with another entity. But I can only declare the ManyToOne relation on the left table:
First table:
#Entity
public class ShipyardModel {
private Long id
....etc
}
Second table (LEFT one):
#Entity
public class Boat{
private Long id;
...
#ManyToOne
#JoinColumn(name = "shipyard_model_id")
private ShipyardModel shipyardModel;
}
Because of serialization reasons I can't declare the #OneToMany relation in the first entity. But they are related, because in the Boat table, i have the shipyard_mode_id. So I created a DTO to query this guy for me and created a JPA query:
Query(" SELECT new br.com.easymarine.dto.ShipyardModelDTO" +
"(sm.id, sm.name, sm.commercialLength, sm.length, sm.width, sm.height, sm.beam, sm.weightWithoutMotor, sm.weightWithMotor, sm.modelYear, sm.shipType,s.id, s.name,count(b.id)) " +
"from ShipyardModel sm " +
"join sm.shipyard s " +
"left join Boat b on b.shipyardModel = sm GROUP BY sm.id")
List<ShipyardModelDTO> findAllShipyardModelDTO();
The Spring is giving me
Path expected for join!
error
Is it possible to execute this query the way it is?

Related

EclipseLink ManyToOne - CriteriaBuilder Generated Query is Wrong

I have an Entity with a ManyToOne Relationship to the Primary Key of another entity. When I create a query that references this Foreign Key eclipseLink always creates a join instead of simply accessing the Foreign Key.
I have created a highly simplified example to show my issue:
#Entity
public class House {
#Id
#Column(name = "H_ID")
private long id;
#Column(name = "NAME")
private String name;
#ManyToOne
#JoinColumn(name = "G_ID")
private Garage garage;
}
#Entity
public class Garage{
#Id
#Column(name = "G_ID")
private long id;
#Column(name = "SPACE")
private Integer space;
}
I created a query that should return all houses that either have no garage or have a garage with G_ID = 0 using the CriteriaBuilder.
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<House> query = cb.createQuery(House.class);
Root<House> houseRoot = query.from(House.class);
Path<Long> garageId = houseRoot.get(House_.garage).get(Garage_.id);
query.where(cb.or(cb.equal(garageId , 0), cb.isNull(garageId)));
TypedQuery<House> typedQuery = entityManager.createQuery(query);
List<House> houses = typedQuery.getResultList();
The generated query is:
SELECT h.NAME, h.G_ID FROM HOUSE h, GARAGE g WHERE (((h.G_ID= 0) OR (g.G_ID IS NULL)) AND (g.G_ID = h.G_ID));
I don't understand why
The or condition first references table HOUSE and then GARAGE (instead of HOUSE)
The join is created in the first place.
The correct query should look like this in my understanding:
SELECT h.NAME, h.G_ID FROM HOUSE h WHERE (((h.G_ID= 0) OR (h.G_ID IS NULL));
Or if a join is made it should take into account that the ManyToOne relationship is nullable and therefore do a LEFT OUTER JOIN.
SELECT h.NAME, h.G_ID FROM HOUSE h LEFT OUTER JOIN GARAGE g ON (h.G_ID = g.G_ID ) WHERE (h.G_ID = 0) OR (g.G_ID IS NULL);
(Note both these queries would work correctly in my more complicated setup. I also get the same error when only wanting to retrieve all houses that have no garage.)
How can I achieve this (while still using the CriteriaBuilder and ideally not having to change the DB Model)?
(Please let me know any additional information that might be required, I'm very new to this topic and came across this issue while migrating an existing application.)
-- edit --
I have found a solution to my problem that will result in slightly different behaviour (but in my application that part of the code I had to migrate didn't make much sense in the first place). Instead of using
Path<Long> garageId = houseRoot.get(House_.garage).get(Garage_.id);
I use
Path<Garage> garage = houseRoot.get(House_.garage);
And then as expected table Garage isn't joined anymore. (I assume the code previously must have been some kind of hack to get the desired behaviour from openJPA)
I don't understand why
The or condition first references table HOUSE and then GARAGE (instead of HOUSE)
I believe this is implementation specific; in any case, it shouldn't have any bearing on the results.
The join is created in the first place.
By saying Path<Long> garageId = houseRoot.get(House_.garage).get(Garage_.id) you're basically telling EclipseLink: 'join Garage to House, we're gonna need it'. That you then access Garage_.id (and not, for example, Garage_.space) is inconsequential.
If you don't want the join, simply map the G_ID column one more time as a simple property: #Column(name = "G_ID", insertable = false, updatable = false) private Long garageId. Then refer to House_.garageId in your query.
Or if a join is made it should take into account that the ManyToOne relationship is nullable and therefore do a LEFT OUTER JOIN.
Path.get(...) always defaults to an INNER JOIN. If you want a different join type, use Root.join(..., JoinType.LEFT), i. e. houseRoot.join(House_.garage, JoinType.LEFT).get(Garage_.id).
One solution that results in the same behaviour is:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<House> query = cb.createQuery(House.class);
Root<House> houseRoot = query.from(House.class);
Path<Garage> garage = houseRoot.get(House_.garage);
Path<Long> garageId = garage.get(Garage_.id);
query.where(cb.or(cb.equal(garageId , 0), cb.isNull(garage)));
TypedQuery<House> typedQuery = entityManager.createQuery(query);
List<House> houses = typedQuery.getResultList();
This results in the following SQL:
SELECT H_ID, NAME, G_ID FROM HOUSE WHERE ((G_ID = 0) OR (G_ID IS NULL));

QueryDSL Left Join not mapping properly with JPA entity

QueryDSL is not working properly with JPA for left join.
I am using queryDSL version 4.2.1 and mapping the response directly to javax.persistence entity.
For Left join/Right join, the joining condition it's not working. It fetches every entity disregarding the joining condition(here it's name = "testName") when entity1.getEntity2() is being called.
Is there any other way applicable for this case to map the result after JOIN tables ?
JPAQuery<Entity1> query = new JPAQuery<>(entityManager);
query.from(table1);
query.leftJoin(table2).on(table2.id.eq(table1.id).and(table2.name.eq("testName"));
List<Entity1> list = query.fetch();
#Entity
public class Entity1{
private Integer id;
#OneToMany(mappedBy = "entity1", fetch = FetchType.LAZY)
private List<Entity2> entity2;
}
An left or right join is an outer join.
So if you use left join all records from the left (in your case table1) will be selected.
If you only want records from table1 if there are corresponding records on table2 you have to use innerJoin.
JPAQuery<Entity1> query = new JPAQuery<>(entityManager);
query.from(table1);
query.innerJoin(table2).on(table2.id.eq(table1.id).and(table2.name.eq("testName"));
List<Entity1> list = query.fetch();
Read more about the join types here:
https://www.diffen.com/difference/Inner_Join_vs_Outer_Join

Rewrite SQL query with JOINS in JPA

I have this SQL query for MariaDB.
select #ref:=id as id, unique_id, reference_id
from mytable
join (select #ref:=id from mytable WHERE unique_id = 55544)tmp
where reference_id=#ref
https://www.db-fiddle.com/f/jKJodfVfvw65aMaVDyFySd/0
How this query can be implemented in HQL query? I would like to use it in JPA?
(Answer largely re-written after comments below)
JPA doesn't have built-in support for hierarchical queries. The main option is a native query.
E.g. with this entity class:
#Entity
public class MyTable {
#Id
#GeneratedValue
private int id;
private int uniqueId;
#ManyToOne
private MyTable reference;
// ... getters and setters ...
}
The following is an example of a native hierachical SQL query (actually against MySQL, just in case):
Query query = entityManager.createNativeQuery(
"select #ref\\:=id as id, unique_id, reference_id\r\n" +
"from my_table\r\n" +
"join (select #ref\\:=?)tmp\r\n" +
"where reference_id=#ref",
MyTable.class);
query.setParameter(1, 1);
query.getResultList();
This was chasing down a chain of references successfully.
(Other alternatives)
There probably aren't too many other options that can do this as a single query. If scalability is less of a concern, adding a back reference would be a simple way to navigate the model:
#OneToMany(mappedBy = "reference")
private Set<MyTable> backReferences;
Those would then be straightforward to recursively navigate. Clearly the relation defaults to lazy loading, so would add little overhead until used.
With #df778899's MyTable in spring-data it could look like:
#Repository
public interface MyRepository extends ...
#Query("select #ref:=id as id, unique_id, reference_id "+
"from mytable join (select #ref:=id from mytable WHERE unique_id = :pUid) tmp "+
"where reference_id=#ref", //just copy paste the query, use :pUid instead of constant...
nativeQuery = true) // and this!
List<MyTable> myCustomHirachicalQuery(#Param("pUid") Integer uid/*String/Long/...*/);
...

Spring Data+JPA: enforce inner join for OneToOne relationship

I have an Entity with OneToOne relation, which is used just to sort results:
#Entity
public class Document {
#Id
Long id;
#OneToOne()
SortProperty sortProp;
...
}
Then I have repository (using QueryDSL predicates):
public interface DocumentRepository
implements PagingAndSortingRepository<Document, Long>,
QueryDslPredicateExecutor<Document> {
#EntityGraph(value = "Document.forceJoins")
Page<Document> findAll(Predicate queryDslPredicate, Pageable pageable);
...
}
As you see above, I use #EntityGraph to control joining relations in the main query. All this work well, the only problem is performance - #OneToOne is fetched vith left outer join which means that DB index is not used:
select * from
document document0_
left outer join
sortproperty sortproper3_
on document0_.documentid=sortproper3_.documentid
...
Is there any way how to enforce using inner join instead of left outer join?
I have tried several things - #OneToOne(optional = false), #org.hibernate.annotations.Fetch, but no success ... Parts generated from QueryDSL predicate(s) use properly inner joins for the property, but the main part of query use always left outer join. I was trying also use annotation with this method:
#Query("select doc from Document doc inner join doc.sortProperties props")
but I was unable to use it properly together with paging and QueryDSL predicates.
Any idea?
Try this with #Query annotation.
#Query("select doc from Document doc join doc.sortProp props")

Finding an inherited entity over a jointable with NamedQuery

How can I use a NamedQuery to find an entity over a jointable?
I have an abstract parent class/entity with #Inheritance(strategy=InheritanceType.JOINED) and two subclasses/subentities.
Hence, in the database I have a parent table (sdrs) and two subtables (xSdrs and ySdrs).
There is another table reservations which shall have a Many-to-Many relationship to table sdrs. That's why I created a jointable between reservations and sdrs.
I intend to have a NamedQuery in the parent entity Sdr to be able to find the key for a record/entity of XSdr or YSdr respectively over the jointable.
In class Sdr I have:
#NamedQuery(name="Sdr.findBySdrId", query="SELECT s FROM Sdr s "
+ "INNER JOIN s.reservations res WHERE res.sdrs = :transactionId")
and
#ManyToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
#JoinTable(name="sdrs_has_reservations",
joinColumns = {
#JoinColumn(name="sdrs_id", referencedColumnName="sdrId", nullable=false)
}, inverseJoinColumns={
#JoinColumn(name="reservations_id", referencedColumnName="reservationId", nullable=false)
})
private List<Reservation<T>> reservations;
But, of course, this sets the transactionId to reservationId which consequentially fails.
On the inverse side I have:
#ManyToMany(mappedBy="reservations", fetch=FetchType.EAGER)
private List<Sdr<T>> sdrs;
So, how do I have to implement the named query in class/entity Sdr to be able to get the proper Sdr (and its related reservations) with an Sdr ID to be set as query parameter?
SELECT sdr FROM Reservation r JOIN r.sdrs sdr WHERE sdr.id=:id