GithubHelp home page GithubHelp logo

morphia's Introduction

Morphia

Build Status

Morphia is a lightweight type-safe library for mapping Java objects to/from MongoDB. Morphia provides a typesafe, and fluent Query API support with (runtime) validation. Morphia uses annotations so there are no XML files to manage or update. Morphia should feel very comfortable for any developer with JPA experience.

##Features

  • Lifecycle Method/Event Support
  • Works great with Guice, Spring, and other DI frameworks.
  • Many extension points (new annotations, converters, mapping behavior, logging, etc.)
  • Does not store Null/Empty values (by default).
  • GWT support (entities are just POJOs) -- (GWT ignores annotations)
  • Advanced mapper which allows raw conversion, void toObject(DBObject) or DBObject fromObject(Object)

Please continue by reading the QuickStart or looking at a list of the annotations. If you have further questions, please reach out to us on our mailing list.

Quick start

Including morphia in your build

Maven

<dependency>
    <groupId>org.mongodb.morphia</groupId>
    <artifactId>morphia</artifactId>
    <version>###</version>
</dependency>

See the dependencies page for more detail.

Sample code

@Entity("employees")
class Employee {
  // auto-generated, if not set (see ObjectId)
  @Id ObjectId id;

  // value types are automatically persisted
  String firstName, lastName;

  // only non-null values are stored
  Long salary = null;

  // by default fields are @Embedded
  Address address;

  //references can be saved without automatic loading
  Key<Employee> manager;

  //refs are stored**, and loaded automatically
  @Reference List<Employee> underlings = new ArrayList<Employee>();

  // stored in one binary field
  @Serialized EncryptedReviews;

  //fields can be renamed
  @Property("started") Date startDate;
  @Property("left") Date endDate;

  //fields can be indexed for better performance
  @Indexed boolean active = false;

  //fields can loaded, but not saved
  @NotSaved string readButNotStored;

  //fields can be ignored (no load/save)
  @Transient int notStored;

  //not @Transient, will be ignored by Serialization/GWT for example.
  transient boolean stored = true;

  //Lifecycle methods -- Pre/PostLoad, Pre/PostPersist...
  @PostLoad void postLoad(DBObject dbObj) { ... }
}

...

Datastore ds = ...; // like new Morphia(new Mongo()).createDatastore("hr")
morphia.map(Employee.class);

ds.save(new Employee("Mister", "GOD", null, 0));

// get an employee without a manager
Employee boss = ds.find(Employee.class).field("manager").equal(null).get();

Key<Employee> scottsKey =
  ds.save(new Employee("Scott", "Hernandez", ds.getKey(boss), 150**1000));

//add Scott as an employee of his manager
UpdateResults<Employee> res =
  ds.update(
    boss,
    ds.createUpdateOperations(Employee.class).add("underlings", scottsKey)
  );

// get Scott's boss; the same as the one above.
Employee scottsBoss =
  ds.find(Employee.class).filter("underlings", scottsKey).get();

for (Employee e : ds.find(Employee.class, "manager", boss))
   print(e);

Note: @Reference will not save objects, just a reference to them; You must save them yourself.

morphia's People

Contributors

10genjen avatar ctrimble avatar evanchooly avatar ismetozalp avatar jaredstehler avatar jyemin avatar locked-fg avatar marcosnils avatar nisin avatar ogg avatar ozguraydinli avatar scotthernandez avatar thomasduchatelle avatar trnl avatar zalbee avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.