java


How to sort projection by alias from SELECT clause in Spring Data JPA with pagination?


I created this two entites to demonstrate my problem:
OwnerEntity.java:
#Entity
public class OwnerEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Size(min = 1)
#OneToMany(mappedBy = "ownerEntity", cascade = CascadeType.ALL)
private Set<ChildEntity> childEntities = new HashSet<>();
}
ChildEntity.java:
#Entity
public class ChildEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
#NotNull
#ManyToOne(optional = false)
private OwnerEntity ownerEntity;
public ChildEntity() {
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public OwnerEntity getOwnerEntity() {
return ownerEntity;
}
public void setOwnerEntity(OwnerEntity ownerEntity) {
this.ownerEntity = ownerEntity;
}
}
I want to write a query that will show information from OwnerEntity and one joined ChildEntity. I created a projection:
OwnerEntityProjection.java:
public interface OwnerEntityProjection {
Long getId();
String getName();
}
My JpaRepository:
public interface OwnerEntityRepository extends JpaRepository<OwnerEntity, Long> {
#Query(" SELECT " +
" ownerEntity.id AS id, " +
" childEntities.name AS name " +
" FROM OwnerEntity ownerEntity " +
" JOIN ownerEntity.childEntities childEntities ")
// There must be also WHERE clause, but for demonstration it is omitted
Slice<OwnerEntityProjection> findAllPaginated(Pageable pageRequest);
}
Now when i run this simple test:
#Test
public void findAllPaginatedTest() {
Pageable pageRequest = new PageRequest(0, 3, Sort.Direction.ASC, "name");
Slice<OwnerEntityProjection> OwnerEntityProjectionsPaginated =
ownerEntityRepository.findAllPaginated(pageRequest);
}
I got following error:
org.hibernate.QueryException: could not resolve property: name of: com.example.domain.OwnerEntity
I also checked generated JQPL in logs:
... order by ownerEntity.name asc
As you can see Spring Data Jpa appended first entity alias from FROM clause. I found that if i change PageRequest to this:
new PageRequest(0, 3, Sort.Direction.ASC, "childEntities.name");
It works without errors, but i don't want to pass ordering property to repository with aliases that are somewhere in JPQL query. I want to pass property that is directly present in projection that repository method returns. If i write ORDER BY manually in JPQL query like this:
... ORDER BY name ASC
Then this query also run without any errors because i can reference aliases in SELECT clause from ORDER BY clause.
Is there any way to tell Spring Data Jpa to perform ordering without appending aliases from FROM or JOIN clauses? Something like this:
new PageRequest(0, 3, Sort.Direction.ASC, "name") ===> ORDER BY name asc

Related Links

Unable to find certification path even after adding certificate to cacerts
Turning on gps automatically in map activity [duplicate]
How to force Tomcat doesn't write data (like logs, temp, ets) to its own root folder?
Andengine - detect collisions with multiple objects and delete them - Java
how to make read & unread property messages in jpa
Invalid property 'jobParameters' of bean class StepContext: Bean property 'jobParameters' is not readable or has an invalid getter method
form json request from jpath
My Remove Method isn't Working
Looping one webpage [closed]
Integer division using recursion--and a few other interesting limitations
jOOQ can I fetch a join of two tables into the respective POJOs
Invoking a java function from JSP Script tag
Eclipse Mars How to add plugin DERBY database
Java boolean return if statement [duplicate]
MS Edge test automation - non selenium
File has not been printed Through Printer device in java

Categories

HOME
clips
platform-builder
spring-jdbc
jpeg
survey
sql-server-2016
enterprise-library-5
django-imagekit
facebook-messenger-bot
google-cloud-ml
google-cloud-spanner
jsprit
invantive-sql
http-status-code-504
text-rendering
contextmenu
autoconf
dxf
pepper
saas
instant-messaging
fabric8
junit5
.net-4.0
oracle-fusion-middleware
titanium-mobile
jspresso
column-family
geopositioning
cloud-code
serve
scaffold
dynamics-crm-2013
streamreader
unoconv
isbn
modelmapper
.net-4.6.2
segment
auto-update
jquery-validate
svmlight
lowpass-filter
setter
sqlclient
epson
account-kit
gnome-shell-extensions
xcb
probability-density
storekit
contact-list
jwplayer7
bgp
captivenetwork
cloudbees
energy
iad
marching-cubes
mutation-observers
wordml
vstest.console.exe
csquery
website-monitoring
nsbutton
client-side-templating
android-radiobutton
aapt
navigationservice
fluentautomation
prettify
buster.js
sabredav
delphi-6
browser-detection
chrono
ohm
asyncfileupload
subscript
zpt
xtype
subviews
jquery-ui-droppable
nsviewanimation
microsoft-virtualization
ctp4
3gp

Resources

Encrypt Message