1. Obten los archivos binarios en un zip o tgz del sitio de postgresql.
2. Ve al folder bin de PostgreSQL. Corre la siguiente herramienta para iniciar la base de datos.
$ initdb -U postgres -A password -E utf8 -W -D C:\Users\750802\Documents\pgsql\data
3. Iniciar el servidor de base de datos.
"pg_ctl" -D "C:\Users\750802\Documents\pgsql\data" -l logfile start
4. Para detener la base de datos.
"pg_ctl" -D "C:\Users\750802\Documents\pgsql\data" -l logfile stop
5. Crea un proyecto maven para hibernate y lo importas en eclipse
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DgroupId=com.luis.tutorial -DartifactId=01-hibernate-conf
6. Agrega estas dependencias en el archivo pom.xml
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.11.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
</dependency>
7. Agrega hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Connection settings -->
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/luis</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">patito</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Print executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create all database on startup -->
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<!-- Annotated entity classes -->
<mapping class="com.luis.tutorial.AppUser" />
</session-factory>
</hibernate-configuration>
8. Crea las siguientes clases:
package com.luis.tutorial;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class AppUser implements Serializable {
public AppUser() {
};
public AppUser(String login) {
this.login = login;
};
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String login;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
}
package com.luis.tutorial;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Unit test for simple App.
*/
public class AppTest {
public static void main(String[] str) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
AppUser user = new AppUser("firstuser");
session.save(user);
session.getTransaction().commit();
session.close();
}
}
No hay comentarios:
Publicar un comentario