Quantcast
Channel: Spring Community Forums - NoSQL
Viewing all articles
Browse latest Browse all 128

Generating ObjectIds in custom ID types

$
0
0
Recently I wanted to update Spring Data Mongodb from 1.0.1 to 1.2.1. Unfortunately generating ObjectIds automatically does not work any more as of 1.0.2.

I have the following structure in the persistence layer:

class TestClass {

@Id
private TestIdentifier id = new TestIdentifier();

...
}

class TestIdentifier extends Identifier {}

class Identifier implements Serializable {
String value;
}

Using 1.0.1 the value of the Identifier was set to a generated ObjectId during MongoTemplate.save(). This worked
in combination with a IdentifierConverter, but only if the key attribute is initialized as shown in the code above. Otherwise I got following exception:

org.springframework.dao.InvalidDataAccessApiUsageE xception: Cannot autogenerate id of type ...
at org.springframework.data.mongodb.core.MongoTemplat e.assertUpdateableIdIfNotSet(MongoTemplate.java:91 7)
at org.springframework.data.mongodb.core.MongoTemplat e.doSave(MongoTemplate.java:713)

This does not work from 1.0.2 on any more.
MongoTemplate.populateIdIfNecessary() line 1377 returns if the Identifier is not null. That's why the generated identifier is not set in the persisted entity.

Also the documentation says that autogenerating keys is only possible if the key attribute is of type ObjectId, String or BigInteger.

So if I understand the behaviour correctly, auto generating Ids for custom ID types is not possible any more ?

Viewing all articles
Browse latest Browse all 128

Trending Articles