Reputation: 95
Integration does not work mongodb + hibernate, does not throw any exception . after running the em.insert line ( p ) does not insert any record .
public class Principal {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("UnidadOGM");
System.out.println(emf);
EntityManager em = emf.createEntityManager();
Persona p = new Pwersona();
p.setId("1");
p.setNombre("Alberto");
p.setNombre("Perez");
em.persist(p);
List<Persona> lista=em.createQuery("select p from Persona p",Persona.class).getResultList();
for(Persona p :lista) {
System.out.println(p.getNombre());
System.out.println(p.getApellidos());
}
em.close();
}
}
Class Persona
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.annotations.Type;
@Entity
public class Persona {
@Id
@Type(type = "objectid")
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private String nombre;
private String apellidos;
public String getApellidos() {
return apellidos;
}
public void setApellidos(String apellidos) {
this.apellidos = apellidos;
}
public String getNombre() {
return nombre;
}
public Persona() {
super();
}
public Persona(String nombre) {
super();
this.nombre = nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
}
In the persistence.xml , does not detect the configuration database. If I change the port of the database or the name of the database with one that does not exist, does not jump no exception . Yes, read the persistence.xml file if persistence-unit change UnidadOGM , by another chain jumps an exception that does not exist persistence-unit .
<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="UnidadOGM" transaction-type="JTA">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<class>com.arquitecturajava.Persona</class>
<properties>
<property name="hibernate.ogm.datastore.provider" value="mongodb" />
<property name="hibernate.ogm.datastore.grid_dialect"
value="org.hibernate.ogm.datastore.mongodb.MongoDBDialect" />
<property name="hibernate.ogm.datastore.database" value="arquitecturajava" />
<property name="hibernate.ogm.mongodb.host" value="127.0.0.1" />
<property name="hibernate.ogm.mongodb.port" value="27017" />
</properties>
</persistence-unit>
</persistence>
dependencies:
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-mongodb</artifactId>
<version>4.1.0.Beta8</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.1_spec</artifactId>
<version>1.0.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.jbossts</groupId>
<artifactId>jbossjta</artifactId>
<version>4.16.4.Final</version>
</dependency>
Upvotes: 0
Views: 263
Reputation: 19956
You need a transaction to insert data
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("UnidadOGM");
EntityManager em = emf.createEntityManager();
Persona p = new Pwersona();
p.setId("1");
p.setNombre("Alberto");
p.setNombre("Perez");
em.getTransaction().begin();
em.persist(p);
em.getTransaction().commit();
em.close();
If you want to insert and query data in one transaction, probably, you need to call flush()
after inserting data.
Upvotes: 1