Skip to content

Instantly share code, notes, and snippets.

@jenglamlow
Created July 20, 2017 15:14
Show Gist options
  • Select an option

  • Save jenglamlow/726ccd8f552e3644547bea0152abebb5 to your computer and use it in GitHub Desktop.

Select an option

Save jenglamlow/726ccd8f552e3644547bea0152abebb5 to your computer and use it in GitHub Desktop.
Maven Hibernate Example Project
<!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>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL95Dialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/pgtest</property>
<!-- Suppress Disabling Contextual LOB creation as createClob() method threw error message -->
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
<property name="hbm2ddl.auto">create</property> <!-- create / create-drop / update -->
<property name="show_sql">true</property> <!-- Show SQL in console -->
<property name="format_sql">true</property> <!-- Show SQL formatted -->
<!-- List of class to map -->
<mapping class="com.jeng.pgtest.model.Host"/>
</session-factory>
</hibernate-configuration>
package com.jeng.pgtest.model;
import javax.persistence.*;
@Entity
@Table(name = "HOST")
public class Host {
@Id @GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "hostname")
private String hostname;
@Column(name = "platform")
private String platform;
public Host() {}
public Host(String hostname, String platform) {
this.hostname = hostname;
this.platform = platform;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
}
package com.jeng.pgtest.app;
import com.jeng.pgtest.model.Host;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args){
System.out.println("Hello World");
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Host host = new Host("klrx001", "RedHat");
session.save(host);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
<?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.jeng</groupId>
<artifactId>pgtest</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.jeng.pgtest.app.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.3</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment