Cosium / spring-data-jpa-entity-graph

Spring Data JPA extension allowing full dynamic usage of EntityGraph on repositories
MIT License
469 stars 52 forks source link

Generated query columns not effected by the entity graph #24

Closed omar-f-nassar closed 5 years ago

omar-f-nassar commented 5 years ago

In a spring boot application with spring data jpa entity graph, I am extending the EntityGraphCrudRepository and also tried EntityGraphJpaRepository, the generated query columns is not effected by the passed entity graph object (all the columns still returned).

Kindly check the generated query in the comment block below in the sample application:

///////////////// Person class

@Data
@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private int age;
}

///////////////// PersonDAO interface

public interface PersonDAO extends EntityGraphJpaRepository<Person, Long>{
    public List<Person> findByNameLike(String name, EntityGraph eg);
}

///////////////// EntityGraphTestApplication  class

@SpringBootApplication
@EnableJpaRepositories(repositoryFactoryBeanClass = EntityGraphJpaRepositoryFactoryBean.class)
public class EntityGraphTestApplication implements CommandLineRunner{

    @Autowired
    private PersonDAO dao;

    public static void main(String[] args) {
        SpringApplication.run(EntityGraphTestApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Person p1 = new Person();
        p1.setAge(10); 
        p1.setName("person1");

        Person p2 = new Person();
        p2.setAge(10); 
        p2.setName("person2");

        Person p3 = new Person();
        p3.setAge(10); 
        p3.setName("person3");

        dao.save(p1);
        dao.save(p2);
        dao.save(p3);

        dao.findAll(EntityGraphUtils.fromAttributePaths("name", "age"));
        /*  -- Generated query below:
         * 
         * 
            select
                person0_.id as id1_0_,
                person0_.age as age2_0_,
                person0_.name as name3_0_ 
            from
                person person0_
         * 
         * 
         * */

        dao.findByNameLike("person1", EntityGraphUtils.fromAttributePaths("name"));
        /*  -- Generated query below:
         * 
            select
                person0_.id as id1_0_,
                person0_.age as age2_0_,
                person0_.name as name3_0_ 
            from
                person person0_ 
            where
                person0_.name like ?
         * 
         * 
         * */           
    }
}

Dependencies:

    <!-- Spring data jpa entity graph-->
    <dependency>
        <groupId>com.cosium.spring.data</groupId>
        <artifactId>spring-data-jpa-entity-graph</artifactId>
        <version>2.0.7</version>
    </dependency>

    <!-- H2 database -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

application.yml:

spring:
  h2:
    console:
      enabled: true
    datasource: 
      url: jdbc:h2:mem:testdb
      username: sa
      password:
      driverClassName: org.h2.Driver

  jpa:
    generate-ddl: true
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        dialect: org.hibernate.dialect.H2Dialect
        format_sql: true

logging:
  level:
    root: warn
    org:
      hibernate:
        SQL: debug
        type: trace
      springframework:
        web: debug
reda-alaoui commented 5 years ago

Hi Omar,

As specified by JPA 2.1, EntityGraph only affects the fetch policy on associated entities. Thus affect only the load of foreign table columns. In your example, Person holds no association. So what you are seeing is normal.

Whatever you do, when you ask JPA to find Person, all person table columns will always be part of the generated query.