I use netbeans-8.0.1 IDE
see my files: pom.xml, persistence.xml, entity Agence.java, AbstractFacade.java and AgenceFacade.java
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId> <artifactId>mavenproject1hibernateogm</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging>
<name>mavenproject1hibernateogm</name>
<properties> <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.8.Final</version> </dependency>
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.8.Final</version> </dependency>
<dependency> <groupId>org.hibernate.ogm</groupId> <artifactId>hibernate-ogm-core</artifactId> <version>4.1.3.Final</version> </dependency> <dependency> <groupId>org.hibernate.ogm</groupId> <artifactId>hibernate-ogm-mongodb</artifactId> <version>4.1.3.Final</version> </dependency>
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-search-orm</artifactId> <version>4.2.0.Final</version> </dependency>
<!--MongoDB Java Driver--> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.12.3</version> <type>jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <compilerArguments> <endorseddirs>${endorsed.dir}</endorseddirs> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.6</version> <executions> <execution> <phase>validate</phase> <goals> <goal>copy</goal> </goals> <configuration> <outputDirectory>${endorsed.dir}</outputDirectory> <silent>true</silent> <artifactItems> <artifactItem> <groupId>javax</groupId> <artifactId>javaee-endorsed-api</artifactId> <version>7.0</version> <type>jar</type> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <persistence version="2.0" 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"> <persistence-unit name="mavenproject2NoSQL" transaction-type="JTA"> <!-- Use Hibernate OGM provider: configuration will be transparent --> <!-- Use Hibernate OGM provider: configuration will be transparent --> <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider> <class>com.mycompany.mavenproject2nosql2.Agence</class> <class>com.mycompany.mavenproject2nosql2.Operations</class> <class>com.mycompany.mavenproject2nosql2.Utilisateur</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="hibernate.ogm.datastore.provider" value="mongodb"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" /> <property name="hibernate.ogm.datastore.database" value="gnocobok"/> <property name="hibernate.ogm.datastore.host" value="localhost"/> <property name="jboss.as.jpa.providerModule" value="org.hibernate:ogm"/>
<!--<property name="hibernate.ogm.mongodb.database" value="gnocobok"/>--> <!--<property name="hibernate.ogm.mongodb.host" value="localhost"/>--> </properties> </persistence-unit> </persistence>
Agence.java
package com.mycompany.mavenproject2nosql2;
import java.io.Serializable; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.xml.bind.annotation.XmlTransient;
/** * * @author madyg */
@Entity public class Agence implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String nomAgence; private String adresseAgence; private String villeAgence; private String telAgence; private String mailAgence; //@OneToMany(mappedBy = "id") //@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @OneToMany(mappedBy = "agence", cascade = CascadeType.PERSIST) private List<Utilisateur> utilisateurList;
public Agence() { }
public Agence(Long id) { this.id = id; }
public Agence(Long id, String nomAgence, String adresseAgence, String villeAgence, String telAgence, String mailAgence) { this.id = id; this.nomAgence = nomAgence; this.adresseAgence = adresseAgence; this.villeAgence = villeAgence; this.telAgence = telAgence; this.mailAgence = mailAgence; }
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getNomAgence() { return nomAgence; }
public void setNomAgence(String nomAgence) { this.nomAgence = nomAgence; }
public String getAdresseAgence() { return adresseAgence; }
public void setAdresseAgence(String adresseAgence) { this.adresseAgence = adresseAgence; }
public String getVilleAgence() { return villeAgence; }
public void setVilleAgence(String villeAgence) { this.villeAgence = villeAgence; }
public String getTelAgence() { return telAgence; }
public void setTelAgence(String telAgence) { this.telAgence = telAgence; }
public String getMailAgence() { return mailAgence; }
public void setMailAgence(String mailAgence) { this.mailAgence = mailAgence; } @XmlTransient public List<Utilisateur> getUtilisateurList() { return utilisateurList; }
public void setUtilisateurList(List<Utilisateur> utilisateurList) { this.utilisateurList = utilisateurList; }
@Override public int hashCode() { int hash = 0; hash += (id != null ? id.hashCode() : 0); return hash; }
@Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Agence)) { return false; } Agence other = (Agence) object; if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; }
@Override public String toString() { return "com.mycompany.gnocobokmaven.Agence[ id=" + id + " ]"; } }
AbstractFacade.java
package com.mycompany.mavenproject1hibernateogm;
import java.util.List; import javax.persistence.EntityManager;
/** * * @author madyg */ public abstract class AbstractFacade<T> { private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) { this.entityClass = entityClass; }
protected abstract EntityManager getEntityManager();
public void create(T entity) { getEntityManager().persist(entity); }
public void edit(T entity) { getEntityManager().merge(entity); }
public void remove(T entity) { getEntityManager().remove(getEntityManager().merge(entity)); }
public T find(Object id) { return getEntityManager().find(entityClass, id); }
public List<T> findAll() { javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(); cq.select(cq.from(entityClass)); return getEntityManager().createQuery(cq).getResultList(); }
public List<T> findRange(int[] range) { javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(); cq.select(cq.from(entityClass)); javax.persistence.Query q = getEntityManager().createQuery(cq); q.setMaxResults(range[1] - range[0] + 1); q.setFirstResult(range[0]); return q.getResultList(); }
public int count() { javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(); javax.persistence.criteria.Root<T> rt = cq.from(entityClass); cq.select(getEntityManager().getCriteriaBuilder().count(rt)); javax.persistence.Query q = getEntityManager().createQuery(cq); return ((Long) q.getSingleResult()).intValue(); } }
AgenceFacade.java
package com.mycompany.mavenproject1hibernateogm;
import com.mycompany.mavenproject2nosql2.Agence; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext;
/** * * @author madyg */ @Stateless public class AgenceFacade extends AbstractFacade<Agence> { @PersistenceContext(unitName = "mavenproject2NoSQL") private EntityManager em;
@Override protected EntityManager getEntityManager() { return em; }
public AgenceFacade() { super(Agence.class); } }
|