diff --git a/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/.gitignore b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..1a547d1407d6392e803467dbd32acbfefd36ac5f --- /dev/null +++ b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/.gitignore @@ -0,0 +1,54 @@ +/target/ +/.settings/ +.classpath +.project + +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties \ No newline at end of file diff --git a/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/Readme.md b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/Readme.md new file mode 100644 index 0000000000000000000000000000000000000000..b07ac459245099d2e00d0731dd240054dbf06f1b --- /dev/null +++ b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/Readme.md @@ -0,0 +1,52 @@ +Hibernate JPA 2 project template +================================ + +Usage: +------ +A basic JPA CRUD example + +General: +-------- +This project contains: + +* `src/main/resources/META-INF/persistence.xml` + + Two persistence units offering a 禄drop-and-create芦 + (re-creating schema) and a 禄strategy_none芦 (don't touch)JDBC database + connection to a Mysql 5 server. You may want to adjust both database + type and connection parameters. + +* Important classes (package root `de.hdm_stuttgart.mi.sda1` omitted + for brevity): + +|File / Class |Description | +|--------------------------|-------------------------------------------| +|`...model.StudyCourse` |Class representing study courses | +|`...CreateStudyCourse` |Persisting a study course | +|`...ReadStudyCourseById` |Retrieving a study course database instance by primary key | + +* `src/main/resources/log4j2.xml` Configuring the log4j subsystem. + +On executing i.e. 禄`mvn install`芦 Maven will +[generate] +(https://docs.jboss.org/hibernate/orm/current/topical/html_single/metamodelgen/MetamodelGenerator.html) +metamodel classes like `StudyCourse_.java` during the +禄`generate-sources`芦 phase. These classes are being required for +[typesafe criteria based queries] +(http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/querycriteria.html#querycriteria-typedquery) +not being used in this project. + + +Targets: +-------- + +* `mvn test` + Creating and re-reading a database instance of `StudyCourse`. + Connection parameters being defined twice in two persistence units in + `src/main/resources/META-INF/persistence.xml`. + +* `mvn package` + Creating an executable jar persisting an `StudyCourse` instance to be + run like e.g.: + + java -jar target/intro-1.0.jar \ No newline at end of file diff --git a/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/pom.xml b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..40b8bf925dce3a9e36c0bfb0ecd7858f7f032f24 --- /dev/null +++ b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/pom.xml @@ -0,0 +1,152 @@ +<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>de.hdm_stuttgart.mi.sda1</groupId> + <artifactId>studycourse_basic</artifactId> + <version>1.0</version> + <packaging>jar</packaging> + + <name>studycourse_basic</name> + + <url>http://www.mi.hdm-stuttgart.de/freedocs/topic/de.hdm_stuttgart.mi.lectures/sd1SectUsingMaven.html</url> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-core</artifactId> + <version>5.2.12.Final</version> + </dependency> + + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <version>6.0.6</version> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <version>2.10.0</version> + </dependency> + + <!-- Required for executable jar generation to avoid ClassNotFoundException: + com.fasterxml.jackson.core.type.TypeReference and similar dependency problems. --> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <version>2.9.3</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> + <version>5.2.12.Final</version> + <scope>provided</scope> + </dependency> + + </dependencies> + + <build> + <plugins> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.7.0</version> + <configuration> + <source>1.8</source> + <target>1.8</target> + <compilerArgument>-proc:none</compilerArgument> + </configuration> + </plugin> + + <plugin> + <groupId>org.bsc.maven</groupId> + <artifactId>maven-processor-plugin</artifactId> + <version>3.2.0</version> + + <executions> + <execution> + <id>process</id> + <goals> + <goal>process</goal> + </goals> + <phase>generate-sources</phase> + <configuration> + <outputDirectory>${project.build.directory}/metamodel</outputDirectory> + <processors> + <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor> + </processors> + </configuration> + </execution> + </executions> + </plugin> + + <!-- Build helper plugin to add generated sources to classpath --> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>build-helper-maven-plugin</artifactId> + <version>3.0.0</version> + <executions> + <execution> + <id>add-source</id> + <phase>generate-sources</phase> + <goals> + <goal>add-source</goal> + </goals> + <configuration> + <sources> + <source>${project.build.directory}/metamodel</source> + </sources> + </configuration> + </execution> + </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> + <version>3.1.0</version> + <configuration> + <transformers> + <transformer + implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> + <manifestEntries> + <Main-Class>de.hdm_stuttgart.mi.sda1.CreateStudyCourse</Main-Class> + </manifestEntries> + </transformer> + </transformers> + </configuration> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/src/main/java/de/hdm_stuttgart/mi/sda1/CreateStudyCourse.java b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/src/main/java/de/hdm_stuttgart/mi/sda1/CreateStudyCourse.java new file mode 100644 index 0000000000000000000000000000000000000000..4abbf3026982bd840fa8c6dce4a3a81b16204aa4 --- /dev/null +++ b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/src/main/java/de/hdm_stuttgart/mi/sda1/CreateStudyCourse.java @@ -0,0 +1,44 @@ + package de.hdm_stuttgart.mi.sda1; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import de.hdm_stuttgart.mi.sda1.model.StudyCourse; + + /** + * Create a transient {@link StudyCourse} instance and persist it to the database. + */ + public class CreateStudyCourse { + + static private final Logger log = LogManager.getLogger(CreateStudyCourse.class); + + /** + * @param args Unused + */ + public static void main( String[] args ) { + + final EntityManagerFactory factory = Persistence.createEntityManagerFactory( + "strategy_drop-and-create"); + final EntityManager em = factory.createEntityManager(); + + log.info("Begin transaction"); + + final EntityTransaction tx = em.getTransaction(); + + tx.begin(); + final StudyCourse csm = new StudyCourse( + "CSM", "Computer Science an Media"); + + em.persist(csm); + tx.commit(); + + log.info("Committed transaction"); + + // See http://stackoverflow.com/questions/21645516/program-using-hibernate-does-not-terminate + factory.close(); + } +} \ No newline at end of file diff --git a/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/src/main/java/de/hdm_stuttgart/mi/sda1/ReadStudyCourseById.java b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/src/main/java/de/hdm_stuttgart/mi/sda1/ReadStudyCourseById.java new file mode 100644 index 0000000000000000000000000000000000000000..7b78907d71a5e4d8d4436f79df531b4ec684456a --- /dev/null +++ b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/src/main/java/de/hdm_stuttgart/mi/sda1/ReadStudyCourseById.java @@ -0,0 +1,35 @@ +package de.hdm_stuttgart.mi.sda1; + +import de.hdm_stuttgart.mi.sda1.model.StudyCourse; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; + +/** + * Read a {@link StudyCourse} by its primary key value. + */ +public class ReadStudyCourseById { + /** + * @param args unused + */ + public static void main( String[] args ) { + + final EntityManagerFactory factory = Persistence.createEntityManagerFactory( + "strategy_none"); + final EntityManager em = factory.createEntityManager(); + + final EntityTransaction tx = em.getTransaction(); + + final StudyCourse studyCourse; + tx.begin(); + studyCourse = em.find(StudyCourse.class, "CSM"); + tx.commit(); + + System.out.println("Read + '" + studyCourse + "'"); + + // See http://stackoverflow.com/questions/21645516/program-using-hibernate-does-not-terminate + factory.close(); + } +} \ No newline at end of file diff --git a/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/src/main/resources/META-INF/persistence.xml b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000000000000000000000000000000000..e99ab49751b506b794b3f1728342499ec2e035fb --- /dev/null +++ b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,96 @@ +<persistence + version="2.1" + xmlns="http://xmlns.jcp.org/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence + http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> + + <!-- + The <code>persistence.xml</code> file configures at least one persistence unit; + each unit must have a unique name. + --> + <persistence-unit name = "strategy_drop-and-create"> + + <!-- + Hibernate will scan your classpath for mapped classes and add them automatically + to your persistence unit. + --> + <exclude-unlisted-classes>false</exclude-unlisted-classes> + + <!-- + setting the previous option "exclude-unlisted-classes" to 'true' requires entity classes to + be listed explicitely. You may want to uncomment the following definition and add more classes. + + <class>sda1.Airline</class> + --> + + <!-- + Standard or vendor-specific options can be set as properties on a persistence unit. + Any standard properties have the <code>javax.persistence</code> name prefix, Hibernate's + settings use <code>hibernate</code> + --> + <properties> + <!-- + 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.user" value="hdmuser"/> + <property name = "javax.persistence.jdbc.password" value="XYZ"/> + + <!-- + The JPA engine should drop and re-create the SQL schema in the database + automatically when it boots. This is ideal for automated testing, when + you want to work with a clean database for every test run. + --> + <property + name = "javax.persistence.schema-generation.database.action" + value="drop-and-create"/> + + <!-- + When printing SQL in logs, let Hibernate format the SQL nicely and generate + comments into the SQL string so we know why Hibernate executed the SQL statement. + --> + <property name = "hibernate.show_sql" value = "true" /> + <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" /> + + <!-- Enable Hibernate scanning for entity classes and adding them automatically + but not for hbm.xml files. --> + <property name = "hibernate.archive.autodetection" value="class"/> + + </properties> + </persistence-unit> + + <!-- The subsequent persistence unit won't modify the database's schema + javax.persistence.schema-generation.database.action=none + --> + <persistence-unit name = "strategy_none"> + + <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.user" value="hdmuser"/> + <property name = "javax.persistence.jdbc.password" value="XYZ"/> + + <property + name = "javax.persistence.schema-generation.database.action" + value="none"/> + + <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> + </persistence-unit> + +</persistence> diff --git a/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/src/main/resources/log4j2.xml b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/src/main/resources/log4j2.xml new file mode 100644 index 0000000000000000000000000000000000000000..4683bf39882bd835c5bba31ea5140a43ee577a69 --- /dev/null +++ b/Doc/Sda1/Ref/JpaIntro/P/StudyCourse_basic/src/main/resources/log4j2.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="UTF-8"?> +<Configuration> + <Appenders> + <File name="A1" fileName="A1.log" append="false"> + <PatternLayout pattern="%t %-5p %c{2} - %m%n"/> + </File> + <Console name="STDOUT" target="SYSTEM_OUT"> + <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/> + </Console> + </Appenders> + <Loggers> + <!-- You may want to enable more detailed hibernate logging --> + <Logger name="org.hibernate" level="info"> + <AppenderRef ref="A1"/> + </Logger> + <Root level="info"> + <AppenderRef ref="STDOUT"/> + </Root> + </Loggers> +</Configuration> \ No newline at end of file