I have change the clinic example and add an entity projection for pets.
entity projection --class ~.domain.PetInfo --entity ~.domain.Pet --fields id,name,type,birthDay,owner --entityFormatExpression "#{name} (#{type})"
repository jpa --entity ~.domain.Pet --interface ~.repository.PetRepository --defaultReturnType ~.domain.PetInfo
I have remove all finder for Pets. The finder must change use PetInfo as return type -so i have remove them to test only entity projection.
Before i have added entity projection the list show all entries in Table Pets with and without set owner.
When i added entity projection in list only show items which have owner not null.
I use mysql db and change settings:
// Create the Spring Boot application
project setup --topLevelPackage org.springframework.roo.clinic --projectName clinic_new --java 8
// Setup the data access layer
// jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
jpa setup --provider HIBERNATE --database MYSQL --databaseName clinic --userName root --password
// --force funktioniert nicht also erst löschen un dann neu hinzufügen
property remove --key spring.datasource.url
property add --key spring.datasource.url --value jdbc:mysql://localhost:3307/clinic --force
property add --key spring.jpa.properties.hibernate.hbm2ddl.auto --value update
property add --key spring.jpa.properties.hibernate.dialect --value org.hibernate.dialect.MySQL5Dialect
property add --key spring.jpa.hibernate.naming.physical-strategy --value org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
property remove --key spring.messages.encoding
property add --key spring.messages.encoding --value UTF-8
property add --key spring.jpa.hibernate.show-sql --value true
Spring Roo generates the following code in the PetRepositoryImpl_Roo_Jpa_Repository_Impl.aj
file:
JPQLQuery<Pet> query = from(pet);
[...]
return loadPage(query, pageable, Projections.constructor(PetInfo.class, pet.id, pet.name, pet.type, pet.owner ));
By default, the loadPage
function will generate an INNER JOIN
with the owner table, so only the pet elements that have relation with Owner
will be showed.
To prevent that, you should make push-in of the findAll
method and define a leftJoin
statement.
JPQLQuery<Pet> query = from(pet).leftJoin(pet.owner);
With that, you should be able to obtain all the pets with relations and without relations.
Hope it helps,