zemirco
zemirco

Reputation: 16395

Trying to understand this weird Java / hibernate-jpamodelgen behaviour

I'm trying to understand a weird behaviour related to hibernate-jpamodelgen but maybe more to Java in general.

I have entities and I generate code via hibernate-jpamodelgen. That all works but then I tried something that cost me a few days and a lot of headaches.

Here is the version that works.

public interface CourseRepository extends JpaRepository<Course, Integer> {

    @EntityGraph(attributePaths = {Course_.TOPIC + "." + Student_.NAME})
    List<Course> findAll();

}

I thought I would make the code a bit more beautiful and put the String Course_.TOPIC + "." + Student_.NAME into a variable.

public interface CourseRepository extends JpaRepository<Course, Integer> {

    // this is new
    static final String TOPIC_NAME = Course_.TOPIC + "." + Student_.NAME;
    
    @EntityGraph(attributePaths = {Course_.TOPIC + "." + Student_.NAME})
    List<Course> findAll();

}

That still works but as soon as I use this variable TOPIC_NAME I cannot compile anymore.

public interface CourseRepository extends JpaRepository<Course, Integer> {

    static final String TOPIC_NAME = Course_.TOPIC + "." + Student_.NAME;
    
    // this DOES NOT work
    @EntityGraph(attributePaths = {TOPIC_NAME})
    List<Course> findAll();

}

When I try to compile via gradle compileJava I'm suddenly getting errors like

  symbol:   variable Student_
  location: interface StudentRepository
/src/main/java/com/example/demo/Course/CourseRepository.java:12: error: cannot find symbol
    static final String TOPIC_NAME = Course_.TOPIC + "." + Student_.NAME;
                                     ^
  symbol:   variable Course_
  location: interface CourseRepository
/src/main/java/com/example/demo/Course/CourseRepository.java:12: error: cannot find symbol
    static final String TOPIC_NAME = Course_.TOPIC + "." + Student_.NAME;

Why is that the case? I'm happy that I found the issue but I don't understand why this happens.

Thank you very much!

Upvotes: 0

Views: 135

Answers (0)

Related Questions