Commit c637f9cf authored by Dr. Martin Goik's avatar Dr. Martin Goik

Mysql --> Postgresql, Querydsl upgrade

parent 79a3e612
......@@ -13,8 +13,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>5.3.7.Final</hibernate.version>
<querydsl.version>3.7.4</querydsl.version>
<hibernate.version>5.4.1.Final</hibernate.version>
<querydsl.version>4.2.1</querydsl.version>
</properties>
<dependencies>
......@@ -36,22 +36,21 @@
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
<dependency>
......@@ -90,6 +89,11 @@
</dependencies>
<build>
<resources>
<resource>
<directory>${project.build.directory}/metamodel</directory>
</resource>
</resources>
<plugins>
<plugin>
......@@ -116,7 +120,7 @@
<phase>generate-sources</phase>
<configuration>
<outputDirectory>${project.build.directory}/metamodel</outputDirectory>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
<options>
<querydsl.includedPackages>de.hdm_stuttgart.mi.sda1.model</querydsl.includedPackages>
</options>
......@@ -193,4 +197,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
\ No newline at end of file
package de.hdm_stuttgart.mi.sda1;
import com.mysema.query.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import de.hdm_stuttgart.mi.sda1.model.QStudyCourse;
import de.hdm_stuttgart.mi.sda1.model.StudyCourse;
import javax.persistence.*;
import java.util.List;
/**
* Read a {@link StudyCourse} by its primary key value.
......@@ -22,21 +21,23 @@ public class QuerydslReadLikeCstudyCourses {
public static void main( String[] args ) {
final QStudyCourse qStudyCourse = QStudyCourse.studyCourse;
{
final List<StudyCourse> studyCourses =
new JPAQuery(entityManager).
from(qStudyCourse).
list(qStudyCourse);
studyCourses.stream().map(s -> "All: Read '" + s + "'").forEach(System.out::println);
}
new JPAQuery(entityManager).
from(qStudyCourse).
where(qStudyCourse.shortName.like("C%")).
list(qStudyCourse).
stream().
map(s -> "Like C%: Read '" + s + "'").
forEach(System.out::println);
final JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);
queryFactory.
selectFrom(qStudyCourse).
fetch().
stream().
map(s -> "All: Read '" + s + "'").
forEach(System.out::println);
queryFactory.
selectFrom(qStudyCourse).
where(qStudyCourse.shortName.like("C%")).
fetch().
stream().
map(s -> "Like C%: Read '" + s + "'").
forEach(System.out::println);
// See http://stackoverflow.com/questions/21645516/program-using-hibernate-does-not-terminate
factory.close();
......
......@@ -33,8 +33,8 @@
<!--
JDBC database connection parameter
-->
<property name = "javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name = "javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hdm?useSSL=false"/>
<property name = "javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name = "javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/hdm"/>
<property name = "javax.persistence.jdbc.user" value="hdmuser"/>
<property name = "javax.persistence.jdbc.password" value="XYZ"/>
......@@ -55,8 +55,7 @@
<property name = "hibernate.format_sql" value="true"/>
<property name = "hibernate.use_sql_comments" value="true"/>
<!-- Choose Mysql. Set »innodb« backend by system property -->
<property name = "hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect" />
<property name = "hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect" />
<!-- Enable Hibernate scanning for entity classes and adding them automatically
but not for hbm.xml files. -->
......@@ -73,8 +72,8 @@
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name = "javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name = "javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hdm?useSSL=false"/>
<property name = "javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name = "javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/hdm"/>
<property name = "javax.persistence.jdbc.user" value="hdmuser"/>
<property name = "javax.persistence.jdbc.password" value="XYZ"/>
......@@ -85,9 +84,7 @@
<property name = "hibernate.show_sql" value = "false" />
<property name = "hibernate.format_sql" value="true"/>
<property name = "hibernate.use_sql_comments" value="true"/>
<property name = "hibernate.dialect.storage_engine" value="innodb" />
<property name = "hibernate.archive.autodetection" value="class"/>
</properties>
......
......@@ -13,8 +13,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>5.3.7.Final</hibernate.version>
<querydsl.version>3.7.4</querydsl.version>
<hibernate.version>5.4.1.Final</hibernate.version>
<querydsl.version>4.2.1</querydsl.version>
</properties>
<dependencies>
......@@ -25,6 +25,9 @@
<version>${hibernate.version}</version>
</dependency>
<!--
https://stackoverflow.com/questions/27412287/junit-tests-fail-with-java-error-when-using-intellij-within-maven-module-after-a#answer-34374834
-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
......@@ -33,9 +36,21 @@
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
<dependency>
......@@ -45,7 +60,7 @@
<scope>test</scope>
</dependency>
<dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
......@@ -57,19 +72,6 @@
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
......@@ -83,9 +85,15 @@
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>${project.build.directory}/metamodel</directory>
</resource>
</resources>
<plugins>
<plugin>
......@@ -112,7 +120,7 @@
<phase>generate-sources</phase>
<configuration>
<outputDirectory>${project.build.directory}/metamodel</outputDirectory>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
<options>
<querydsl.includedPackages>de.hdm_stuttgart.mi.sda1.model</querydsl.includedPackages>
</options>
......@@ -124,7 +132,8 @@
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>3.2.0</version>
<version>3.3.3</version>
<executions>
<execution>
<id>process</id>
......@@ -163,12 +172,6 @@
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
......@@ -176,7 +179,7 @@
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>de.hdm_stuttgart.mi.sda1.CreateStudyCourse</Main-Class>
</manifestEntries>
......@@ -194,4 +197,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
\ No newline at end of file
package de.hdm_stuttgart.mi.sda1;
import com.mysema.query.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import de.hdm_stuttgart.mi.sda1.model.QStudyCourse;
import de.hdm_stuttgart.mi.sda1.model.StudyCourse;
import javax.persistence.*;
import java.util.List;
/**
* Read a {@link StudyCourse} by its primary key value.
......@@ -22,21 +21,23 @@ public class QuerydslReadLikeCstudyCourses {
public static void main( String[] args ) {
final QStudyCourse qStudyCourse = QStudyCourse.studyCourse;
{
final List<StudyCourse> studyCourses =
new JPAQuery(entityManager).
from(qStudyCourse).
list(qStudyCourse);
studyCourses.stream().map(s -> "All: Read '" + s + "'").forEach(System.out::println);
}
new JPAQuery(entityManager).
from(qStudyCourse).
where(qStudyCourse.shortName.like("C%")).
list(qStudyCourse).
stream().
map(s -> "Like C%: Read '" + s + "'").
forEach(System.out::println);
final JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);
queryFactory.
selectFrom(qStudyCourse).
fetch().
stream().
map(s -> "All: Read '" + s + "'").
forEach(System.out::println);
queryFactory.
selectFrom(qStudyCourse).
where(qStudyCourse.shortName.like("C%")).
fetch().
stream().
map(s -> "Like C%: Read '" + s + "'").
forEach(System.out::println);
// See http://stackoverflow.com/questions/21645516/program-using-hibernate-does-not-terminate
factory.close();
......
......@@ -21,7 +21,7 @@ public class ReadStudyCourseById {
*/
public static void main( String[] args ) {
final StudyCourse studyCourse = entityManager.find(StudyCourse.class, "CSM");
final StudyCourse studyCourse = entityManager.find(StudyCourse.class, (long)1);
System.out.println("Read '" + studyCourse + "'");
......
......@@ -22,7 +22,7 @@ public class UpdateStudyCourseById {
*/
public static void main( String[] args ) {
final StudyCourse csm = entityManager.find(StudyCourse.class, "CSM");
final StudyCourse csm = entityManager.find(StudyCourse.class, (long) 1);
final EntityTransaction tx = entityManager.getTransaction();
tx.begin();
......
......@@ -33,8 +33,8 @@
<!--
JDBC database connection parameter
-->
<property name = "javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name = "javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hdm?useSSL=false"/>
<property name = "javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name = "javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/hdm"/>
<property name = "javax.persistence.jdbc.user" value="hdmuser"/>
<property name = "javax.persistence.jdbc.password" value="XYZ"/>
......@@ -55,8 +55,7 @@
<property name = "hibernate.format_sql" value="true"/>
<property name = "hibernate.use_sql_comments" value="true"/>
<!-- Choose Mysql. Set »innodb« backend by system property -->
<property name = "hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect" />
<property name = "hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect" />
<!-- Enable Hibernate scanning for entity classes and adding them automatically
but not for hbm.xml files. -->
......@@ -73,8 +72,8 @@
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name = "javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name = "javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hdm?useSSL=false"/>
<property name = "javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name = "javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/hdm"/>
<property name = "javax.persistence.jdbc.user" value="hdmuser"/>
<property name = "javax.persistence.jdbc.password" value="XYZ"/>
......@@ -86,8 +85,6 @@
<property name = "hibernate.format_sql" value="true"/>
<property name = "hibernate.use_sql_comments" value="true"/>
<property name = "hibernate.dialect.storage_engine" value="innodb" />
<property name = "hibernate.archive.autodetection" value="class"/>
</properties>
......
......@@ -2,3 +2,5 @@
/.classpath
/.settings
/.project
*.iml
.idea
DROP TABLE IF EXISTS Friends;
CREATE TABLE Friends (
id INTEGER NOT NULL PRIMARY KEY
,nickname char(10)
......@@ -5,22 +7,13 @@ CREATE TABLE Friends (
);
DELETE FROM Friends;
INSERT INTO Friends VALUES
(1, 'Jim', '1991-10-10')
,(2, 'Eve', '2003-5-24')
,(3, 'Mick','2001-12-30');
DELETE FROM Friends;
INSERT INTO Friends VALUES
(1, 'Jim', '1991-10-10')
,(2, NULL, '2003-5-24')
,(3, 'Mick', '2001-12-30');
-- testing DB/Java type conversions
-- testing duplicate key constraint
INSERT INTO Friends VALUES (1, '31', '1991-10-10');
DROP TABLE Friends;
SELECT * FROM Friends;
\ No newline at end of file
......@@ -3,21 +3,21 @@ DROP TABLE IF EXISTS Person;
CREATE TABLE Person (
name char(80)
,email CHAR(20) UNIQUE
,email CHAR(20) PRIMARY KEY
);
DELETE FROM Person;
DELETE FROM Person where login = 'me';
SELECT * FROM Person;
-- Users may authenticate --
DROP TABLE IF EXISTS Person;
CREATE TABLE Person (
name char(80) NOT NULL
,email CHAR(20) NOT NULL UNIQUE
,login CHAR(10) UNIQUE -- login names must be unique --
,password TEXT -- hash values may be looooong! --
,login CHAR(10) PRIMARY KEY -- login names must be unique --
,password TEXT -- hash values may be looooong! --
);
SELECT * FROM Person WHERE login='fred' and password = 'sd' OR '1' = '1';
......@@ -25,3 +25,4 @@ SELECT * FROM Person WHERE login='fred' and password = 'sd' OR '1' = '1';
INSERT INTO Person VALUES('Fred Blix', 'fb@my.com', 'blix',
'HwX2DkuYiwp7xogm3AGndza8DKRVvCMntxRvCrCGFPw=$6Ix11yHNB4uPZuF2IQYxVV/MYragJwTDE33OIFR9a24=');
DELETE FROM Person where login = 'me';
\ No newline at end of file
......@@ -19,9 +19,9 @@
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.2</version>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
<dependency>
......@@ -86,4 +86,4 @@
</plugins>
</build>
</project>
</project>
\ No newline at end of file
......@@ -10,7 +10,7 @@ public class DbAccessDriver {
*/
public static void main(String[] args) {
final DbAccess dbaccess = new DbAccess();
dbaccess.connect("jdbc:mysql://localhost:3306/hdm?serverTimezone=UTC",
dbaccess.connect("jdbc:postgresql://localhost/hdm",
"hdmuser", "XYZ");
System.out.println(dbaccess.readPrice("x-223"));
System.out.println(dbaccess.readPrice("aaargh!"));
......
......@@ -73,7 +73,7 @@ public class XmlRdbms2Html {
private void addPrices() {
final List<Object> products = selectProducts.evaluate(catalog.getRootElement());
db.connect("jdbc:mysql://localhost:3306/hdm?serverTimezone=UTC", "hdmuser", "XYZ");
db.connect("jdbc:postgresql://localhost/hdm", "hdmuser", "XYZ");
for (Object p: products) {
final Element product = (Element ) p;
final String productId = product.getAttributeValue("id");
......
......@@ -9,7 +9,7 @@ import dom.xsl.DbAccess;
*/
public class HtmlEventHandler extends DefaultHandler{
public void startDocument() {
dbaccess.connect("jdbc:mysql://localhost:3306/hdm?serverTimezone=UTC",
dbaccess.connect("jdbc:postgresql://localhost/hdm",
"hdmuser", "XYZ");
System.out.println("<html><head><title>Catalog</title></head>");
}
......
......@@ -13,9 +13,9 @@ public class SimpleInsert {
// Step 1: Open a connection to the database server
final Connection conn = DriverManager.getConnection (
"jdbc:mysql://localhost:3306/hdm?serverTimezone=UTC", //JDBC URL
"hdmuser", // Username
"XYZ"); // Password
"jdbc:postgresql://localhost/hdm", //JDBC URL
"hdmuser", // Username
"XYZ"); // Password
final DbWrite write = new SecureWrite();
write.save(conn, "Eve", "eve@cs.com");
......
......@@ -136,7 +136,7 @@ public class PersistenceHandler {
*
* <dt><b>Precondition:</b></dt>
* <dd>The following properties must be set:
* <pre>PersistenceHandler.jdbcUrl=jdbc:mysql://localhost:3306/hdm?serverTimezone=UTC
* <pre>PersistenceHandler.jdbcUrl=jdbc:postgresql://localhost/hdm
PersistenceHandler.password=XYZ
PersistenceHandler.username=foo</pre>
* </dd>
......
......@@ -85,7 +85,7 @@ public class PersistenceHandler {
*
* <dt><strong>Precondition:</strong></dt>
* <dd>The following properties must be set:
* <pre>PersistenceHandler.jdbcUrl=jdbc:mysql://localhost:3306/hdm?serverTimezone=UTC
* <pre>PersistenceHandler.jdbcUrl=jdbc:postgresql://localhost/hdm
PersistenceHandler.password=XYZ
PersistenceHandler.username=foo</pre>
* </dd>
......
PersistenceHandler.jdbcUrl=jdbc:mysql://localhost:3306/hdm?allowMultiQueries=true&serverTimezone=UTC
PersistenceHandler.jdbcUrl=jdbc:postgresql://localhost/hdm
PersistenceHandler.password=XYZ
PersistenceHandler.username=hdmuser
......@@ -27,10 +27,10 @@ import java.sql.Statement;
*/
public class PersistenceHandler {
Connection conn = null;
Statement stmt = null;
private Connection conn = null;
private Statement stmt = null;
String errorMessage = null;
private String errorMessage = null;
/**
* New instances are in <strong>disconnected</strong> state. See {@link #isConnected()}
......@@ -85,7 +85,7 @@ public class PersistenceHandler {
*
* <dt><b>Precondition:</b></dt>
* <dd>The following properties must be set:
* <pre>PersistenceHandler.jdbcUrl=jdbc:mysql://localhost:3306/hdm?serverTimezone=UTC
* <pre>PersistenceHandler.jdbcUrl=jdbc:postgresql://localhost/hdm
PersistenceHandler.password=XYZ
PersistenceHandler.username=foo</pre>
* </dd>
......
PersistenceHandler.jdbcUrl=jdbc:mysql://localhost:3306/hdm?serverTimezone=UTC
PersistenceHandler.jdbcUrl=jdbc:postgresql://localhost/hdm
PersistenceHandler.username=hdmuser
PersistenceHandler.password=XYZ
......@@ -29,10 +29,10 @@ import sda.jdbc.intro.v1.DbProps;
*/
public class PersistenceHandler {
Connection conn = null;
PreparedStatement pstmt;
private Connection conn = null;
private PreparedStatement pstmt;
String errorMessage = null;
private String errorMessage = null;
/**
* New instances are in <strong>disconnected</strong> state. See {@link #isConnected()}
......@@ -86,7 +86,7 @@ public class PersistenceHandler {
*
* <dt><b>Precondition:</b></dt>
* <dd>The following properties must be set:
* <pre>PersistenceHandler.jdbcUrl=jdbc:mysql://localhost:3306/hdm
* <pre>PersistenceHandler.jdbcUrl=jdbc:postgresql://localhost/hdm
PersistenceHandler.password=XYZ
PersistenceHandler.username=foo</pre>
* </dd>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment