From 2683c413d76c7883916db9859cd01621a27d531d Mon Sep 17 00:00:00 2001 From: goik <goik@hdm-stuttgart.de> Date: Wed, 13 Feb 2013 14:40:53 +0100 Subject: [PATCH] transient fields, maven depedencies --- Doc/Makefile | 2 +- Doc/course.xml | 99 ++++++++++++++++--- Doc/items.xml | 5 + ws/eclipse/HibIntro/.project | 4 +- ws/eclipse/HibIntro/pom.xml | 11 +++ .../src/main/java/hibintro/v1/model/User.java | 1 - .../java/hibintro/v1/run/PersistUsers.java | 4 +- .../java/hibintro/v1/run/RetrieveAll.java | 6 +- .../main/java/hibintro/v1/run/SelectUser.java | 2 +- .../src/main/java/hibintro/v2/model/User.java | 65 ++++++++++++ .../hibintro/v2/run/PersistSingleUser.java | 34 +++++++ .../java/hibintro/v2/run/hibernate.cfg.xml | 17 ++++ 12 files changed, 230 insertions(+), 20 deletions(-) create mode 100644 ws/eclipse/HibIntro/src/main/java/hibintro/v2/model/User.java create mode 100644 ws/eclipse/HibIntro/src/main/java/hibintro/v2/run/PersistSingleUser.java create mode 100644 ws/eclipse/HibIntro/src/main/java/hibintro/v2/run/hibernate.cfg.xml diff --git a/Doc/Makefile b/Doc/Makefile index f2fa66746..e070ebbda 100644 --- a/Doc/Makefile +++ b/Doc/Makefile @@ -23,7 +23,7 @@ all::${BUILDNAVI}/index.html ${BUILDNAVI}/printversion.pdf jdoc: cd ../ws/eclipse;javadoc \ --classpath ${HIBDEPS}:/usr/share/java/jdom-2.0.4.jar:/usr/share/java/testng.jar:/ma/goik/workspace/GoikLectures/ws/eclipse/Jdbc/lib/commons-codec-1.5.jar -d ${BUILDNAVI}/Ref/api -linksource \ +-classpath ${HIBDEPS}:/ma/goik/workspace/GoikLectures/ws/eclipse/Jdbc/lib/commons-codec-1.5.jar -d ${BUILDNAVI}/Ref/api -linksource \ -link http://docs.oracle.com/javase/7/docs/api -link http://testng.org/javadocs -link http://docs.oracle.com/javaee/6/api \ -link http://www.jdom.org/docs/apidocs -link http://docs.jboss.org/hibernate/orm/4.1/javadocs \ `find . -name \*.java|grep -v \.metadata` diff --git a/Doc/course.xml b/Doc/course.xml index f63380034..2340cdaeb 100644 --- a/Doc/course.xml +++ b/Doc/course.xml @@ -15727,7 +15727,11 @@ public class HibernateUtil { xlink:href="http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html">annotation</link> <coref linkend="entityAnnotation"/>:</para> - <programlisting>package hibintro.v1.model; + <figure xml:id="mappingUserInstances"> + <title>Mapping <classname>hibintro.v1.model.User</classname> + instances to a database.</title> + + <programlisting>package hibintro.v1.model; ... @@ -15753,6 +15757,7 @@ public class User { this.cname = cname; } }</programlisting> + </figure> <para>With respect to <xref linkend="hibernateConfigurationFile"/> we notice our class <classname>hibintro.v1.model.User</classname> being @@ -15820,8 +15825,7 @@ import javax.persistence.Entity; <emphasis role="bold">import javax.persistence.Id;</emphasis> ... -@Entity -public class User {... +@Entity public class User {... <emphasis role="bold">@Id</emphasis> <co xml:id="primaryKeyDefinition"/> public String getUid() { return uid; @@ -15869,8 +15873,8 @@ public class RetrieveSingleUser { } }</programlisting> - <para>This retrieves the expected result among logged SQL background - statements:</para> + <para>This retrieves the expected result among logged SQL + <quote>background</quote> statements:</para> <programlisting>... INFO: HHH000232: Schema update complete @@ -15895,7 +15899,8 @@ public class PersistUsers { ... final Transaction transaction = session.beginTransaction(); - final User users[] = {new User("Fred", "Wings"), new User("Eve", "Briggs")}; + final User users[] = {new User("wings", "Fred Wings"), + new User("eve", "Eve Briggs")} ; for (final User u : users ) {session.save(u);} transaction.commit(); ...</programlisting> @@ -15910,15 +15915,87 @@ public class PersistUsers { extents SQL with respect to polymorphic queries. The current example does not use inheritance leaving us with a simple <abbrev xlink:href="http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch16.html">HQL</abbrev> - query:</para> - </section> + query <coref linkend="hqlFromUser"/> in + <classname>hibintro.v1.run.RetrieveAll</classname>:</para> + + <programlisting>final Query searchUsers = session.createQuery("<emphasis + role="bold">from User</emphasis>");<co xml:id="hqlFromUser"/> +final List<User> users = (List<User>) searchUsers.list(); + for (final User u: users) { + System.out.println("uid=" + u.getUid() + ", " + u.getCname()); + }</programlisting> + + <para>This yields the expected result:</para> + + <programlisting>uid=eve, Eve Briggs +uid=goik, Martin Goik +uid=wings, Fred Wings</programlisting> - <section xml:id="sect_hibernateValidation"> - <title>Hibernate validation</title> + <para>The careful reader may already have expected <abbrev + xlink:href="http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch16.html">HQL</abbrev> + to offer additional features. Following + <classname>hibintro.v1.run.SelectUser</classname> we may restrict + our result set by an <acronym + xlink:href="http://en.wikipedia.org/wiki/Sql">SQL</acronym> style + <code>where</code> clause:</para> + + <programlisting> final List<User> users = (List<User>) session.createQuery( + "<emphasis role="bold">from User u where u.cname like '%e%'</emphasis>").list(); + for (final User u: users) { + System.out.println("Found user '" + u.getCname() + "'"); + }</programlisting> + + <para>This time we receive a true subset of + <classname>hibintro.v1.model.User</classname> instances:</para> - <para/> + <programlisting>Found user 'Eve Briggs' +Found user 'Fred Wings'</programlisting> </section> </section> + + <section xml:id="mappingClasses"> + <title>Mapping classes</title> + + <para>We take a closer look at <xref linkend="mappingUserInstances"/> + and assume that Instances of + <classname>hibintro.v1.model.User</classname> need a <emphasis + role="bold">GUI related</emphasis> property <code>isSelected</code> + <coref linkend="propertyIsSelected"/>:</para> + + <programlisting>package hibintro.v2.model; + + +@Entity public class User { +... + boolean <emphasis role="bold">isSelected</emphasis> <co + xml:id="propertyIsSelected"/> = false; + + public boolean isSelected() { + return isSelected; + } + public void setSelected(boolean isSelected) { + this.isSelected = isSelected; + } + ... +}</programlisting> + + <para>Hibernates produces the following <abbrev + xlink:href="http://en.wikipedia.org/wiki/Data_definition_language">DDL</abbrev> + statements containing an attribute <code>selected</code> <coref + linkend="attributeSelected"/>:</para> + + <programlisting>CREATE TABLE User ( + uid VARCHAR(255) NOT NULL PRIMARY KEY, + cname VARCHAR(255), + selected <co xml:id="attributeSelected"/> BIT NOT NULL, +) </programlisting> + </section> + + <section xml:id="sect_hibernateValidation"> + <title>Hibernate validation</title> + + <para/> + </section> </chapter> </part> diff --git a/Doc/items.xml b/Doc/items.xml index a69d43893..b028bc0f8 100644 --- a/Doc/items.xml +++ b/Doc/items.xml @@ -54,6 +54,11 @@ xlink:href="http://en.wikipedia.org/wiki/Api">API</abbrev></para> </listitem> + <listitem> + <para><abbrev + xlink:href="http://en.wikipedia.org/wiki/Data_definition_language">DDL</abbrev></para> + </listitem> + <listitem> <para><acronym xlink:href="http://www.w3.org/DOM">DOM</acronym></para> </listitem> diff --git a/ws/eclipse/HibIntro/.project b/ws/eclipse/HibIntro/.project index c5e688c5a..c096ee0f7 100644 --- a/ws/eclipse/HibIntro/.project +++ b/ws/eclipse/HibIntro/.project @@ -11,12 +11,12 @@ </arguments> </buildCommand> <buildCommand> - <name>org.eclipse.m2e.core.maven2Builder</name> + <name>org.hibernate.eclipse.console.hibernateBuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> - <name>org.hibernate.eclipse.console.hibernateBuilder</name> + <name>org.eclipse.m2e.core.maven2Builder</name> <arguments> </arguments> </buildCommand> diff --git a/ws/eclipse/HibIntro/pom.xml b/ws/eclipse/HibIntro/pom.xml index 96c7ab3e2..59395221a 100644 --- a/ws/eclipse/HibIntro/pom.xml +++ b/ws/eclipse/HibIntro/pom.xml @@ -34,5 +34,16 @@ <artifactId>hibernate-core</artifactId> <version>4.1.9.Final</version> </dependency> + <dependency> + <groupId>org.jdom</groupId> + <artifactId>jdom</artifactId> + <version>2.0.2</version> + </dependency> + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <version>6.8</version> + </dependency> + </dependencies> </project> diff --git a/ws/eclipse/HibIntro/src/main/java/hibintro/v1/model/User.java b/ws/eclipse/HibIntro/src/main/java/hibintro/v1/model/User.java index 65104f0d1..3339c0852 100644 --- a/ws/eclipse/HibIntro/src/main/java/hibintro/v1/model/User.java +++ b/ws/eclipse/HibIntro/src/main/java/hibintro/v1/model/User.java @@ -51,7 +51,6 @@ public class User { * @param cname See {@link #getCname()}. */ public User(String uid, String cname) { - super(); this.uid = uid; this.cname = cname; } diff --git a/ws/eclipse/HibIntro/src/main/java/hibintro/v1/run/PersistUsers.java b/ws/eclipse/HibIntro/src/main/java/hibintro/v1/run/PersistUsers.java index c78ef43cf..accd388c2 100644 --- a/ws/eclipse/HibIntro/src/main/java/hibintro/v1/run/PersistUsers.java +++ b/ws/eclipse/HibIntro/src/main/java/hibintro/v1/run/PersistUsers.java @@ -23,8 +23,8 @@ public class PersistUsers { final Transaction transaction = session.beginTransaction(); - final User users[] = {new User("Fred", "Wings"), - new User("Eve", "Briggs")} ; + final User users[] = {new User("wings", "Fred Wings"), + new User("eve", "Eve Briggs")} ; for (final User u : users ) { session.save(u); } diff --git a/ws/eclipse/HibIntro/src/main/java/hibintro/v1/run/RetrieveAll.java b/ws/eclipse/HibIntro/src/main/java/hibintro/v1/run/RetrieveAll.java index 5bd60d2d3..6e6338aaa 100644 --- a/ws/eclipse/HibIntro/src/main/java/hibintro/v1/run/RetrieveAll.java +++ b/ws/eclipse/HibIntro/src/main/java/hibintro/v1/run/RetrieveAll.java @@ -7,6 +7,7 @@ import hibintro.v1.model.User; import java.util.List; +import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; @@ -31,9 +32,10 @@ public class RetrieveAll { final Transaction transaction = session.beginTransaction(); - final List<User> users = (List<User>) session.createQuery("from User").list(); + final Query searchUsers = session.createQuery("from User"); + final List<User> users = (List<User>) searchUsers.list(); for (final User u: users) { - System.out.println("Found user '" + u.getCname() + "'"); + System.out.println("uid=" + u.getUid() + ", " + u.getCname()); } transaction.commit(); diff --git a/ws/eclipse/HibIntro/src/main/java/hibintro/v1/run/SelectUser.java b/ws/eclipse/HibIntro/src/main/java/hibintro/v1/run/SelectUser.java index 65d189344..0be582b5a 100644 --- a/ws/eclipse/HibIntro/src/main/java/hibintro/v1/run/SelectUser.java +++ b/ws/eclipse/HibIntro/src/main/java/hibintro/v1/run/SelectUser.java @@ -28,7 +28,7 @@ public class SelectUser { final Transaction transaction = session.beginTransaction(); - final List<User> users = (List<User>) session.createQuery("from User u where u.cname like '%r%'").list(); + final List<User> users = (List<User>) session.createQuery("from User u where u.cname like '%e%'").list(); for (final User u: users) { System.out.println("Found user '" + u.getCname() + "'"); } diff --git a/ws/eclipse/HibIntro/src/main/java/hibintro/v2/model/User.java b/ws/eclipse/HibIntro/src/main/java/hibintro/v2/model/User.java new file mode 100644 index 000000000..3242f3c90 --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/hibintro/v2/model/User.java @@ -0,0 +1,65 @@ +package hibintro.v2.model; + +import javax.persistence.Entity; +import javax.persistence.Id; + +/** + * @author goik + * + * A simple class indended to be mapped to a database server + * + */ +@Entity +public class User { + + String uid; + /** + * + * @return The user's unique login name e.g. "goik" + */ + @Id + public String getUid() { + return uid; + } + /** + * @param uid See {@link #getUid()}. + */ + public void setUid(String uid) { + this.uid = uid; + } + String cname; + /** + * @return The user's common name e.g. "Martin Goik" + */ + public String getCname() { + return cname; + } + /** + * @param cname See {@link #getCname()}. + */ + public void setCname(String cname) { + this.cname = cname; + } + + boolean isSelected = false; + + public boolean isSelected() { + return isSelected; + } + public void setSelected(boolean isSelected) { + this.isSelected = isSelected; + } +/** + * Hibernate/JPA require a default constructor + */ + public User() {} + + /** + * @param uid See {@link #getUid()}. + * @param cname See {@link #getCname()}. + */ + public User(String uid, String cname) { + this.uid = uid; + this.cname = cname; + } +} \ No newline at end of file diff --git a/ws/eclipse/HibIntro/src/main/java/hibintro/v2/run/PersistSingleUser.java b/ws/eclipse/HibIntro/src/main/java/hibintro/v2/run/PersistSingleUser.java new file mode 100644 index 000000000..3562da2e2 --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/hibintro/v2/run/PersistSingleUser.java @@ -0,0 +1,34 @@ +package hibintro.v2.run; + + + +import hibintro.util.HibernateUtil; +import hibintro.v2.model.User; + +import org.hibernate.Session; +import org.hibernate.Transaction; + + + +/** + * @author goik + * + * Persisting a simple instance of {@link User} + * + */ +public class PersistSingleUser { + + /** + * @param args not used. + */ + public static void main(String[] args) { + final Session session = HibernateUtil.createSessionFactory("hibintro/v2/run/hibernate.cfg.xml").openSession(); + + final Transaction transaction = session.beginTransaction(); + + final User u = new User("goik", "Martin Goik"); + session.save(u); + + transaction.commit(); + } +} \ No newline at end of file diff --git a/ws/eclipse/HibIntro/src/main/java/hibintro/v2/run/hibernate.cfg.xml b/ws/eclipse/HibIntro/src/main/java/hibintro/v2/run/hibernate.cfg.xml new file mode 100644 index 000000000..921c9f68e --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/hibintro/v2/run/hibernate.cfg.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" + "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> +<hibernate-configuration> + <session-factory > + <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> + <property name="hibernate.connection.password">XYZ</property> + <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hdm</property> + <property name="hibernate.connection.username">hdmuser</property> + <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> + <property name="hibernate.show_sql">true</property> + <property name="hibernate.format_sql">true</property> + <property name="hibernate.hbm2ddl.auto">update</property> + + <mapping class="hibintro.v2.model.User"/> + </session-factory> +</hibernate-configuration> -- GitLab