diff --git a/Doc/Sda1/jpaintro.xml b/Doc/Sda1/jpaintro.xml index 847090246b3ad33c41ca1e78a06552c374e90a3b..a784988860baeb79f9ff4b3ea616a730fca5b206 100644 --- a/Doc/Sda1/jpaintro.xml +++ b/Doc/Sda1/jpaintro.xml @@ -16,131 +16,99 @@ <qandadiv> <qandaentry> <question> - <para>Import the following <xref linkend="glo_JPA"/> Maven based - project into your Eclipse workspace:</para> - - <annotation role="make"> - <para role="eclipse">Sda1/Jpa/HibernateStandalone</para> - </annotation> - - <para>This project is based on the <code - xlink:href="https://maven.mi.hdm-stuttgart.de/Archetypes/de/hdm-stuttgart/mi/mi-maven-archetype-jpa-hibernate">mi-maven-archetype-jpa-hibernate</code> - Maven archetype and contains the following resources:</para> - - <glosslist> - <glossentry> - <glossterm><filename>src/main/resources/META-INF/persistence.xml</filename></glossterm> - - <glossdef> - <para>A persistence unit definition being providing access - to the local <productname>Mysql</productname> database - server.</para> - - <para>The parameter - <parameter>hibernate.hbm2ddl.auto</parameter> causes table - re-creation on every new invocation and may be adjusted - according to <quote - xlink:href="https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch03.html#configuration-misc-properties">Miscellaneous - Properties</quote>.</para> - </glossdef> - </glossentry> - - <glossentry> - <glossterm><classname>de.hdm_stuttgart.mi.sda1.hiberjpa.model.Lecture</classname></glossterm> - - <glossdef> - <para>A <xref linkend="glo_JPA"/> entity class carrying - <xref linkend="glo_JPA"/> persistence annotations.</para> - </glossdef> - </glossentry> - - <glossentry> - <glossterm><classname>de.hdm_stuttgart.mi.sda1.hiberjpa.PersistDriver</classname></glossterm> - - <glossdef> - <para>An executable class persisting a single - <classname>Lecture</classname> instance.</para> - </glossdef> - </glossentry> - - <glossentry> - <glossterm><filename>pom.xml</filename></glossterm> - - <glossdef> - <para>The dependency section does contain Hibernate / <xref - linkend="glo_JPA"/> related entries.</para> - </glossdef> - </glossentry> - </glosslist> + <para>Read the <productname + xlink:href="https://maven.mi.hdm-stuttgart.de/archetype-catalog.xml">mi-maven-archetype-jpa-hibernate</productname> + archetype's <link + xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/blob/master/ws/Archetypes/mi-maven-archetype-jpa-hibernate/src/main/resources/archetype-resources/Readme.md#eclipse-ide-users">documentation</link> + and create a <xref linkend="glo_Maven"/> from it <link + linkend="sd1SectUsingMavenEclipse">using the MI archetype + catalog</link>. For the sake of a simplified discussion regarding + default package names choose the following + <filename>pom.xml</filename> configuration:</para> + + <programlisting language="xml"><project xmlns=...> + + <groupId>jpaintro</groupId> + <artifactId>university</artifactId> + ... +</project></programlisting> <para>Tasks:</para> <orderedlist> <listitem> <para>Execute - <classname>de.hdm_stuttgart.mi.sda1.hiberjpa.PersistDriver</classname> - and watch the generated <xref linkend="glo_SQL"/> + <classname>jpaintro.university.CreateAirline</classname> and + watch the generated <xref linkend="glo_SQL"/> statements.</para> + + <tip> + <para>Configure <filename>log4j2.xml</filename> using log + level <quote>debug</quote>.</para> + </tip> </listitem> <listitem> <para>Analyze the generated database schema.</para> <tip> - <para><code>SHOW CREATE TABLE Lecture</code> is your - friend.</para> + <para><code language="sql">SHOW CREATE TABLE Airline</code> + is your friend.</para> + </tip> + </listitem> + + <listitem> + <para>Execute + <classname>jpaintro.university.SearchByIcao</classname>. + Visualize the <xref linkend="glo_JPA"/> generated <xref + linkend="glo_SQL"/> statements and explain the <xref + linkend="glo_JDBC"/> layer messages including search parameter + values.</para> + + <tip> + <para>Configure <filename>log4j2.xml</filename> using log + level <quote>trace</quote>.</para> </tip> </listitem> <listitem> - <para>Write an application which persists the following three - <classname>Lecture</classname> instances to the - database:</para> + <para>Replace the existing data model in favour of an + application persisting <classname>Lecture</classname> + instances. Each lecture will be described by its title e.g. + <quote>Structured Data and Applications</quote>. Persist the + following three lectures:</para> <itemizedlist> <listitem> - <para>Structured data and applications 1</para> + <para>Structured data and applications</para> </listitem> <listitem> - <para>Software development 1</para> + <para>Software development</para> </listitem> <listitem> <para>Database systems</para> </listitem> </itemizedlist> + + <tip> + <para>Do not forget to build your meta model class + <classname>Lecture_</classname> and manually delete the + existing <classname>Airline_</classname> (unless + re-factoring <classname>Airline</classname> to + <classname>Lecture</classname>).</para> + </tip> </listitem> <listitem> - <para>Code a second application which retrieves all lectures - from the database having a <property>name</property> property - starting with <quote>S</quote>.</para> + <para>Modify + <classname>jpaintro.university.SearchByIcao</classname> to + retrieve all lectures from the database having a title + starting with the letter <quote>S</quote>.</para> <tip> <itemizedlist> - <listitem> - <para>You may want to open - <filename>persistence.xml</filename> using Eclipse's - built in <quote>Persistence XML editor</quote> rather - than a generic one.</para> - </listitem> - - <listitem> - <para>You will need a second <persistence-unit> - having a different <parameter>name</parameter> parameter - defining <code><property - name="hibernate.hbm2ddl.auto" value="update" - /></code>. This avoids table re-creation at - application startup resulting in empty datasets.</para> - </listitem> - - <listitem> - <para>The bogus warning <quote>Multiple persistence - units defined - only the first persistence unit will be - recognized</quote> may be safely ignored.</para> - </listitem> - <listitem> <para>Read the <link xlink:href="https://www.objectdb.com/java/jpa/query/api">TypedQuery @@ -153,80 +121,68 @@ <listitem> <para>A <quote>best practices</quote> advice recommends - supplying a business key in addition to a (surrogate) primary - key. Read <link + supplying a business key in addition to a <link + xlink:href="https://en.wikipedia.org/wiki/Surrogate_key">surrogate + primary key</link>. Read <link xlink:href="https://docs.oracle.com/javaee/7/api/javax/persistence/Column.html">Annotation - Type Column</link> and define a unique, non-null property - <property>nr</property> in your <classname>Lecture</classname> - class.</para> + Type Column</link> and define a unique, non-null integer + property <property>courseNumber</property> in your + <classname>Lecture</classname> class.</para> <para>Check your generated <productname>Mysql</productname> schema.</para> </listitem> + + <listitem> + <para>Query for all lectures starting with the letter + <quote>S</quote> and having a + <property>courseNumber</property> greater than 2000.</para> + </listitem> </orderedlist> </question> <answer> <orderedlist> <listitem> - <para>Execution of predefined class - <classname>PersistDriver</classname> yields:</para> + <para>Execution of class + <classname>jpaintro.university.CreateAirline</classname> + yields:</para> - <programlisting language="none">Jun 23, 2015 10:02:54 AM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation -INFO: HHH000204: Processing PersistenceUnitInfo [ - name: jpa-recreate <co linkends="sda1HibernateSchemaLog-1" + <programlisting language="none">... +DEBUG [main] util.LogHelper (LogHelper.java:102) - PersistenceUnitInfo [ +name: strategy_drop-and-create <co linkends="sda1HibernateSchemaLog-1" xml:id="sda1HibernateSchemaLog-1-co"/> - ...] ... -INFO: HHH000206: hibernate.properties not found <co +INFO [main] cfg.Environment (Environment.java:213) - HHH000206: hibernate.properties not found <co linkends="sda1HibernateSchemaLog-2" xml:id="sda1HibernateSchemaLog-2-co"/> ... -Jun 23, 2015 10:02:55 AM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure -INFO: HHH010002: C3P0 using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/hdm -Jun 23, 2015 10:02:55 AM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure -INFO: HHH000046: Connection properties: {user=hdmuser, password=****} <co +INFO [main] ... Connection properties: {user=hdmuser, password=XYZ} <co linkends="sda1HibernateSchemaLog-3" xml:id="sda1HibernateSchemaLog-3-co"/> -Jun 23, 2015 10:02:55 AM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure -INFO: HHH000006: Autocommit mode: false <co +INFO [main] ... Autocommit mode: false <co linkends="sda1HibernateSchemaLog-4" xml:id="sda1HibernateSchemaLog-4-co"/> -Jun 23, 2015 10:02:55 AM com.mchange.v2.log.MLog <clinit> -INFO: MLog clients using java 1.4+ standard logging. -Jun 23, 2015 10:02:55 AM com.mchange.v2.c3p0.C3P0Registry banner -INFO: Initializing c3p0-0.9.2.1 [built 20-March-2013 10:47:27 +0000; debug? true; trace: 10] -Jun 23, 2015 10:02:55 AM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager ... -INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect <co +INFO [main] dialect.Dialect ... Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect <co linkends="sda1HibernateSchemaLog-5" xml:id="sda1HibernateSchemaLog-5-co"/> ... -Jun 23, 2015 10:02:55 AM org.hibernate.tool.hbm2ddl.SchemaExport execute -INFO: HHH000227: Running hbm2ddl schema export <co - linkends="sda1HibernateSchemaLog-6" - xml:id="sda1HibernateSchemaLog-6-co"/> -Hibernate: - drop table if exists Lecture <co linkends="sda1HibernateSchemaLog-7" + drop table if exists Airline <co linkends="sda1HibernateSchemaLog-7" xml:id="sda1HibernateSchemaLog-7-co"/> -Hibernate: - create table Lecture ( <co linkends="sda1HibernateSchemaLog-8" +... + create table Airline ( <co linkends="sda1HibernateSchemaLog-8" xml:id="sda1HibernateSchemaLog-8-co"/> - id integer not null auto_increment, + id bigint not null auto_increment, + icaoCode varchar(255), name varchar(255), - nr integer not null, primary key (id) ) ENGINE=InnoDB -Jun 23, 2015 10:02:55 AM org.hibernate.tool.hbm2ddl.SchemaExport execute -INFO: HHH000230: Schema export complete -Hibernate: - insert <co linkends="sda1HibernateSchemaLog-9" +... +insert into Airline (icaoCode, name) values (?, ?) <co + linkends="sda1HibernateSchemaLog-9" xml:id="sda1HibernateSchemaLog-9-co"/> - into - Lecture - (name, nr) - values - (?, ?)</programlisting> +...</programlisting> <calloutlist> <callout arearefs="sda1HibernateSchemaLog-1-co" @@ -254,6 +210,9 @@ Hibernate: <property name="javax.persistence.jdbc.user" value="hdmuser" /> <property name="javax.persistence.jdbc.password" value="XYZ" /> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /></programlisting> + + <para>Yeah, buddy: Log files may reveal clear text + passwords!</para> </callout> <callout arearefs="sda1HibernateSchemaLog-4-co" @@ -268,16 +227,12 @@ Hibernate: <productname>Mysql</productname> backend.</para> </callout> - <callout arearefs="sda1HibernateSchemaLog-6-co" - xml:id="sda1HibernateSchemaLog-6"> - <para>Starting schema export</para> - </callout> - <callout arearefs="sda1HibernateSchemaLog-7-co" xml:id="sda1HibernateSchemaLog-7"> - <para>Drop existing table. This is due to the - <code><property name="hibernate.hbm2ddl.auto" - value="create-drop" /></code> definition in + <para>Drop table <classname>Airline</classname> if exists. + This is due to the <code><property + name="hibernate.hbm2ddl.auto" value="create-drop" + /></code> definition in <filename>persistence.xml</filename>.</para> </callout> @@ -288,43 +243,71 @@ Hibernate: <callout arearefs="sda1HibernateSchemaLog-9-co" xml:id="sda1HibernateSchemaLog-9"> - <para>Inserting data using PreparedStatements.</para> + <para>Inserting data using PreparedStatements. Notice the + two question marks (?, ?) representing <xref + linkend="glo_JDBC"/> <interfacename + xlink:href="https://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html">PreparedStatement</interfacename> + place holders.</para> </callout> </calloutlist> </listitem> <listitem> - <programlisting language="none+">mysql> show create table Lecture; - -CREATE TABLE `Lecture` ( - `id` int(11) NOT NULL AUTO_INCREMENT, + <programlisting language="none+">SHOW CREATE TABLE Airline + ... +CREATE TABLE `Airline` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `icaoCode` varchar(255) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, - `nr` int(11) NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `UK_n8ex2hjw64ebjvidwb2uwxwj9` (`nr`) -) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1</programlisting> + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1</programlisting> </listitem> <listitem> - <para>See class - <classname>de.hdm_stuttgart.mi.sda1.hiberjpa.PersistDriver</classname> - in:</para> + <para>The log output contains:</para> + + <programlisting language="none">... +select airline0_.id as id1_0_, + airline0_.icaoCode as icaoCode2_0_, + airline0_.name as name3_0_ +from Airline airline0_ +where airline0_.icaoCode=? +... +TRACE [main] sql.BasicBinder (BasicBinder.java:65) - binding parameter [1] as [VARCHAR] - [DLH] +...</programlisting> + <para>Removing the SQL table alias this may be + simplified:</para> + + <programlisting language="none">... +select id, icaoCode, name +from Airline +where airline0_.icaoCode=? +... +TRACE [main] sql.BasicBinder (BasicBinder.java:65) - binding parameter [1] as [VARCHAR] - [DLH] +...</programlisting> + + <para>This is an expected statement again using a placeholder + representing the <property>icaoCode</property> search + value.</para> + </listitem> + + <listitem> <annotation role="make"> <para role="eclipse">Sda1/Jpa/LectureOperations</para> </annotation> </listitem> <listitem> - <para>See class - <classname>de.hdm_stuttgart.mi.sda1.hiberjpa.ReadAllLecturesJpqlDriver</classname> - in above project.</para> + <annotation role="make"> + <para role="eclipse">Sda1/Jpa/LectureOperations</para> + </annotation> </listitem> <listitem> - <para>See class - <classname>de.hdm_stuttgart.mi.sda1.hiberjpa.model.Lecture</classname> - in above project.</para> + <annotation role="make"> + <para role="eclipse">Sda1/Jpa/LectureOperations</para> + </annotation> </listitem> </orderedlist> </answer> diff --git a/P/Sda1/Jpa/HibernateStandalone/.gitignore b/P/Sda1/Jpa/HibernateStandalone/.gitignore deleted file mode 100644 index 791f189b0340b3b60a44d663448b4ec0a9f8f10f..0000000000000000000000000000000000000000 --- a/P/Sda1/Jpa/HibernateStandalone/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -/target/ -/.settings/ -.classpath -.project -/A1.log diff --git a/P/Sda1/Jpa/HibernateStandalone/pom.xml b/P/Sda1/Jpa/HibernateStandalone/pom.xml deleted file mode 100644 index bced6f21af15d5c9551406f49658e0b56247bb89..0000000000000000000000000000000000000000 --- a/P/Sda1/Jpa/HibernateStandalone/pom.xml +++ /dev/null @@ -1,58 +0,0 @@ -<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>jpa-university</artifactId> - <version>0.1</version> - <packaging>jar</packaging> - - <name>Jpa-university</name> - - <url>http://www.mi.hdm-stuttgart.de/freedocs</url> - - <dependencies> - - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate-entitymanager</artifactId> - <version>5.1.0.Final</version> - </dependency> - - <dependency> - <groupId>mysql</groupId> - <artifactId>mysql-connector-java</artifactId> - <version>6.0.5</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.6</version> - </dependency> - - </dependencies> - - <build> - <plugins> - - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <version>3.5.1</version> - <configuration> - <source>1.8</source> - <target>1.8</target> - </configuration> - </plugin> - - </plugins> - </build> -</project> diff --git a/P/Sda1/Jpa/HibernateStandalone/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/PersistDriver.java b/P/Sda1/Jpa/HibernateStandalone/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/PersistDriver.java deleted file mode 100644 index 11e5874e90d1d8772bb66a04d9d564c37abdb5db..0000000000000000000000000000000000000000 --- a/P/Sda1/Jpa/HibernateStandalone/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/PersistDriver.java +++ /dev/null @@ -1,35 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.hiberjpa; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; - -import de.hdm_stuttgart.mi.sda1.hiberjpa.model.Lecture; - -/** - * A simple Jpa 2.1 example - * - */ -public class PersistDriver { - - /** - * @param args Unused - */ - public static void main(String[] args) { - final EntityManagerFactory emFactory = Persistence - .createEntityManagerFactory("jpa-recreate"); - - final EntityManager em = emFactory.createEntityManager(); - - em.getTransaction().begin(); - { - em.persist(new Lecture("Structure data and applications 1")); - } - em.getTransaction().commit(); - - em.close(); - - // See http://stackoverflow.com/questions/21645516/program-using-hibernate-does-not-terminate - emFactory.close(); - } -} diff --git a/P/Sda1/Jpa/HibernateStandalone/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/model/Lecture.java b/P/Sda1/Jpa/HibernateStandalone/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/model/Lecture.java deleted file mode 100644 index ad4cfa66cabef652762511ee566ce3fe8c6d397d..0000000000000000000000000000000000000000 --- a/P/Sda1/Jpa/HibernateStandalone/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/model/Lecture.java +++ /dev/null @@ -1,38 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.hiberjpa.model; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; - -/** - * A simple example entity - * - */ -@Entity -public class Lecture { - - @Id - @GeneratedValue (strategy=GenerationType.IDENTITY) - private Integer id; - - String name; - - protected Lecture (){} - - public Lecture (final String name){ - setName(name); - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Integer getId() { - return id; - } -} diff --git a/P/Sda1/Jpa/HibernateStandalone/src/main/resources/META-INF/persistence.xml b/P/Sda1/Jpa/HibernateStandalone/src/main/resources/META-INF/persistence.xml deleted file mode 100644 index 2a9778f5480b3df3dc4299503c3102b7347748ae..0000000000000000000000000000000000000000 --- a/P/Sda1/Jpa/HibernateStandalone/src/main/resources/META-INF/persistence.xml +++ /dev/null @@ -1,67 +0,0 @@ -<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="jpa-recreate"> - - <!-- - 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>de.mi.hdm_stuttgart.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.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.format_sql" value="true"/> - <property name="hibernate.use_sql_comments" value="true"/> - - <!-- Choose Mysql innodb backend --> - <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> - - <!-- 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> - -</persistence> diff --git a/P/Sda1/Jpa/HibernateStandalone/src/main/resources/log4j2.xml b/P/Sda1/Jpa/HibernateStandalone/src/main/resources/log4j2.xml deleted file mode 100644 index a387d6f7cfb53f316aadd84719e918013a1b5912..0000000000000000000000000000000000000000 --- a/P/Sda1/Jpa/HibernateStandalone/src/main/resources/log4j2.xml +++ /dev/null @@ -1,21 +0,0 @@ -<?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 my want to define class or package level per-logger rules --> - <Logger name="de.hdm_stuttgart.mi.sda1.hiberjpa.App" level="debug"> - <AppenderRef ref="A1"/> - </Logger> - <Root level="info"> - <AppenderRef ref="STDOUT"/> - </Root> - </Loggers> -</Configuration> \ No newline at end of file diff --git a/P/Sda1/Jpa/HibernateStandalone/src/test/java/de/hdm_stuttgart/mi/sda1/hiberjpa/AppTest.java b/P/Sda1/Jpa/HibernateStandalone/src/test/java/de/hdm_stuttgart/mi/sda1/hiberjpa/AppTest.java deleted file mode 100644 index 1eac334a04f0548c00c6c0fe4b84bf9abae4d06f..0000000000000000000000000000000000000000 --- a/P/Sda1/Jpa/HibernateStandalone/src/test/java/de/hdm_stuttgart/mi/sda1/hiberjpa/AppTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.hiberjpa; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Unit test for simple App. - */ -public class AppTest { - /** - * Dummy test method - */ - @Test - public void testApp() { - Assert.assertTrue( true ); - } -} diff --git a/P/Sda1/Jpa/LectureOperations/.gitignore b/P/Sda1/Jpa/LectureOperations/.gitignore index 791f189b0340b3b60a44d663448b4ec0a9f8f10f..a1c3ab4d08c0f9f91918f21c730272a4711885e8 100644 --- a/P/Sda1/Jpa/LectureOperations/.gitignore +++ b/P/Sda1/Jpa/LectureOperations/.gitignore @@ -2,4 +2,3 @@ /.settings/ .classpath .project -/A1.log diff --git a/P/Sda1/Jpa/LectureOperations/pom.xml b/P/Sda1/Jpa/LectureOperations/pom.xml index 4a325864e91c1cfb5c32742b530011faac35632a..aa7ad0b6f0a958e98873199ed68e0ac1810903cb 100644 --- a/P/Sda1/Jpa/LectureOperations/pom.xml +++ b/P/Sda1/Jpa/LectureOperations/pom.xml @@ -2,33 +2,31 @@ 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> - <parent> - <groupId>de.hdm-stuttgart.mi</groupId> - <artifactId>lecturenotes-pom</artifactId> - <version>1.0</version> - <relativePath>../../../pom.xml</relativePath> - </parent> - - <groupId>de.hdm-stuttgart.mi.sda1</groupId> - <artifactId>lecture-operations</artifactId> - <version>0.9</version> + <groupId>jpaintro</groupId> + <artifactId>university</artifactId> + <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> - <name>lecture-operations</name> + <name>university</name> - <url>http://www.mi.hdm-stuttgart.de/freedocs</url> + <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-entitymanager</artifactId> - <version>5.1.0.Final</version> + <artifactId>hibernate-core</artifactId> + <version>5.2.5.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> + <version>5.1.38</version> </dependency> <dependency> @@ -41,12 +39,11 @@ <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> - <version>2.6</version> + <version>2.7</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> @@ -61,20 +58,95 @@ <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> - <version>3.5.1</version> + <version>3.6.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> + <dependencies> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-jpamodelgen</artifactId> + <version>5.2.5.Final</version> + </dependency> + </dependencies> + </plugin> + + <!-- Build helper plugin to add generated sources to classpath --> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>build-helper-maven-plugin</artifactId> + <version>1.12</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>2.10.3</version> + <version>2.10.4</version> <configuration /> </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>2.4.3</version> + <configuration> + <transformers> + <transformer + implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> + <manifestEntries> + <Main-Class>jpaintro.university.CreateEntity</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/P/Sda1/Jpa/LectureOperations/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/PersistDriver.java b/P/Sda1/Jpa/LectureOperations/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/PersistDriver.java deleted file mode 100644 index 839dfe2f1ebb2b8928584eb30ddb660021d265ee..0000000000000000000000000000000000000000 --- a/P/Sda1/Jpa/LectureOperations/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/PersistDriver.java +++ /dev/null @@ -1,39 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.hiberjpa; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityTransaction; -import javax.persistence.Persistence; - -import de.hdm_stuttgart.mi.sda1.hiberjpa.model.Lecture; - - -/** - * A simple Jpa 2.1 example - * - */ -public class PersistDriver { - - /** - * @param args Unused - */ - public static void main( String[] args ) { - final EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("jpa-recreate"); - - final EntityManager em = emFactory.createEntityManager(); - - { - final EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - - em.persist(new Lecture("Structured data and applications 1", 113305)); - em.persist(new Lecture("Software development 1", 113105)); - em.persist(new Lecture("Database systems", 113204)); - - transaction.commit(); - } - - em.close(); - emFactory.close(); - } -} diff --git a/P/Sda1/Jpa/LectureOperations/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/ReadAllLecturesCriteriaDriver.java b/P/Sda1/Jpa/LectureOperations/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/ReadAllLecturesCriteriaDriver.java deleted file mode 100644 index 181dc7dc1209cfd893890fe77e150d055498cfa9..0000000000000000000000000000000000000000 --- a/P/Sda1/Jpa/LectureOperations/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/ReadAllLecturesCriteriaDriver.java +++ /dev/null @@ -1,53 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.hiberjpa; - -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityTransaction; -import javax.persistence.Persistence; -import javax.persistence.TypedQuery; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Root; - -import de.hdm_stuttgart.mi.sda1.hiberjpa.model.Lecture; - - -/** - * A simple Jpa 2.1 example - * - */ -public class ReadAllLecturesCriteriaDriver { - - /** - * @param args Unused - */ - public static void main( String[] args ) { - final EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("jpa-update"); - - final EntityManager em = emFactory.createEntityManager(); - - { - final EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - - final CriteriaBuilder cb = em.getCriteriaBuilder(); - final CriteriaQuery<Lecture> cq = cb.createQuery(Lecture.class); - final Root<Lecture> rootEntry = cq.from(Lecture.class); - final CriteriaQuery<Lecture> all = cq.select(rootEntry); - final TypedQuery<Lecture> allQuery = em.createQuery(all); - - final List<Lecture> lectures = allQuery.getResultList(); - - System.out.println(lectures.size() + " lecture(s) found:"); - - lectures.forEach(System.out::println); - - transaction.commit(); - } - - em.close(); - emFactory.close(); - } -} diff --git a/P/Sda1/Jpa/LectureOperations/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/ReadAllLecturesJpqlDriver.java b/P/Sda1/Jpa/LectureOperations/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/ReadAllLecturesJpqlDriver.java deleted file mode 100644 index 5c7c30ea89c31f3fa1bd15988afe07d4c1bb1856..0000000000000000000000000000000000000000 --- a/P/Sda1/Jpa/LectureOperations/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/ReadAllLecturesJpqlDriver.java +++ /dev/null @@ -1,46 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.hiberjpa; - -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityTransaction; -import javax.persistence.Persistence; -import javax.persistence.TypedQuery; - -import de.hdm_stuttgart.mi.sda1.hiberjpa.model.Lecture; - - -/** - * A simple Jpa 2.1 query example - * - */ -public class ReadAllLecturesJpqlDriver { - - /** - * @param args Unused - */ - public static void main( String[] args ) { - final EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("jpa-update"); - - final EntityManager em = emFactory.createEntityManager(); - - { - final EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - - final TypedQuery<Lecture> allQuery = em.createQuery("from Lecture l where l.name like 'S%'", Lecture.class); - - final List<Lecture> lectures = allQuery.getResultList(); - - System.out.println(lectures.size() + " lecture(s) found:"); - - lectures.forEach(System.out::println); - - transaction.commit(); - } - - em.close(); - emFactory.close(); - } -} diff --git a/P/Sda1/Jpa/LectureOperations/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/model/Lecture.java b/P/Sda1/Jpa/LectureOperations/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/model/Lecture.java deleted file mode 100644 index 0740774973c2d2160399cde93ddd3e6a191ce6ac..0000000000000000000000000000000000000000 --- a/P/Sda1/Jpa/LectureOperations/src/main/java/de/hdm_stuttgart/mi/sda1/hiberjpa/model/Lecture.java +++ /dev/null @@ -1,57 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.hiberjpa.model; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; - -/** - * A simple example entity - * - */ -@Entity -public class Lecture { - - @Id - @GeneratedValue - private Integer id; - - @Column(nullable=false, unique=true) - int nr; - - String name; - - protected Lecture (){} - - public Lecture (final String name, int nr){ - setName(name); - setNr(nr); - } - - public Integer getId() { - return id; - } - - public int getNr() { - return nr; - } - - public void setNr(int nr) { - this.nr = nr; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public String toString() { - return "Lecture " + getName() + ", nr=" + getNr(); - } - - -} diff --git a/P/Sda1/Jpa/LectureOperations/src/main/java/jpaintro/university/CreateLectures.java b/P/Sda1/Jpa/LectureOperations/src/main/java/jpaintro/university/CreateLectures.java new file mode 100644 index 0000000000000000000000000000000000000000..2281980d4a6b91354d5cbb892b86203ba2525095 --- /dev/null +++ b/P/Sda1/Jpa/LectureOperations/src/main/java/jpaintro/university/CreateLectures.java @@ -0,0 +1,44 @@ +package jpaintro.university; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import jpaintro.university.model.Lecture; + +public class CreateLectures { + + static final Logger log = LogManager.getLogger(CreateLectures.class); + + /** + * This refers to a corresponding element <persistence-unit name = "strategy_drop-and-create" > + * in META-INF/persistence.xml re-creating the relational database schema. + */ + static public final String PERSISTENCE_UNIT_DROP_CREATE = "strategy_drop-and-create"; + /** + * @param args Unused + */ + public static void main( String[] args ) { + + final EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_DROP_CREATE); + final EntityManager em = factory.createEntityManager(); + + log.info("Begin transaction"); + em.getTransaction().begin(); + + em.persist(new Lecture("Structured data and applications", 1111)); + em.persist(new Lecture("Software development", 2222)); + em.persist(new Lecture("Database systems", 3333)); + + em.getTransaction().commit(); + log.info("Committed transaction"); + + em.close(); + + // See http://stackoverflow.com/questions/21645516/program-using-hibernate-does-not-terminate + factory.close(); + } +} diff --git a/P/Sda1/Jpa/LectureOperations/src/main/java/jpaintro/university/SearchByFirstLetter.java b/P/Sda1/Jpa/LectureOperations/src/main/java/jpaintro/university/SearchByFirstLetter.java new file mode 100644 index 0000000000000000000000000000000000000000..4a18938cd85873521e36470e30ce16b21fbe8380 --- /dev/null +++ b/P/Sda1/Jpa/LectureOperations/src/main/java/jpaintro/university/SearchByFirstLetter.java @@ -0,0 +1,58 @@ +package jpaintro.university; + +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + + +import jpaintro.university.model.Lecture; +import jpaintro.university.model.Lecture_; + +public class SearchByFirstLetter { + static final Logger log = LogManager.getLogger(SearchByFirstLetter.class); + + /** + * This refers to a corresponding element <persistence-unit name = "strategy_none" > + * in META-INF/persistence.xml preserving the relational database schema. + */ + static public final String PERSISTENCE_UNIT_NO_CHANGE = "strategy_none"; + /** + * @param args Unused + */ + public static void main( String[] args ) { + + + final EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NO_CHANGE); + final EntityManager em = factory.createEntityManager(); + final CriteriaBuilder builder = factory.getCriteriaBuilder(); + + log.info("Read lectures starting with letter 'S%'"); + em.getTransaction().begin(); + + + final CriteriaQuery<Lecture> criteria = builder.createQuery( Lecture.class ); + final Root<Lecture> lectureRoot = criteria.from( Lecture.class ); + criteria.select( lectureRoot ); + + criteria.where(builder.like(lectureRoot.get(Lecture_.title), "S%" )); + final List<Lecture> lectures = em.createQuery( criteria ).getResultList(); + System.out.println("Retrieved " + lectures.size() + " lectures:"); + for (final Lecture lecture : lectures ) { + System.out.println(lecture); + } + em.getTransaction().commit(); + log.info("Read completed"); + + em.close(); + + factory.close(); + } +} \ No newline at end of file diff --git a/P/Sda1/Jpa/LectureOperations/src/main/java/jpaintro/university/SearchByTitleAndByNumber.java b/P/Sda1/Jpa/LectureOperations/src/main/java/jpaintro/university/SearchByTitleAndByNumber.java new file mode 100644 index 0000000000000000000000000000000000000000..f1d937211ad7843f85e78833ca52fe1190b4b359 --- /dev/null +++ b/P/Sda1/Jpa/LectureOperations/src/main/java/jpaintro/university/SearchByTitleAndByNumber.java @@ -0,0 +1,61 @@ +package jpaintro.university; + +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + + +import jpaintro.university.model.Lecture; +import jpaintro.university.model.Lecture_; + +public class SearchByTitleAndByNumber { + static final Logger log = LogManager.getLogger(SearchByTitleAndByNumber.class); + + /** + * This refers to a corresponding element <persistence-unit name = "strategy_none" > + * in META-INF/persistence.xml preserving the relational database schema. + */ + static public final String PERSISTENCE_UNIT_NO_CHANGE = "strategy_none"; + /** + * @param args Unused + */ + public static void main( String[] args ) { + + + final EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NO_CHANGE); + final EntityManager em = factory.createEntityManager(); + final CriteriaBuilder builder = factory.getCriteriaBuilder(); + + log.info("Read lectures starting with letter 'S%'"); + em.getTransaction().begin(); + + + final CriteriaQuery<Lecture> criteria = builder.createQuery( Lecture.class ); + final Root<Lecture> lectureRoot = criteria.from( Lecture.class ); + criteria.select(lectureRoot ); + + criteria.where(builder.and( + builder.like(lectureRoot.get(Lecture_.title), "S%" ), + builder.greaterThan(lectureRoot.get(Lecture_.courseNumber), 2000) + )); + final List<Lecture> lectures = em.createQuery(criteria).getResultList(); + System.out.println("Retrieved " + lectures.size() + " lectures:"); + for (final Lecture lecture : lectures ) { + System.out.println(lecture); + } + em.getTransaction().commit(); + log.info("Read completed"); + + em.close(); + + factory.close(); + } +} \ No newline at end of file diff --git a/P/Sda1/Jpa/LectureOperations/src/main/java/jpaintro/university/model/Lecture.java b/P/Sda1/Jpa/LectureOperations/src/main/java/jpaintro/university/model/Lecture.java new file mode 100644 index 0000000000000000000000000000000000000000..f71c18fd3dc1ab4a074795eedd173d217416924e --- /dev/null +++ b/P/Sda1/Jpa/LectureOperations/src/main/java/jpaintro/university/model/Lecture.java @@ -0,0 +1,33 @@ +package jpaintro.university.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Lecture { + @Id + @GeneratedValue (strategy=GenerationType.IDENTITY) + Long id; + protected Lecture(){} + public Long getId() { + return id; + } + + String title; + public String getTitle() { + return title; + } + + Integer courseNumber; + public Lecture(final String title, final Integer courseNumber) { + this.title = title; + this.courseNumber = courseNumber; + } + + @Override + public String toString() { + return id + ":" + title + ", courseNumber=" + courseNumber; + } +} diff --git a/P/Sda1/Jpa/LectureOperations/src/main/resources/META-INF/persistence.xml b/P/Sda1/Jpa/LectureOperations/src/main/resources/META-INF/persistence.xml index 7b9d657cc9273f84aa726546defefc7d89da2b68..e67cabbb4f5b974bd2f0b387f86d6b3b9c95b98f 100644 --- a/P/Sda1/Jpa/LectureOperations/src/main/resources/META-INF/persistence.xml +++ b/P/Sda1/Jpa/LectureOperations/src/main/resources/META-INF/persistence.xml @@ -1,119 +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>jpaintro.university.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.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"/> -<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"> - - <persistence-unit name="jpa-recreate"> - - <!-- - 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>de.mi.hdm_stuttgart.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.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.format_sql" value="true"/> - <property name="hibernate.use_sql_comments" value="true"/> - - <!-- Choose Mysql's innodb backend --> - <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> - - <!-- 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> - - <persistence-unit name="jpa-update"> - - <!-- - 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>de.mi.hdm_stuttgart.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.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="update"/> - - <!-- - 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.format_sql" value="true"/> - <property name="hibernate.use_sql_comments" value="true"/> - - <!-- Choose Mysql's innodb backend --> - <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> - - <!-- 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> -</persistence> \ No newline at end of file + <!-- Choose Mysql's innodb backend --> + <property name = "hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> + + <!-- 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.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 = "true" /> + <property name = "hibernate.format_sql" value="true"/> + <property name = "hibernate.use_sql_comments" value="true"/> + + <property name = "hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> + + <property name = "hibernate.archive.autodetection" value="class"/> + + </properties> + </persistence-unit> + +</persistence> diff --git a/P/Sda1/Jpa/LectureOperations/src/main/resources/log4j2.xml b/P/Sda1/Jpa/LectureOperations/src/main/resources/log4j2.xml index a387d6f7cfb53f316aadd84719e918013a1b5912..57d860d6f20406bd60767875343a443f8b81c496 100644 --- a/P/Sda1/Jpa/LectureOperations/src/main/resources/log4j2.xml +++ b/P/Sda1/Jpa/LectureOperations/src/main/resources/log4j2.xml @@ -10,11 +10,11 @@ </Appenders> <Loggers> - <!-- You my want to define class or package level per-logger rules --> - <Logger name="de.hdm_stuttgart.mi.sda1.hiberjpa.App" level="debug"> + <!-- You my want to enable detailed hibernate logging --> + <Logger name="org.hibernate" level="error"> <AppenderRef ref="A1"/> </Logger> - <Root level="info"> + <Root level="error"> <AppenderRef ref="STDOUT"/> </Root> </Loggers> diff --git a/P/Sda1/Jpa/LectureOperations/src/test/java/de/hdm_stuttgart/mi/sda1/hiberjpa/AppTest.java b/P/Sda1/Jpa/LectureOperations/src/test/java/de/hdm_stuttgart/mi/sda1/hiberjpa/AppTest.java deleted file mode 100644 index 1eac334a04f0548c00c6c0fe4b84bf9abae4d06f..0000000000000000000000000000000000000000 --- a/P/Sda1/Jpa/LectureOperations/src/test/java/de/hdm_stuttgart/mi/sda1/hiberjpa/AppTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.hdm_stuttgart.mi.sda1.hiberjpa; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Unit test for simple App. - */ -public class AppTest { - /** - * Dummy test method - */ - @Test - public void testApp() { - Assert.assertTrue( true ); - } -} diff --git a/P/Sda1/Jpa/LectureOperations/src/test/java/jpaintro/university/EntityTest.java b/P/Sda1/Jpa/LectureOperations/src/test/java/jpaintro/university/EntityTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1f34512cfde8d15ae09b6891b1486c2833968c01 --- /dev/null +++ b/P/Sda1/Jpa/LectureOperations/src/test/java/jpaintro/university/EntityTest.java @@ -0,0 +1,47 @@ +package jpaintro.university; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import org.junit.Assert; +import org.junit.Test; + +import jpaintro.university.model.Lecture; + +/** + * Dealing with insertion and reading entities. + */ +public class EntityTest { + /** + * Creating a database entity and testing successful reload by primary key. + */ + @Test + public void testApp() { + final EntityManagerFactory factory = Persistence.createEntityManagerFactory(CreateLectures.PERSISTENCE_UNIT_DROP_CREATE); + final EntityManager em = factory.createEntityManager(); + + final String lectureName = "Web development"; + + final Long id; // Will be initialized by database generated primary key value. + + em.getTransaction().begin(); // Creating and storing an entity + { + final Lecture lh = new Lecture(lectureName, 2222); + + em.persist(lh); + id = lh.getId(); + } + em.getTransaction().commit(); + + em.getTransaction().begin(); // Reloading the entity by its primary key + { + final Lecture readLh = em.find(Lecture.class, id); + Assert.assertEquals(lectureName, readLh.getTitle()); + } + em.getTransaction().commit(); + + em.close(); + factory.close(); + } +}