ardnaxela
ardnaxela

Reputation: 61

Spring Roo 2.0.0.RC1 - How can i add a finder over two or more tables

I wan't to generate with Roo a finder over two or more tables. Example clinic i want add a finder that get all owner with have a pet with given name or all pets with have the owner with given name.

Is this possible in Roo?

Upvotes: 1

Views: 156

Answers (1)

jcgarcia
jcgarcia

Reputation: 3882

Spring Roo provides a finder command that allows you to include new finders in the repository interface. When Spring Data Jpa detects these methods in the interface, it uses the method names to create the queries. Check:

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation

However, the query you want to create contains a relation and that is not supported using named queries, so you need to use QueryDSL to construct your own query. Follow the next steps:

  • Create new method public List<Owner> findAllByPetName(String name); in the OwnerRepositoryCustom.java interface.
  • Implements the method above in the OwnerRepositoryImpl.java class like the following one:

    public List<Owner> findAllByPetName( String name ) { 
     QOwner owner = QOwner.owner;
     QPet pet = QPet.pet;          
     JPQLQuery<Owner> query = from( owner )
        .leftJoin( owner.pets, pet)
        .fetchJoin()
        .where( pet.name.eq( name ) );
    
     applyOrderById( query );
     return query.fetch();
    
    }
    

As you could see, using QueryDSL is really easy to implement your own queries. Repeat the steps above with the Pet repository to obtain all the pets assigned to an Owner with an specific name.

Check more info about QueryDSL here:

http://www.querydsl.com/

Hope it helps.

Upvotes: 2

Related Questions