Skip to content

Instantly share code, notes, and snippets.

@fjavier
Created September 12, 2014 16:50
Show Gist options
  • Save fjavier/8e99a5f15f01a96ba50f to your computer and use it in GitHub Desktop.
Save fjavier/8e99a5f15f01a96ba50f to your computer and use it in GitHub Desktop.
Configuracion de Proyecto Hibernate
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="connection.url">jdbc:postgresql://localhost/database</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgresPlusDialect</property>
<mapping class="inss.gob.ni.hibernate.model.FarmDetTablaCatalogoModel"/>
<mapping class="inss.gob.ni.hibernate.model.FarmTablaCatalogoModel"/>
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>
package inss.gob.ni.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
/**
* Created by fbriceno on 11/09/2014.
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static final SessionFactory buildSessionFactory(){
try {
//Creando el sesion Factory del Hibernate.cfg.xml
return new AnnotationConfiguration().configure().buildSessionFactory();
}catch (Throwable e){
System.out.println("Error al crear la sessionFactory");
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
public static Session openSession(){
return sessionFactory.openSession();
}
public static Session getCurrentSession(){
return getSessionFactory().getCurrentSession();
}
public static void closeSession(Session session){
session.close();
}
}
package inss.gob.ni.hibernate;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
* Created by fbriceno on 11/09/2014.
*/
public class HibernateUtilWithPersistence {
private EntityManagerFactory entityManagerFactory;
private EntityManager entityManager;
private String PERSISTENCE_UNIT_NAME = "experimentPersistenceUnit";
public void initEntityManager(){
entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
entityManager = entityManagerFactory.createEntityManager();
System.out.println("EntityManager Satisfactoriamente creado");
}
public void closeEntityManager(){
entityManager.close();
System.out.println("EntityManager Satisfactoriamente Cerrado");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="experimentPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<description>
Experimento de algoritmos de coincidencias entre valores de la BD y nuevos valores
</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>inss.gob.ni.hibernate.model.FarmDetTablaCatalogoModel</class>
<class>inss.gob.ni.hibernate.model.FarmTablaCatalogoModel</class>
<properties>
<property name="javax.persistence.validation.mode" value="NONE" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhos:5432/database"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.default_schema" value="public"/>
<property name="hibernate.connection.username" value="usuario"/>
<property name="hibernate.connection.password" value="password"/>
</properties>
</persistence-unit>
</persistence>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>inss.gob.ni.experimento</groupId>
<artifactId>experimento-match</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>experimento-match</name>
<url>http://maven.apache.org</url>
<dependencies>
<!--Hibernate-->
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.1.Final</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.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!--Dependencia del Hibernate -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
<!-- Fin de Dependencia de Hibernate -->
<!-- Libreria implementada para comparacion de cadenas -->
<dependency>
<groupId>uk.ac.shef.wit</groupId>
<artifactId>simmetrics</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<!-- DAO generic Search-->
<dependency>
<groupId>com.googlecode.genericdao</groupId>
<artifactId>search-hibernate</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.googlecode.genericdao</groupId>
<artifactId>search-jpa-hibernate</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</project>
@fjavier
Copy link
Author

fjavier commented Sep 12, 2014

Archivos Genericos para la configuracion de Proyectos con Hibernate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment