1

When I'm getting a property which doesn't exist in java object error when I do this. My record object has

@Document
public class Record{
    @Field("RecordIdentifier")
    String RecordIdentifier;
    ...
}

My MongoRepository object has

public interface RecordRepository extends MongoRepository<Record,String> {

    @Query("{ 'RecordIdentifier' : ?0 }
    public Record findByRecordIdentifier(String RecordIdentifier);

}

And in the MongoDB I have

{
    'RecordIdentifier' : 'D'
}

But when I call the query method

@Autowired
RecordRepository repository

...

repository.findAll();
repository.findByRecordIdentifier("D");

The repository.findAll() returns fine and maps everything correctly, but findByRecordIdentifier() returns an error saying that it cannot find property 'recordIdentifier' in the Record object. If I change the name of the field from 'RecordIdentifier' to 'recordIdentifier' in the Record object it works fine but why can't I have the field be called 'RecordIdentifier'

What's really weird is if I change the Mongo document to

{
 'Record_Identifier' : 'D'
}

I still get the same error even though the query should no longer be returning anything since it's now pointing to a field that doesn't exist. Could someone help me out? I can provide more details if need be.

1
  • please put dobule quotes and parentesi for @Query, you forgot the last one Commented Jun 9, 2020 at 16:22

4 Answers 4

0

Change String RecordIdentifier; to String recordIdentifier; and Add getter/setter methods in the class. Your field declaration doesn't conform Java Bean naming standard so Reflection couldn't find proper setter method.

Sign up to request clarification or add additional context in comments.

2 Comments

There are about 1000 other fields and the package this java object is in is being used by other projects so there would be a big impact if I needed to change the naming convention of every string. It does work but I'd only want to use it as a last resort
The only other option is to write your own query using monogTemplate execute it and populate POJO taking data from BSON. That will too low level and amount of effort will be same but change will be in one place i.e. in your DAO layer.
0

To understand why this is happening, you need to understand how spring-data works.

Spring data Repository work mostly with method names. It creates query based on the method name.
Spring-data will formulate the query to fetch the field specified after findBy (RecordIdentifier in your case). But at compile time, it checks whether you are providing a valid field So that it can prepare a proper query. It does so by checking your entity class (Record in your case) for this field.

Note : If the Field name is abc, then you have to specify the method name as findByAbc (Notice uppercase A)

For example in your case, if you specify just this(without @Query)

 public Record findByRecordIdentifier(String RecordIdentifier);

It will try to search for field recordIdentifier in database.

If you don't want spring-data to formulate the query on its own from method name then you can specify that with @Query as you are doing now.

But the reason it is not working is because you are confusing spring-data by specifying the method name such that it tries to formulate the Query on its own.

Solution :
1. change the field names to naming conventions which is recordIdentifier. If you do this, then you wouldn't need @Query on your Repository method.
2. change the method name (ommitting findBy and findAllBy prefix) to something like

@Query("{ 'RecordIdentifier' : ?0 }
public Record getRecordByRecordIdentifier(String RecordIdentifier);

2 Comments

The explanation is good, however the solution is not as clean as Debopam's. If you change the suggested solution to that, I'll upvote this.
I don't want to change the naming convention for all the fields in the project as the package with the java object is used by other projects. I tried solution #2 but I am still getting the same error.
0

If you don't want to change your field names, other option is to using custom converter

public class PersonReadConverter implements Converter<Document, Person> {

  public Person convert(Document source) {
    Person p = new Person((ObjectId) source.get("_id"), (String) source.get("name"));
    p.setAge((Integer) source.get("age"));
    return p;
  }
}

Register them using

<mongo:mapping-converter>
  <mongo:custom-converters base-package="com.acme.**.converters" />
</mongo:mapping-converter>

For your reference https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.custom-converters

Comments

0

you forgot put " at the end of @Query. please use

@Query("{ 'RecordIdentifier' : ?0 }")

instead of

@Query("{ 'RecordIdentifier' : ?0 })

2 Comments

Your post could benefit from some applied formatting. See here stackoverflow.com/editing-help
And please clarify how this answer the question at the top of this page. I get the impression that you are commenting on the answer by @pvpkiram If that actially is what you are trying, then please use your commenting privilege and delete this non-answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.