0

I have code like:

public Foo add(String firebaseId) {
    Foo foo = fooRepository.findByFirebase(firebaseId);
    if (foo == null) {
        foo = new Foo();
    }
    foo.setFirebaseId(firebaseId);
    return fooRepository.save(foo);
}

public interface UsersPlayersRepository extends MongoRepository<Foo, ObjectId> {

    Foo findByFirebaseId(String firebaseId);
}

@Document(collection = "foo")
public class Foo {

    public Foo() {}

    @Id
    @JsonIgnore
    private ObjectId id;

    @JsonProperty
    private String firebaseId;
....

but yet somehow in logs I later see errors on retrieval:

ERROR] 2019-12-27 01:15:07.985 [http-nio-9000-exec-408] [dispatcherServlet] - Servlet .service() for servlet [dispatcherServlet] in context with path [] threw exception [Re quest processing failed; nested exception is org.springframework.dao.IncorrectResultSi zeDataAccessException: Query { "$java" : Query: { "firebaseId" : "3OdjeUiCkOeKzH1ETvGL HjH0xLv1" }, Fields: { }, Sort: { } } returned non unique result.] with root cause

I want to make sure only unique firebase item is stored? Is there something wrong in my construction?

user2395365
  • 1,991
  • 4
  • 18
  • 34
  • Post entity and repository details also. – Ashish Bakwad Dec 28 '19 at 04:07
  • 1
    As mentioned in your error response, you have more than one record in `fooRepository` for given firebaseId. Either make sure firebase id is unique or store results in list – lucid Dec 28 '19 at 04:29
  • 2
    What is the entity’s ID? If it’s that ObjectId property and it’s marked as JsonIgnore, you might not be retrieving the ID from the database, and when you insert it, it will insert it as a new one, which would result in duplicates. – Tomaz Fernandes Dec 28 '19 at 04:50
  • Either way, I don’t understand what’s the point of saving the entity again if it was retrieved from the database, since the only property assigned is firebaseId, which was the criteria for the search in the first place – Tomaz Fernandes Dec 28 '19 at 04:52
  • You have a typo in this line Foo foo = fooRepository.findByFirebase(firebaseId); It should be Foo foo = fooRepository.findByFirebaseId(firebaseId); – Faron Dec 28 '19 at 16:43

0 Answers0