FazoM
FazoM

Reputation: 4956

Could not obtain identifier from

I am getting following problem when trying to save an Entity into MongoDB database.

I am using Spring CrudRepository

An my code looks as follow:

UserDocument user = processUser();
userRepository.save(user);

This is the error I am getting:

java.lang.IllegalStateException: Could not obtain identifier from UserDocument(id=null, ownerId=..., ...)!
    at o.s.d.m.TargetAwareIdentifierAccessor.getRequiredIdentifier(TargetAwareIdentifierAccessor.java:47)
    at o.s.d.m.c.EntityOperations$MappedEntity.getId(EntityOperations.java:466)
    ... 53 frames excluded

UserDocument class:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.CompoundIndexes;
import org.springframework.data.mongodb.core.mapping.Document;

@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@SuperBuilder
@Document(collection = UserDocument.COLLECTION)
public class UserDocument extends BaseDocument<ObjectId> {

  public static final String COLLECTION = "users";

  @Id
  private ObjectId id;

 .....
}

Upvotes: 5

Views: 5291

Answers (2)

wi2ard
wi2ard

Reputation: 1555

I struggled with this too. In my case, the problem was the @Version field in the object I was trying to save was set to 0. After I set it to null I didn't have this anymore. I noticed the methods in the trace getQueryForVersion, doSaveVersioned...

java.lang.IllegalStateException: Could not obtain identifier from .....
    at org.springframework.data.mapping.TargetAwareIdentifierAccessor.getRequiredIdentifier(TargetAwareIdentifierAccessor.java:48)
    at org.springframework.data.mongodb.core.EntityOperations$MappedEntity.getId(EntityOperations.java:527)
    at org.springframework.data.mongodb.core.EntityOperations$MappedEntity.getQueryForVersion(EntityOperations.java:556)
    at org.springframework.data.mongodb.core.MongoTemplate.doSaveVersioned(MongoTemplate.java:1383)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:1370)
    at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:88)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)

Upvotes: 6

FazoM
FazoM

Reputation: 4956

For anyone that is struggling with this problem - in my case it was problem with mapstruct Mapper that as a side effect was populating fields in super class:

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.annotation.Version;

@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Accessors(chain = true)
@SuperBuilder
public abstract class BaseDocument<ID extends Serializable> implements Persistable<ID> {

  @Version
  private Long revision;

  @CreatedDate
  private Instant createdDateTime;

  @LastModifiedDate
  private Instant lastModifiedDateTime;

  @Override
  public boolean isNew() {
    return isNull(createdDateTime);
  }
}

So make sure these fields are null when you are saving a new entity!

Upvotes: 7

Related Questions