From 69ce3858383b063d6cb7e6a1ae3d085fc13759c5 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Wed, 30 Mar 2016 21:19:26 +0200 Subject: [PATCH] Providing a complete persistence.xml rather than just a fragment. Providing additional Maven dependencies --- Doc/Persist/topics.xml | 130 +++++++++++++++++++++++++++++------------ 1 file changed, 92 insertions(+), 38 deletions(-) diff --git a/Doc/Persist/topics.xml b/Doc/Persist/topics.xml index 1d888d1df..9559db9b6 100644 --- a/Doc/Persist/topics.xml +++ b/Doc/Persist/topics.xml @@ -103,35 +103,6 @@ <quote>Airline</quote>.</para> </listitem> - <listitem> - <para><link - xlink:href="http://techbus.safaribooksonline.com/book/programming/java/9781617290459/part-1dot-getting-started-with-orm/kindle_split_013_html">Chapter - 2</link> of <xref linkend="bib_Bauer15"/> provides hints - starting a project. Rather than using the described more - elaborate <xref linkend="glo_JTA"/> based setup you may just - want to follow a simpler <xref linkend="glo_JDBC"/> based - approach when defining a persistence unit in your project's - <filename>src/main/resources/META-INF/persistence.xml</filename> - file:</para> - - <programlisting language="xml"><persistence... > - <persistence-unit name="AirlinePU"> - ... - <class>de.hdm_stuttgart.mi.persist.airline.model.Airline</class> - <properties> - <!-- JDBC database connection parameter (MI local MySQL installation) --> - <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> - <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hdm"/> - <property name="javax.persistence.jdbc.user" value="hdmuser"/> - <property name="javax.persistence.jdbc.password" value="XYZ"/> - ... - - </properties> - </persistence-unit> - ... -</persistence></programlisting> - </listitem> - <listitem> <para>Create an <classname>Airline</classname> <classname xlink:href="http://docs.oracle.com/javaee/7/api/javax/persistence/Entity.html">@Entity</classname> @@ -158,10 +129,93 @@ public class Airline { protected Airline(){} }</programlisting> + <para>The three imports require additional libraries to be + present. Using Maven this may be achieved by adding the + following dependency to your project's + <filename>pom.xml</filename> file:</para> + + <programlisting language="xml"><dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-entitymanager</artifactId> + <version>5.1.0.Final</version> +</dependency></programlisting> + <para>Why do we need <code>protected Airline(){}</code> ?</para> </listitem> + <listitem> + <para><link + xlink:href="http://techbus.safaribooksonline.com/book/programming/java/9781617290459/part-1dot-getting-started-with-orm/kindle_split_013_html">Chapter + 2</link> of <xref linkend="bib_Bauer15"/> provides hints + starting a project. Rather than using the described more + elaborate <xref linkend="glo_JTA"/> based setup you may just + want to follow a simpler <xref linkend="glo_JDBC"/> based + approach when defining a persistence unit in your project's + <filename>src/main/resources/META-INF/persistence.xml</filename> + file:</para> + + <programlisting language="xml"><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://www.oracle.com/webfolder/technetwork/jsc/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="AirlinePU"> + + <!-- A persistent unit has persistent (mapped) classes, you list them here. --> + <class>de.hdm_stuttgart.mi.persist.airline.model.Airline</class> + + <!-- Hibernate can scan your classpath for mapped classes and add them automatically + to your persistence unit. This setting disables that feature. --> + <exclude-unlisted-classes>true</exclude-unlisted-classes> + + <!-- 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"/> + <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"/> + + <!-- Disable Hibernate scanning completely, we also don't want any hbm.xml files + discovered and added automatically. --> + <property name="hibernate.archive.autodetection" value="none"/> + + </properties> + </persistence-unit> +</persistence></programlisting> + + <para>Our persistence unit connecting to a <xref + linkend="glo_Soft_Mysql"/> database server requires another + Maven dependency:</para> + + <programlisting language="xml"><dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <version>5.1.38</version> +</dependency></programlisting> + </listitem> + <listitem> <para>Our <methodname>main()</methodname> class will just write a single <classname>Airline</classname> instance to @@ -250,9 +304,9 @@ mysql> show create table hibernate_sequence; )</programlisting> <para>On object creation the <xref linkend="glo_JPA"/> provider - will use <code>hibernate_sequence</code> to generate - <code>id</code> values. Thus the <xref - linkend="glo_Soft_Mysql"/> <link + will use the additional <code>hibernate_sequence</code> table + for generating <code>id</code> values. Thus <xref + linkend="glo_Soft_Mysql"/>'s <link xlink:href="http://dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html">AUTO_INCREMENT</link> feature is not yet being used.</para> </answer> @@ -269,8 +323,8 @@ mysql> show create table hibernate_sequence; <qandadiv> <qandaentry> <question> - <para>Modify the current class <classname>Airline</classname> - to: like:</para> + <para>Modify the current <classname>Airline</classname> + class:</para> <orderedlist> <listitem> @@ -445,12 +499,12 @@ public class Airline { <listitem> <para>Stable approach with respect to <xref linkend="glo_Java"/> code refactoring: If e.g. the - property <property>icaoCode</property> gets + <property>icaoCode</property> property gets consistently renamed to <property>icaoCode</property>Number <xref - linkend="glo_SqlDdl"/> code will still be properly - generated. However this problem may be overcome for - our complex solution by providing stable <xref + linkend="glo_SqlDdl"/> code will still be generated + properly. However our complex solution may overcome + this problem by providing stable <xref linkend="glo_SqlDdl"/> attribute names via <code>@Column(name="icaoCode" ...)</code> thereby protecting the database from unnecessary schema -- GitLab