Using Morphia to map Java objects in MongoDB
MongoDB is an open source document-oriented NoSQL database system which stores data as JSON-like documents with dynamic schemas.
As it doesn't store data in tables as is done in the usual relational database setup, it doesn't map well to the JPA way of storing data. Morphia is an open source lightweight type-safe library designed to bridge the gap between the MongoDB Java driver and domain objects. It can be an alternative to SpringData if you're not using the Spring Framework to interact with MongoDB.
This post will cover the basics of persisting and querying entities along the lines of JPA by using Morphia and a MongoDB database instance.
There are four POJOs this example will be using. First we have BaseEntity which is an abstract class containing the Id and Version fields:
import org.bson.types.ObjectId;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.Property;
import com.google.code.morphia.annotations.Version;
public abstract class BaseEntity {
@Id
@Property("id")
protected ObjectId id;
@Version
@Property("version")
private Long version;
public BaseEntity() {
super();
}
public ObjectId getId() {
return id;
}
public void setId(ObjectId id) {
this.id = id;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
As it doesn't store data in tables as is done in the usual relational database setup, it doesn't map well to the JPA way of storing data. Morphia is an open source lightweight type-safe library designed to bridge the gap between the MongoDB Java driver and domain objects. It can be an alternative to SpringData if you're not using the Spring Framework to interact with MongoDB.
This post will cover the basics of persisting and querying entities along the lines of JPA by using Morphia and a MongoDB database instance.
There are four POJOs this example will be using. First we have BaseEntity which is an abstract class containing the Id and Version fields:
CODE:
package com.city81.mongodb.morphia.entity;import org.bson.types.ObjectId;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.Property;
import com.google.code.morphia.annotations.Version;
public abstract class BaseEntity {
@Id
@Property("id")
protected ObjectId id;
@Version
@Property("version")
private Long version;
public BaseEntity() {
super();
}
public ObjectId getId() {
return id;
}
public void setId(ObjectId id) {
this.id = id;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-735500/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Operating on Specific Objects Using EXPDPObject
- Export a DataSet to Microsoft Excel without using the COM objectsExportROSExcelObject
- Using MongoDB in C#MongoDBC#
- Import Data From MS Excel to DataSet without using COM ObjectsImportExcelObject
- Spring Data MongoDB cascade save on DBRef objectsSpringMongoDBObject
- java mapJava
- 好用的java.util.Objects類JavaObject
- Java 中的map - The Map Interface.Java
- MongoDB報錯:"assertion" : "can't map file memory"MongoDB
- Java集合類——MapJava
- JAVA集合——Map介面Java
- Java筆記——【Map】Java筆記
- Java List/Set/MapJava
- Java 8 Streams map()Java
- java Map及Map.Entry詳解Java
- ObjectsObject
- java中Map根據Map的value取keyJava
- Java操作MongoDBJavaMongoDB
- write picture to oracle using javaOracleJava
- JAVA集合框架 - Map介面Java框架
- Java map轉JSONJavaJSON
- Java Map集合練習Java
- 【java】【Map】HashMap、Hashtable、CollectionsJavaHashMap
- java Map Set遍歷Java
- java中的Map集合Java
- Mongodb 的中資料統計神器Map_Reduce的使用MongoDB
- java Map相關總結Java
- Java物件轉換成MapJava物件
- Java集合四:Map簡介;Java
- 深入淺出java的MapJava
- Java Collection或Map的同步Java
- Java Collection、Map集合總結Java
- java Map遍歷最優Java
- Java中將多個Map扁平化為單個MapJava
- Java Map的最佳實踐 - tremblayJavaREM
- Java中實現不可變MapJava
- java將map轉成bean工具JavaBean
- Java中Map的遍歷方法Java