📜 ⬆️ ⬇️

Morphia - easy annotations ORM for MongoDB

Reading the daily portion of posts from my Google Reader collection, I came across a post about ORM for MongoDB with the alluring name “Morphia”. Below you will find the layout of materials from its documentation, claiming the title of "a very brief overview."

  1. Morphia is very easy to use. This is a lightweight and fast framework.
  2. Supports both annotated POJO objects and DAO approach
  3. All configuration is specified by annotations, XML files are not used.
  4. The extension interface is supported (validation (jsr330) and SLF4J support for logging are currently built into the framework)
  5. Works great with Google Guice, Spring and other DI frameworks.
  6. Contains a large number of expansion points
  7. Supports GWT



Essence in Morphia is described as:
')
@Entity("employees") class Employee { @Id ObjectId id; //  ,     String firstName, lastName; // ,      Long salary = null; //   -null  Address address; //       @Embedded (   ,    ) Key<Employee> manager; //      ... @Reference List<Employee> underlings = new ArrayList<Employee>(); //...     @Serialized EncryptedReviews; //       @Property("started") Date startDate; //  ... @Property("left") Date endDate; @Indexed boolean active = false; //...   @NotSaved String readButNotStored; //  ,   ... @Transient int notStored; //...     transient boolean stored = true; //   transient   // ,        @PostLoad void postLoad(DBObject dbObj) { ... } } 


You can work with the base like this:

 Morphia morphia = new Morphia(); db = new Mongo(); Datastore ds = morphia.createDatastore(db, appname, user, pass.toCharArray()); morphia.map(Employee.class); ds.save(new Employee("Mister", "GOD", null, 0)); //  ,     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)); //      ds.update(boss, ds.createUpdateOperations(Employee.class).add("underlings", scottsKey)); //    Employee scottsBoss = ds.find(Employee.class).filter("underlings", scottsKey).get(); for (Employee e : ds.find(Employee.class, "manager", boss)) print(e); 


An example of queries built using the Fluent API:

 Query q = ds.createQuery(MyEntity.class).filter("foo >", 12).filter("foo <", 30).order("dateAdded").offset(1000).retrievedFields(true, "foo"); //    MyEntity e = ds.find(MyEntity.class).field("name").equal("someName").get(); //     name = "someName"; List<Hotel> hotels = ds.find(Hotel.class, "stars >", 3).sort("-stars").asList(); 


Example of work in the style of DAO:

 public class HotelDAO extends BasicDAO<Hotel, String> { public HotelDAO(Morphia morphia, Mongo mongo ) { super(mongo, morphia, "myDB"); } public List<Hotel> findByTitle( String title ) { Pattern regExp = Pattern.compile(name + ".*", Pattern.CASE_INSENSITIVE); return ds.find(Hotel.class).filter("title", regExp).sort("title").asList(); } } HotelDAO hDAO = new HotelDAO(...); List<Hotel> hotels = hDAO.findByTitle("Luxury"); hDAO.save(new Hotel(...)); 


References:

Source: https://habr.com/ru/post/118783/


All Articles