尝试将实体保存到MongoDB
数据库时遇到以下问题。
我正在使用Spring
CrudRepository
我的代码如下:
UserDocument user = processUser();
userRepository.save(user);
这是我得到的错误:
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类:
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;
.....
}
答案 0 :(得分:1)
对于任何正在为此问题苦苦挣扎的人-在我的情况下,mapstruct
Mapper
的问题是,它正在填充超类中的字段:
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);
}
}
因此,在保存新实体时,请确保这些字段为null
!
答案 1 :(得分:1)
我也为此苦苦挣扎。在我的例子中,问题是我试图保存的对象中的 @Version
字段被设置为 0。在我将它设置为 null 后,我不再有这个了。
我注意到跟踪中的方法 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)