The idea behind of this project is to provide an example for a secured web service connected using REST architecture style with command pattern and composite command pattern, to a mongodb instance and a mysql instance too, best of both worlds.
There are at least two ways in java world to connect to a mongo db instance, you can choose spring-data-mongodb project, or Morphia, both of them are very easy to use, you only need to create an interface that extends something and that´s it!.
Using morphia way, you have to declare an interface like this:
package com.aironman.sample.dao;
import org.bson.types.ObjectId;
import com.aironman.sample.dao.model.Employee;
/**
* Date: 12 junio 2014
*
* @author Konrad Malawski
* @author Alonso Isidoro
*/
public interface EmployeeDao extends org.mongodb.morphia.dao.DAO<Employee, ObjectId> {
}
And its implementation file:
package com.aironman.sample.dao;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.dao.BasicDAO;
import com.aironman.sample.dao.EmployeeDao;
import com.aironman.sample.dao.model.Employee;
import com.mongodb.Mongo;
/**
* Date: 12 junio 2014
*
* @author Konrad Malawski
* @author Alonso Isidoro
*/
public class EmployeeDaoMorphiaImpl extends BasicDAO<Employee, ObjectId> implements EmployeeDao {
public EmployeeDaoMorphiaImpl(Morphia morphia,Mongo mongo,String dbName) {
super(mongo, morphia,dbName);
}
}
Super easy!
What about if you want to use spring-data-mongo project? an interface and that is all!
package com.aironman.sample.mongo.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.aironman.sample.mongo.documents.Role;
public interface RoleRepository extends MongoRepository<Role, String> {
}
And what about jpa?
package com.aironman.sample.dao;
import com.aironman.sample.dao.model.User;
import org.springframework.data.repository.CrudRepository;
/**
* User: aironman
* Date: 4 de junio del 2014
*/
public interface UserDao extends CrudRepository<User,Long> {
}
The most important using this nonsql technology is to design wisely the mongo db document, which is in JSON format, don’t forget about it, and depending of the wrapper technology chosen, spring or morphia, the way to build one differs. For example, the morphia document:
Employee class, modeled with morphia:
@Entity(value = “employees”, noClassnameStored = true)
public class Employee {
@Id
private ObjectId id;
private String firstName;
private String lastName; // value types are automatically persisted
Long salary; // only non-null values are stored
@Embedded
Address address;
@Reference
Employee manager; // refs are stored*, and loaded automatically
@Reference
List<Employee> underlings; // interfaces are supported
// @Serialized
// EncryptedReviews enchryptedReviews; // stored in one binary field
@Property(“started”)
Date startDate; //fields can be renamed
@Property(“left”)
Date endDate;
@Indexed
boolean active = false; //fields can be indexed for better performance
@NotSaved
String readButNotStored; //fields can read, but not saved
@Transient
int notStored; //fields can be ignored (load/save)
transient boolean stored = true; //not @Transient, will be ignored by Serialization/GWT for example.
getters and setters
}
Now a spring data document class:
@Document
public class Role {
@Id
private String id;
public Role() {
super();
}
public Role(String id) {
super();
this.setId(id);
}
getters, setters, hashCode and equals method…
}
What differences are? the annotation , org.springframework.data.mongodb.core.mapping.Document for the spring-data and
org.mongodb.morphia.annotations.Entity for Morphia, that`s all.
the used jpa pojo in this example is the User class, with a different @Entity annotation.
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String email;
getters and setters
}
That is the difficult part, enjoy with the rest!
Alonso
Links
http://projects.spring.io/spring-data-mongodb/
https://github.com/mongodb/morphia
The source code is located in https://github.com/alonsoir/mycxf-mongodb-morphia-mysql-sample