`
leonzhx
  • 浏览: 786899 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Quick Start

阅读更多

1.       Morphia depends on the following third party JARs:

  1)   The MongoDB java driver (see the required version for each Morphia Release )

  2)   Optional (but needed if you use Maven)

      a)   CGLib

      b)   ProxyToys


2.   If you use Maven to manage your project, you can reference Morphia as a dependency:

Repo: http://morphia.googlecode.com/svn/mavenrepo/

Project Settings:

<dependency>
        <groupId>com.google.code.morphia</groupId>
        <artifactId>morphia</artifactId>
        <version>###</version>
</dependency>
<!-Optional Jars (for certain features) but required by maven for bulding. -->
<dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-nodep</artifactId>
        <version>[2.1_3,)</version>
        <type>jar</type>
        <optional>true</optional>
</dependency>
<dependency>
        <groupId>com.thoughtworks.proxytoys</groupId>
        <artifactId>proxytoys</artifactId>
        <version>1.0</version>
        <type>jar</type>
        <optional>true</optional>
</dependency>
 

 

3.   If we want to save instances of these objects to MongoDB, you need to add the Morphia annotations @Entity to the class we want to persist. We can annotate the member filed of the class with @Embedded if it doesn’t have a life outside that class.   All the basic fields are automatically mapped by Morphia. If you want to exclude a field, just annotate it with @Transient .

 

4.   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.

 

5.   Mapping the classes at the beginning of your application is a good practice. It allows the system to validate your classes and prepare for storing the data, and retrieving it:

Mongo mongo = ...;
Morphia morphia = new Morphia();
morphia.map(Hotel.class).map(Address.class);
Datastore ds = morphia.createDatastore(mongo, "my_database");
 

 

 

6.   We can use the Datastore instance to save classes with MongoDB:

Hotel hotel = new Hotel();
hotel.setName("My Hotel");
…

Address address = new Address();
address.setStreet("123 Some street");
…

hotel.setAddress(address);
Morphia morphia = ...;
Datastore ds = morphia.createDatastore("testDB");
ds.save(hotel); 
 

 

7.   Load a Hotel from Mongo:

String hotelId = ...; // the ID of the hotel we want to load

// and then map it to our Hotel object
Hotel hotel = ds.get(Hotel.class, hotelId); 
 

 

8.   Load a Hotel using a query:

// it is easy to get four-star hotels.
List<Hotel> fourStarHotels = ds.find(Hotel.class, "stars >=", 4).asList();
//or
fourStarHotels = ds.find(Hotel.class).field("stars").greaterThenEq(4).asList();
 

 

9.   In a web application environment, we would probably inject the Mongo, Morphia, and Datastore instances into a DAO/Service, and then inject that into a controller, so the controller would never directly deal with Mongo or Morphia:

public class HotelDAO extends BasicDAO<Hotel, String> {
    public HotelDAO(Morphia morphia, Mongo mongo ) {
        super(mongo, morphia, "myDB");
    }
}

HotelDAO hDAO = ...
hDAO.save(new Hotel(...));
 

 

分享到:
评论

相关推荐

    ActivitiDeveloperQuickStart Quick Start Guide

    《Activiti Developer Quick Start快速入门指南》 Activiti是一款开源的工作流引擎,它基于Java,设计用于企业级应用,提供了一套强大的业务流程自动化(BPM)解决方案。本指南将帮助开发者快速上手Activiti,从...

    Word 2013 Quick Start Guide

    Word2013快速入门指南、 Word 2013 Quick Start Guide、英文版

    tensorflow2.0 quick start

    在这个“TensorFlow 2.0 Quick Start”指南中,我们将深入探讨这个新版本的关键特性,以及如何快速上手。 **一、核心概念与安装** 1. **核心API**: TensorFlow 2.0 强调了Eager Execution(即时执行),这是一种...

    MERN Quick Start Guide pdf

    Quick Start Guides are focused, shorter titles that provide a faster paced introduction to a technology. They are for people who don't need all the detail at this point in their learning curve. The ...

    SwiftUI Views Quick Start 2019.pdf

    本文件《SwiftUI Views Quick Start 2019.pdf》是关于SwiftUI的一个快速入门指南,它详细介绍了SwiftUI的各个控件使用方法和实例。 SwiftUI是为Swift语言设计的一个声明式UI框架,允许开发者用更少的代码来创建复杂...

    pytest Quick Start Guide - 2018

    ### pytest Quick Start Guide - 2018 关键知识点概览 #### 一、pytest框架简介及优势 - **背景介绍**:Python 的标准单元测试模块 `unittest` 基于 xUnit 框架家族,起源于 Smalltalk 和 Java,使用起来较为冗长且...

    MERN Quick Start Guide

    Quick Start Guides are focused, shorter titles that provide a faster paced introduction to a technology. They are for people who don't need all the detail at this point in their learning curve. The ...

    Blender Quick Start Guide

    Blender Quick Start Guide

    ads2011快速开始教程quick start

    ADS2011快速开始教程(Quick Start)是专为新用户设计的入门级教程,旨在帮助他们快速掌握软件的基本使用方法,从而能够顺利完成设计任务。 教程的第一部分是“Einleitung und Einstiegshilfen für ADS2011”,即...

    Vuex Quick Start Guide 2018年4月出版 vue最新图书

    2018年4月出版的vue最新图书 Vuex Quick Start Guide Chapter 1, Rethinking User Interfaces with Flux, Vue, and Vuex, introduces the concepts of Flux architecture and the small differences in Vuex ...

    Truffle quick start guide

    You will begin with covering the basics of Truffle, briefly explaining how it integrates Solidity and Web3, in orderto start building a mini decentralized application. Also, you will dive into ...

    Quick Start V1.0

    "Quick Start V1.0" 是一款旨在帮助用户简化桌面管理、提高启动效率的软件工具。这款工具的主要功能是让用户能够快速地访问和启动常用的应用程序,从而避免桌面图标过多导致的混乱,同时也能提高用户的操作速度。 ...

    TensorFlow Reinforcement Learning Quick Start Guide

    TensorFlow Reinforcement Learning Quick Start Guide: Get up and running with training and deploying intelligent, self-learning agents using Python Author: Kaushik Balakrishnan Pub Date: 2019 ISBN: 978...

    TimeQuest Timing Analyzer quick start tutorial.pdf

    标题《TimeQuest Timing Analyzer quick start tutorial.pdf》和描述《quartus ii时序分析,很多人学了时序分析的脚本,但是却不知道quartus ii上面的时序分析器图形界面怎么操作,此教程可以快速帮你上手时序约束,...

    Pentaho Data Integration Quick Start Guide(2018)随书代码

    《Pentaho Data Integration Quick Start Guide(2018)随书代码》是关于Pentaho Data Integration(也称为Kettle)的入门指南,旨在帮助初学者快速掌握这个强大的ETL(提取、转换、加载)工具。这本书包含了从Chapter...

    Google Flutter Mobile Development Quick Start Guide

    Google Flutter Mobile Development Quick Start Guide - Salvatore Giordano & Prajyot Mainkar(2019),epub格式,欢迎下载

Global site tag (gtag.js) - Google Analytics