Skip to content

NoSuchFieldException: Property not found when declare private access modifier on getter method #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SinaiNIN opened this issue Aug 29, 2019 · 6 comments · Fixed by #173

Comments

@SinaiNIN
Copy link
Contributor

There is an error what throwing NoSuchFieldException when declare private access modifier on getter method.

Error

 WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customGraphqlExecutor' defined in file [E:\core\core\build\classes\java\main\com\mbanq\graphql\CustomGraphqlExecutor.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'graphQLSchemaFactoryBean' defined in class path resource [com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.introproventures.graphql.jpa.query.autoconfigure.GraphQLSchemaFactoryBean]: Factory method 'graphQLSchemaFactoryBean' threw exception; nested exception is java.lang.RuntimeException: java.lang.NoSuchFieldException: age
 INFO o.s.o.j.LocalContainerEntityManagerFactoryBean Closing JPA EntityManagerFactory for persistence unit 'jpa-pu' 
Aug 29, 2019 10:44:41 AM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service [Tomcat]

Example of Code

public class Student{

    @Temporal(TemporalType.DATE)
    @Column(name = "DOB")
    private Date DateOfBirth;

    @Column(name ="age")
     private Integer age;

     private Integer getAge(){
           return Period.between(LocalDate.now(), dob).getYears();
     }
}
SinaiNIN added a commit to SinaiNIN/graphql-jpa-query that referenced this issue Aug 29, 2019
…not found - because declared private access modifier on getter method.
SinaiNIN added a commit to SinaiNIN/graphql-jpa-query that referenced this issue Aug 29, 2019
…found when declared private access modifier on getter method
@anotender
Copy link
Contributor

@igdianov @SinaiNIN it is probably a similar concern to the one I mentioned in this comment. It is a question whether we really want to throw an exception in such case?

@igdianov
Copy link
Collaborator

igdianov commented Aug 29, 2019

@SinaiNIN I have created a test case to try reproduce the issue. Feel free to create a PR into #175 branch with your test cases.

@anotender I think that not throwing exceptions like that will hide problems, i.e. when using wrong attribute name for introspection. It is better to fail fast to be aware of any such problems. These problems will go away when we accumulate good test coverage.

@anotender
Copy link
Contributor

@igdianov I've investigated a little bit the NoSuchFieldException that is thrown during startup with our app's entity model and here are my observations:

  1. The exception is thrown when there is a protected getter in the entity. Such entry exists in the metamodel but is not introspected by the IntrospectionUtils as it takes public properties into consideration only.
  2. Very similar to the one above but the non-public property comes from the superclass.

I am not able to reflect such issue in tests by amending the entity model.
In my opinion it is good to throw an exception but we should catch it, log properly as a warn or error and ignore such property but still allow the application to start so it is not a blocker.
I am writing it from a position of a legacy system dev where I cannot change some structures unfortunately.

@igdianov
Copy link
Collaborator

I have found out how Hibernate handles property and field access: https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/property/access/internal/PropertyAccessMixedImpl.java

I think will modify the IntrospectionUtils to follow Hibernate property access resolution strategy. It should solve the problem.

@igdianov
Copy link
Collaborator

igdianov commented Sep 1, 2019

@anotender I had refactored persistent property detection in this PR: #173

Please, take a look and let me know if it resolves your issue.

@igdianov
Copy link
Collaborator

igdianov commented Sep 8, 2019

Fixed via #173

@igdianov igdianov closed this as completed Sep 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants