Skip to content
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

Failed usage of provided ID value on document insert #4184

Closed
aburmakov opened this issue Sep 27, 2022 · 3 comments
Closed

Failed usage of provided ID value on document insert #4184

aburmakov opened this issue Sep 27, 2022 · 3 comments
Assignees
Labels
type: regression A regression from a previous release

Comments

@aburmakov
Copy link

aburmakov commented Sep 27, 2022

Self-generated ID cannot be insert into mongo collection. System-generated ObjectID.get() is used instead of self-generatd.
Looks like problem appears after commit: 8ee33b2 ("Generate and convert id on insert if explicitly defined.", 2022-05-19)

Sample code:

@Document(collection = AcmeDocument.COLLECTION_NAME)
public class AcmeDocument implements IdDocument {
    public static final String COLLECTION_NAME = "acme";
    @MongoId
    private String id;
    private String userName;
    public AcmeDocument(String userName) {
        this.id = UUID.randomUUID().toString();
        this.userName = userName;
    }
//...
}

// usage:
AcmeDocument d = new AcmeDocument(userName);
d = acmeRepository.save(d);
log.debug("create.id: {}", d.getId());
Log output: create.id: e43afa9b-06db-4935-a94a-f76578c7af83
But mongoDb document has _Id as generated ObjectId: 6332ca654af6176f2c0c230d
@aburmakov aburmakov changed the title Failed usage of self-generated ID in document (v.3.4.3) Failed usage of self-generated ID when inserts document (v.3.4.3) Sep 27, 2022
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 27, 2022
@aburmakov aburmakov changed the title Failed usage of self-generated ID when inserts document (v.3.4.3) Failed usage of self-generated string ID when inserts document (v.3.4.3) Sep 28, 2022
@christophstrobl christophstrobl self-assigned this Oct 4, 2022
@christophstrobl
Copy link
Member

I might be missing something but I cannot reproduce the issue.

class AcmeDocument {
	@MongoId
	private String id;
	private String userName;
	public MyOddIdDocument(String userName) {
		this.id = UUID.randomUUID().toString();
		this.userName = userName;
	}
}

template.dropCollection(AcmeDocument.class);

AcmeDocument source = new AcmeDocument("one");
template.save(source);

org.bson.Document dbDoc = collection.find().limit(1).cursor().next());

assertThat(dbDoc).isNotNull();
assertThat(dbDoc.get("_id")).isEqualTo(source.id);

AcmeDocument loaded = template.findOne(query(where("id").is(source.id)), AcmeDocument.class);
assertThat(loaded).isEqualTo(source);
assertThat(loaded.id).isInstanceOf(String.class);

Please take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.

@christophstrobl christophstrobl added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 11, 2022
@TarasZubreiFiverr
Copy link

@christophstrobl Hey, please take a look at referenced issue spring-projects/spring-boot#32635. It has a reproducible example and focuses on insert() method that is actually producing wrong id.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Oct 11, 2022
@mp911de mp911de added type: regression A regression from a previous release and removed status: feedback-provided Feedback has been provided labels Oct 12, 2022
@mp911de mp911de added this to the 3.4.4 (2021.2.4) milestone Oct 12, 2022
@mp911de mp911de changed the title Failed usage of self-generated string ID when inserts document (v.3.4.3) Failed usage of self-generated string ID on document insert Oct 12, 2022
@mp911de mp911de changed the title Failed usage of self-generated string ID on document insert Failed usage of provided ID value on document insert Oct 12, 2022
mp911de pushed a commit that referenced this issue Oct 12, 2022
See: #4184
See: #4197
Original pull request: #4203.
mp911de pushed a commit that referenced this issue Oct 12, 2022
This commit fixes an issue where an existing Id got replaced with a generated one when using MongoId annotation.

Closes: #4184
Closes: #4197
Original pull request: #4203.
mp911de pushed a commit that referenced this issue Oct 12, 2022
See: #4184
See: #4197
Original pull request: #4203.
@pcornelissen
Copy link

save also creates the wrong ID in spring boot 2.7.4: https://github.com/pcornelissen/springboot-mongo-bug

@mp911de As you have worked on that, is there an ETA for a new release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants