name := "mongodb"
version := "1.0.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(play.PlayJava, com.typesafe.sbt.web.SbtWeb)
libraryDependencies ++= Seq( play.PlayImport.javaCore, play.PlayImport.javaJpa, play.PlayImport.javaWs, "org.hibernate.ogm" % "hibernate-ogm-core" % "4.1.0.Beta8", "org.mongodb" % "mongo-java-driver" % "2.12.4")
resolvers ++= Seq()
resolvers += Resolver.sonatypeRepo("releases")
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider> <non-jta-data-source>DefaultDS</non-jta-data-source> <properties> <property name="hibernate.ogm.datastore.provider" value="org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider"/> <property name="hibernate.ogm.mongodb.host" value="127.0.0.1"/> <property name="hibernate.ogm.mongodb.port" value="27017"/> <property name="hibernate.ogm.mongodb.database" value="db"/> <property name="hibernate.ogm.mongodb.safe" value="true"/>
</properties> </persistence-unit>
</persistence>
jpa.default=defaultPersistenceUnit
Could not load requested class : org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider
hibernate.ogm.datastore.provider to mongodbname := "mongodb"
version := "1.0.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(play.PlayJava, com.typesafe.sbt.web.SbtWeb)
libraryDependencies ++= Seq( play.PlayImport.javaCore, play.PlayImport.javaJpa, play.PlayImport.javaWs, "org.hibernate" % "hibernate-entitymanager" % "4.3.6.Final", "org.hibernate.ogm" % "hibernate-ogm-core" % "4.1.0.Beta8", "org.hibernate.ogm" % "hibernate-ogm-mongodb" % "4.1.0.Beta8", "org.jboss.jbossts" % "jbossjta" % "4.16.6.Final")
resolvers ++= Seq()
resolvers += Resolver.sonatypeRepo("releases")
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider> <non-jta-data-source>DefaultDS</non-jta-data-source> <properties>
<property name="hibernate.ogm.datastore.provider" value="mongodb"/> <property name="hibernate.ogm.datastore.host" value="127.0.0.1"/> <property name="hibernate.ogm.datastore.port" value="27017"/> <property name="hibernate.ogm.datastore.database" value="db"/> <property name="hibernate.ogm.datastore.safe" value="true"/>
</properties> </persistence-unit>
</persistence>
@Entity@Indexedpublic class User implements Serializable {
private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("defaultPersistenceUnit"); private static EntityManager em = emf.createEntityManager();
private static final long serialVersionUID = 1L;
@DocumentId @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid2") private String id;
@Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES) public String username;
public User(){}
public User(String username) { this.username = username; this.save(); }
public void save() {
Logger.debug("Saving User ");
if (em == null || !em.isOpen()) em = emf.createEntityManager();
try { em.getTransaction().begin(); em.persist(this); em.getTransaction().commit(); } catch (Exception re) { em.getTransaction().rollback(); throw re; } finally { if (em != null) { em.clear(); em.close(); em = null; } } }
public void update() {
Logger.debug("Updating User ");
if (em == null || !em.isOpen()) em = emf.createEntityManager();
try { em.getTransaction().begin(); em.merge(this); em.getTransaction().commit(); } catch (Exception re) { em.getTransaction().rollback(); throw re; } finally { if (em != null) { em.clear(); em.close(); em = null; } } }}User u = new User("Pippo"); -> I save the entry Pippo
u.username = "Pluto";
u.update(); -> I update the entry with PlutoIf you want to ODM that use java async driver you can use it
https://github.com/raimdtu/PlayAsyncJavaMongoODM
It will give you overview.