Previously I briefly introduced the mjorm mongo-java-ORM solution I found. This post will go more in-depth and give more specific examples of querying, creating, and removing records from your mongoDB instance. If you don’t already have mongoDB, you’ll need it for this tutorial. After I’m through with my dog and pony show, I’ll provide a link to download a copy of the app that will be created throughout the tutorial.
If you’re following along on your own and don’t want to use the demo app as a crutch you’ll need these two jar files and you’ll have to manually install them into your local maven repository as I couldn’t find them on any public repositories anywhere.
I chose Spring MVC as the framework for this demo app because you get a lot of functionality baked in without much effort. You can be up and running with annotations in about 10 minutes including the time it’ll take Maven to download the requisite libraries.
We’re going to create an example of everyone’s favorite things: a used car lot. The seedy buy here pay here type; not an offshoot of a new car lot. We’ve got cars. They’ve got a price, a make, and a model. Let’s pretend that Salesperson are assigned cars. I know, it’s a lame idea but I couldn’t think of a better idea. Salespeople have a name and their assigned cars.
public class Manufacturer {
private String name;
}
public class Car {
private Manufacturer make;
private String model;
private long price;
}
public class Salesperson{
private String name;
private List<Car> assignedVehicles;
}
You can create your own accessors and mutators, lazy.
Next we’ll create the xml files that mjorm uses to map the objects to their respective Mongo collections.
Manufacturer.mongo.xml
<?xml version="1.0" encoding="UTF-8"?> <descriptors> <object class="com.cribbstechnologies.springmvc.model.Manufacturer"> <property name="name"></property> </object> </descriptors>
Car.mongo.xml
<?xml version="1.0" encoding="UTF-8"?> <descriptors> <object class="com.cribbstechnologies.springmvc.model.Car"> <property name="name"></property> <property name="make" class="com.cribbstechnologies.springmvc.model.Manufacturer"></property> <property name="price"></property> </object> </descriptors>
Salesperson.mongo.xml
<?xml version="1.0" encoding="UTF-8"?> <descriptors> <object class="com.cribbstechnologies.springmvc.model.Salesperson"> <property name="name"></property> <property name="cars"> <type-param class="com.cribbstechnologies.springmvc.model.Car"></type-param> </property> </object> </descriptors>
This post will be continued in part 3.
Pingback: Java ORM using MongoDB | Cribbs Technologies
It is actually rare to find skillful individuals with this matter, nevertheless, you be understood as you understand what you are speaking about! Thank you