DirtyMikeAndTheBoys
DirtyMikeAndTheBoys

Reputation: 1077

Is there a way to use Mongo/Morphia without org.bson.types.ObjectId?

Morphia is a persistence library for the JVM, used when your app needs to communicate with a MongoDB. When you use it, apparently in Mongo/Morphia land, this unique ID needs to be a org.bson.types.ObjectId.

Is this true? Is there any reason why I can't use a Long for the ID, such as in:

// Groovy pseudo-code
@Entity
@JsonIgnoreProperties(ignoreUnknown=true)
@JsonInclude(JsonInclude.Include.NON_NULL)
class WidgetEntity {
    @Id
    Long id

    // ...etc.
}

If it is possible to just use a Long, then what am I giving up by using a Long instead of an ObjectId?

Upvotes: 1

Views: 285

Answers (1)

wero
wero

Reputation: 32980

You can use a Long but in this case you need to assign a value yourself.

From the morphia quickstart sample:

Also note that we had to add a new field "id" to our Hotel class. The "id" value can be any persist-able type; like an int, uuid, or other object. If you want an auto-generated value just declare it as an ObjectId. If you don't use an ObjectId you must set the value before saving.

Upvotes: 1

Related Questions