Skip to content

Instantly share code, notes, and snippets.

@fede-vaccaro
Last active September 28, 2019 11:21
Show Gist options
  • Save fede-vaccaro/d7e0f0a43e7f4cd6bd6072360e8d9f2d to your computer and use it in GitHub Desktop.
Save fede-vaccaro/d7e0f0a43e7f4cd6bd6072360e8d9f2d to your computer and use it in GitHub Desktop.
Testing with Docker - PostgreSQL
docker run --name postgres_db --rm -e POSTGRES_USER=docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=docker -p 5432:5432 postgres:10
<?xml version="1.0" encoding="UTF-8"?>
<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="test-persistence-unit"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.unifiprojects.app.appichetto.models.User</class>
<class>com.unifiprojects.app.appichetto.models.Item</class>
<class>com.unifiprojects.app.appichetto.models.Receipt</class>
<class>com.unifiprojects.app.appichetto.models.Accounting</class>
<properties>
<property name="javax.persistence.validation.mode"
value="AUTO" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.PostgreSQL95Dialect" /> <!-- DB Dialect -->
<property name="javax.persistence.jdbc.driver"
value="org.postgresql.Driver" /> <!-- DB Driver -->
<property name="hibernate.connection.url"
value="jdbc:postgresql://localhost:5432/docker" />
<property name="hibernate.connection.username"
value="docker" />
<property name="hibernate.connection.password"
value="docker" />
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"
/> -->
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212.jre6</version>
</dependency>
@Before
public void createUserRepository() {
entityManager.getTransaction().begin();
entityManager.createNativeQuery("do\n" +
"$$\n" +
"declare\n" +
" l_stmt text;\n" +
"begin\n" +
" select 'truncate ' || string_agg(format('%I.%I', schemaname, tablename), ',')\n" +
" into l_stmt\n" +
" from pg_tables\n" +
" where schemaname in ('public');\n" +
"\n" +
" execute l_stmt;\n" +
"end;\n" +
"$$").executeUpdate();
entityManager.getTransaction().commit();
userRepository = new UserRepositoryHibernate(entityManager);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment