Skip to content

Instantly share code, notes, and snippets.

@JaiHirsch
Last active October 8, 2018 02:37
Show Gist options
  • Save JaiHirsch/944c8313d4eff0a59ec9 to your computer and use it in GitHub Desktop.
Save JaiHirsch/944c8313d4eff0a59ec9 to your computer and use it in GitHub Desktop.
MongoDB 3.0 java driver Codec example - base class for the Codec mapping (bean)
/*
MongoDB bean object for java 3.0 driver mapping
*/
package org.scratch;
import org.bson.BsonDocument;
import org.bson.BsonDocumentWrapper;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
public class Grades implements Bson {
private ObjectId _id;
private int studentId;
private String type;
private double score;
public Grades() {
}
public Grades(int id, String type, double score) {
this.studentId = id;
this.type = type;
this.score = score;
}
public Grades withNewObjectId() {
setId(new ObjectId());
return this;
}
public org.bson.types.ObjectId getId() {
return _id;
}
public void setId(ObjectId _id) {
this._id = _id;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public <TDocument> BsonDocument toBsonDocument(Class<TDocument> documentClass,
CodecRegistry codecRegistry) {
return new BsonDocumentWrapper<Grades>(this, codecRegistry.get(Grades.class));
}
@Override
public String toString() {
return "Grades [_id=" + _id + ", studentId=" + studentId + ", type=" + type + ", score="
+ score + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment