From 88643b70767d7b670a06ec3d8a7b1efda2eb9ef3 Mon Sep 17 00:00:00 2001
From: Martin Goik <goik@hdm-stuttgart.de>
Date: Thu, 11 Dec 2014 17:15:23 +0100
Subject: [PATCH] University JPA example

---
 Sda2/P/Jpa/University/.gitignore              |  5 +
 Sda2/P/Jpa/University/pom.xml                 | 98 +++++++++++++++++++
 .../mi/sda2/jpa/cd/domain/Lecture.java        | 68 +++++++++++++
 .../mi/sda2/jpa/cd/domain/Student.java        | 60 ++++++++++++
 .../mi/sda2/jpa/cd/domain/StudentLecture.java | 72 ++++++++++++++
 .../mi/sda2/jpa/university/Driver.java        | 69 +++++++++++++
 .../main/resources/META-INF/persistence.xml   | 22 +++++
 .../University/src/main/resources/log4j2.xml  | 21 ++++
 .../mi/sda2/jpa/university/AppTest.java       | 17 ++++
 9 files changed, 432 insertions(+)
 create mode 100644 Sda2/P/Jpa/University/.gitignore
 create mode 100644 Sda2/P/Jpa/University/pom.xml
 create mode 100644 Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/cd/domain/Lecture.java
 create mode 100644 Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/cd/domain/Student.java
 create mode 100644 Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/cd/domain/StudentLecture.java
 create mode 100644 Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/university/Driver.java
 create mode 100644 Sda2/P/Jpa/University/src/main/resources/META-INF/persistence.xml
 create mode 100644 Sda2/P/Jpa/University/src/main/resources/log4j2.xml
 create mode 100644 Sda2/P/Jpa/University/src/test/java/de/hdm_stuttgart/mi/sda2/jpa/university/AppTest.java

diff --git a/Sda2/P/Jpa/University/.gitignore b/Sda2/P/Jpa/University/.gitignore
new file mode 100644
index 000000000..60016e96d
--- /dev/null
+++ b/Sda2/P/Jpa/University/.gitignore
@@ -0,0 +1,5 @@
+/target/
+/.settings/
+.classpath
+.project
+A1.log
diff --git a/Sda2/P/Jpa/University/pom.xml b/Sda2/P/Jpa/University/pom.xml
new file mode 100644
index 000000000..6ab35d427
--- /dev/null
+++ b/Sda2/P/Jpa/University/pom.xml
@@ -0,0 +1,98 @@
+<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.sda2.jpa</groupId>
+	<artifactId>university</artifactId>
+	<version>0.9</version>
+	<packaging>jar</packaging>
+
+	<name>university</name>
+
+	<!--Fixme: Add a sensible project related domain here -->
+	<url>http://somedomain.org</url>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.11</version>
+			<scope>test</scope>
+		</dependency>
+
+		<dependency>
+			<groupId>org.apache.logging.log4j</groupId>
+			<artifactId>log4j-api</artifactId>
+			<version>2.1</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.logging.log4j</groupId>
+			<artifactId>log4j-core</artifactId>
+			<version>2.1</version>
+		</dependency>
+
+		<dependency>
+			<groupId>mysql</groupId>
+			<artifactId>mysql-connector-java</artifactId>
+			<version>5.1.33</version>
+		</dependency>
+
+		<dependency>
+			<groupId>org.eclipse.persistence</groupId>
+			<artifactId>eclipselink</artifactId>
+			<version>2.5.0</version>
+		</dependency>
+
+	</dependencies>
+
+	<build>
+		<plugins>
+
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>3.1</version>
+				<configuration>
+					<source>1.8</source>
+					<target>1.8</target>
+				</configuration>
+			</plugin>
+
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-javadoc-plugin</artifactId>
+				<version>2.10.1</version>
+				<configuration />
+			</plugin>
+
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-shade-plugin</artifactId>
+				<version>2.3</version>
+				<configuration>
+					<transformers>
+						<transformer
+							implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+							<manifestEntries>
+								<Main-Class>de.hdm_stuttgart.mi.sda2.jpa.cd.App</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/Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/cd/domain/Lecture.java b/Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/cd/domain/Lecture.java
new file mode 100644
index 000000000..e099845b6
--- /dev/null
+++ b/Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/cd/domain/Lecture.java
@@ -0,0 +1,68 @@
+package de.hdm_stuttgart.mi.sda2.jpa.cd.domain;
+
+import static javax.persistence.CascadeType.PERSIST;
+import static javax.persistence.CascadeType.REMOVE;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.Table;
+
+@Entity  @Table(name = "Lecture")
+public class Lecture {
+   @Id
+   @GeneratedValue
+   Long  id;
+   
+   @Column(nullable=false)
+   String title;
+   
+   @Column(unique=true, nullable=false)
+   int no;
+   
+   @OneToMany(mappedBy="lecture", cascade={PERSIST, REMOVE})
+   Set<StudentLecture> students = new HashSet<StudentLecture>();
+   
+   protected Lecture() {}
+   
+   public Long getId() {
+      return id;
+   }
+   
+   public Set<StudentLecture> getStudents() {
+      return students;
+   }
+
+   public void setStudents(Set<StudentLecture> students) {
+      this.students = students;
+   }
+
+   public Lecture(final String title, final int no) {
+      setTitle(title);
+      setNo(no);
+   }
+
+   public String getTitle() {
+      return title;
+   }
+
+   public void setTitle(String title) {
+      this.title = title;
+   }
+
+   public int getNo() {
+      return no;
+   }
+
+   public void setNo(int no) {
+      this.no = no;
+   }
+
+   
+   
+}
diff --git a/Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/cd/domain/Student.java b/Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/cd/domain/Student.java
new file mode 100644
index 000000000..5d9fd4bac
--- /dev/null
+++ b/Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/cd/domain/Student.java
@@ -0,0 +1,60 @@
+package de.hdm_stuttgart.mi.sda2.jpa.cd.domain;
+
+import static javax.persistence.CascadeType.PERSIST;
+import static javax.persistence.CascadeType.REMOVE;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.Table;
+
+
+@Entity @Table(name = "Student")
+public class Student {
+   @Id
+   @GeneratedValue
+   Long  id;
+   
+   @Column(nullable=false)
+   String fullName;
+   
+   @Column(unique=true, nullable=false)
+   int matriculation;
+
+   @OneToMany(mappedBy="student", cascade={PERSIST, REMOVE})
+   Set<StudentLecture> lectures = new HashSet<StudentLecture>();
+   
+   protected Student() {}
+   
+   public Set<StudentLecture> getLectures() {
+      return lectures;
+   }
+   public void setLectures(Set<StudentLecture> lectures) {
+      this.lectures = lectures;
+   }
+
+   public Student(final String fullName, int matriculation) {
+      setFullName(fullName);
+      setMatriculation(matriculation);
+   }
+   public String getFullName() {
+      return fullName;
+   }
+   public void setFullName(String fullName) {
+      this.fullName = fullName;
+   }
+   public int getMatriculation() {
+      return matriculation;
+   }
+   public void setMatriculation(int matriculation) {
+      this.matriculation = matriculation;
+   }
+   public Long getId() {
+      return id;
+   }
+}
diff --git a/Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/cd/domain/StudentLecture.java b/Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/cd/domain/StudentLecture.java
new file mode 100644
index 000000000..d84b4b523
--- /dev/null
+++ b/Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/cd/domain/StudentLecture.java
@@ -0,0 +1,72 @@
+package de.hdm_stuttgart.mi.sda2.jpa.cd.domain;
+
+import static javax.persistence.CascadeType.PERSIST;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.Table;
+import javax.persistence.UniqueConstraint;
+
+@Entity 
+@Table(name = "StudentLecture", uniqueConstraints=@UniqueConstraint(columnNames={"student", "lecture"}))
+public class StudentLecture {
+
+   @Id @GeneratedValue
+   Long id;
+   
+   @ManyToOne(cascade={PERSIST})
+   @JoinColumn(name="student", nullable=false)
+   Student student;
+   
+   @ManyToOne(cascade={PERSIST})
+   @JoinColumn(name="lecture", nullable=false)
+   Lecture lecture;
+
+   @Column(nullable=false)
+   int workload;
+   
+   protected StudentLecture() {}
+   
+   public StudentLecture(final Student student, final Lecture lecture, int workload) {
+      setStudent(student);
+      student.getLectures().add(this);
+      
+      setLecture(lecture);
+      lecture.getStudents().add(this);
+      setWorkload(workload);
+   }
+
+   public Long getId() {
+      return id;
+   }
+
+   public Student getStudent() {
+      return student;
+   }
+
+   public void setStudent(Student student) {
+      this.student = student;
+   }
+
+   public Lecture getLecture() {
+      return lecture;
+   }
+
+   public void setLecture(Lecture lecture) {
+      this.lecture = lecture;
+   }
+
+   public int getWorkload() {
+      return workload;
+   }
+
+   public void setWorkload(int workload) {
+      this.workload = workload;
+   }
+}
+
+
diff --git a/Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/university/Driver.java b/Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/university/Driver.java
new file mode 100644
index 000000000..611cc34d1
--- /dev/null
+++ b/Sda2/P/Jpa/University/src/main/java/de/hdm_stuttgart/mi/sda2/jpa/university/Driver.java
@@ -0,0 +1,69 @@
+package de.hdm_stuttgart.mi.sda2.jpa.university;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+import javax.xml.bind.JAXBException;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import de.hdm_stuttgart.mi.sda2.jpa.cd.domain.Lecture;
+import de.hdm_stuttgart.mi.sda2.jpa.cd.domain.Student;
+import de.hdm_stuttgart.mi.sda2.jpa.cd.domain.StudentLecture;
+
+/**
+ * A simple http://logging.apache.org/log4j/2.x demo, see file log4j2.xml for
+ * configuration options.
+ * 
+ */
+public class Driver {
+   private static Logger log = LogManager.getLogger(Driver.class);
+
+   /**
+    * @param args
+    *           Unused
+    * @throws JAXBException
+    */
+   public static void main(String[] args) throws JAXBException {
+
+      final EntityManagerFactory factory = Persistence
+            .createEntityManagerFactory("persistenceUnit");
+      final EntityManager manager = factory.createEntityManager();
+
+      final EntityTransaction tx = manager.getTransaction();
+
+      final Student joe = new Student("Joe Blix", 12346);
+      final Lecture db = new Lecture("Database systems", 11312);
+      final StudentLecture joeDb = new StudentLecture(joe, db, 5);
+
+      tx.begin();
+      {
+         // manager.persist(joe); // not required if cascading persist is
+         // enabled
+         // manager.persist(db);
+         manager.persist(joeDb);
+      }
+      tx.commit();
+
+      tx.begin();
+      {
+         manager.refresh(joe);
+         System.out.println("Lectures of Joe:");
+         for (StudentLecture sl : joe.getLectures()) {
+            System.out.println(sl.getLecture().getTitle());
+         }
+      }
+      tx.commit();
+
+      tx.begin();
+      {
+         System.out.println("Deleting Joe from Database");
+         manager.remove(joe);
+      }
+      tx.commit();
+
+   }
+
+}
diff --git a/Sda2/P/Jpa/University/src/main/resources/META-INF/persistence.xml b/Sda2/P/Jpa/University/src/main/resources/META-INF/persistence.xml
new file mode 100644
index 000000000..6914d0be7
--- /dev/null
+++ b/Sda2/P/Jpa/University/src/main/resources/META-INF/persistence.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
+	version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
+	<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
+		<!-- shouldn't be valid for java SE per specification, but it works for EclipseLink ... -->
+		<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" />
+			<property name="javax.persistence.jdbc.user" value="hdmuser" />
+			<property name="javax.persistence.jdbc.password" value="XYZ" />
+
+			<!-- EclipseLink should create the database schema automatically -->
+			<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
+			<property name="eclipselink.ddl-generation.output-mode" value="database" />
+			<property name="eclipselink.logging.level" value="SEVERE"/>				
+		</properties>
+
+	</persistence-unit>
+</persistence>
\ No newline at end of file
diff --git a/Sda2/P/Jpa/University/src/main/resources/log4j2.xml b/Sda2/P/Jpa/University/src/main/resources/log4j2.xml
new file mode 100644
index 000000000..3d47b7de2
--- /dev/null
+++ b/Sda2/P/Jpa/University/src/main/resources/log4j2.xml
@@ -0,0 +1,21 @@
+<?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.sda2.jpa.cd.App" level="debug">
+            <AppenderRef ref="A1"/>
+        </Logger>
+        <Root level="debug">
+            <AppenderRef ref="STDOUT"/>
+        </Root>
+    </Loggers>
+</Configuration>
\ No newline at end of file
diff --git a/Sda2/P/Jpa/University/src/test/java/de/hdm_stuttgart/mi/sda2/jpa/university/AppTest.java b/Sda2/P/Jpa/University/src/test/java/de/hdm_stuttgart/mi/sda2/jpa/university/AppTest.java
new file mode 100644
index 000000000..8e9ad2d7d
--- /dev/null
+++ b/Sda2/P/Jpa/University/src/test/java/de/hdm_stuttgart/mi/sda2/jpa/university/AppTest.java
@@ -0,0 +1,17 @@
+package de.hdm_stuttgart.mi.sda2.jpa.university;
+
+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 );
+    }
+}
-- 
GitLab