Last active
October 8, 2018 02:37
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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