Commit 5e851c13 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Xml --> JPA

parent 6914aaba
......@@ -2,3 +2,4 @@
/.settings/
.classpath
.project
A1.log
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:simpleType name="money">
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element ref="cd" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:key name="uniqueId">
<xs:selector xpath="cd"></xs:selector>
<xs:field xpath="@id"></xs:field>
</xs:key>
</xs:element>
<xs:element name="cd">
<xs:complexType>
<xs:sequence>
<xs:element name="title"/>
<xs:element name="artist"/>
<xs:element name="country"/>
<xs:element name="company"/>
<xs:element name="price"/>
<xs:element name="year"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="artist" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="company" type="xs:string"/>
<xs:element name="price" type="money"/>
<xs:element name="year" type="xs:int"/>
</xs:sequence>
<xs:attribute name="id" type="xs:int" use="required"/>
</xs:complexType>
</xs:element>
......
xjc -p de.hdm_stuttgart.mi.sda2.jpa.cd.domain cd.xsd
tar c- de |tar -x -C ~/workspace/GoikLectures/Sda2/P/Jpa/Cd/src/main/java/
<?xml version="1.0" encoding="ISO8859-1" ?>
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Schema/cd.xsd">
<cd>
<cd id="12">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
......@@ -9,7 +9,7 @@
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<cd id="34">
<title>Hide your heart</title>
<artist>Bonnie Tylor</artist>
<country>UK</country>
......@@ -17,7 +17,7 @@
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<cd id="35">
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
......@@ -25,7 +25,7 @@
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<cd id="66">
<title>Still got the blues</title>
<artist>Gary More</artist>
<country>UK</country>
......@@ -33,7 +33,7 @@
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<cd id="90">
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
......@@ -41,7 +41,7 @@
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<cd id="103">
<title>One night only</title>
<artist>Bee Gees</artist>
<country>UK</country>
......@@ -49,7 +49,7 @@
<price>10.90</price>
<year>1998</year>
</cd>
<cd>
<cd id="113">
<title>Sylvias Mother</title>
<artist>Dr.Hook</artist>
<country>UK</country>
......@@ -57,7 +57,7 @@
<price>8.10</price>
<year>1973</year>
</cd>
<cd>
<cd id="121">
<title>Maggie May</title>
<artist>Rod Stewart</artist>
<country>UK</country>
......@@ -65,7 +65,7 @@
<price>8.50</price>
<year>1990</year>
</cd>
<cd>
<cd id="133">
<title>Romanza</title>
<artist>Andrea Bocelli</artist>
<country>EU</country>
......@@ -73,7 +73,7 @@
<price>10.80</price>
<year>1996</year>
</cd>
<cd>
<cd id="147">
<title>When a man loves a woman</title>
<artist>Percy Sledge</artist>
<country>USA</country>
......@@ -81,7 +81,7 @@
<price>8.70</price>
<year>1987</year>
</cd>
<cd>
<cd id="152">
<title>Black angel</title>
<artist>Savage Rose</artist>
<country>EU</country>
......@@ -89,7 +89,7 @@
<price>10.90</price>
<year>1995</year>
</cd>
<cd>
<cd id="163">
<title>1999 Grammy Nominees</title>
<artist>Many</artist>
<country>USA</country>
......@@ -97,7 +97,7 @@
<price>10.20</price>
<year>1999</year>
</cd>
<cd>
<cd id="203">
<title>For the good times</title>
<artist>Kenny Rogers</artist>
<country>UK</country>
......@@ -105,7 +105,7 @@
<price>8.70</price>
<year>1995</year>
</cd>
<cd>
<cd id="227">
<title>Big Willie style</title>
<artist>Will Smith</artist>
<country>USA</country>
......@@ -113,7 +113,7 @@
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<cd id="250">
<title>Tupelo Honey</title>
<artist>Van Morrison</artist>
<country>UK</country>
......@@ -121,7 +121,7 @@
<price>8.20</price>
<year>1971</year>
</cd>
<cd>
<cd id="251">
<title>Soulsville</title>
<artist>Jorn Hoel</artist>
<country>Norway</country>
......@@ -129,7 +129,7 @@
<price>7.90</price>
<year>1996</year>
</cd>
<cd>
<cd id="255">
<title>The very best of</title>
<artist>Cat Stevens</artist>
<country>UK</country>
......@@ -137,7 +137,7 @@
<price>8.90</price>
<year>1990</year>
</cd>
<cd>
<cd id="259">
<title>Stop</title>
<artist>Sam Brown</artist>
<country>UK</country>
......@@ -145,7 +145,7 @@
<price>8.90</price>
<year>1988</year>
</cd>
<cd>
<cd id="260">
<title>Bridge of Spies</title>
<artist>T`Pau</artist>
<country>UK</country>
......@@ -153,7 +153,7 @@
<price>7.90</price>
<year>1987</year>
</cd>
<cd>
<cd id="263">
<title>Private Dancer</title>
<artist>Tina Turner</artist>
<country>UK</country>
......@@ -161,7 +161,7 @@
<price>8.90</price>
<year>1983</year>
</cd>
<cd>
<cd id="299">
<title>Midt om natten</title>
<artist>Kim Larsen</artist>
<country>EU</country>
......@@ -169,7 +169,7 @@
<price>7.80</price>
<year>1983</year>
</cd>
<cd>
<cd id="301">
<title>Pavarotti Gala Concert</title>
<artist>Luciano Pavarotti</artist>
<country>UK</country>
......@@ -177,7 +177,7 @@
<price>9.90</price>
<year>1991</year>
</cd>
<cd>
<cd id="312">
<title>The dock of the bay</title>
<artist>Otis Redding</artist>
<country>USA</country>
......@@ -185,7 +185,7 @@
<price>7.90</price>
<year>1987</year>
</cd>
<cd>
<cd id="322">
<title>Picture book</title>
<artist>Simply Red</artist>
<country>EU</country>
......@@ -193,7 +193,7 @@
<price>7.20</price>
<year>1985</year>
</cd>
<cd>
<cd id="338">
<title>Red</title>
<artist>The Communards</artist>
<country>UK</country>
......@@ -201,7 +201,7 @@
<price>7.80</price>
<year>1987</year>
</cd>
<cd>
<cd id="421">
<title>Unchain my heart</title>
<artist>Joe Cocker</artist>
<country>USA</country>
......
<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>Cd</artifactId>
<version>0.9</version>
<packaging>jar</packaging>
<name>Cd</name>
<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>
<!--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>
<groupId>de.hdm-stuttgart.mi.sda2.jpa</groupId>
<artifactId>Cd</artifactId>
<version>0.9</version>
<packaging>jar</packaging>
<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>
<name>Cd</name>
</dependencies>
<build>
<plugins>
<!--Fixme: Add a sensible project related domain here -->
<url>http://somedomain.org</url>
<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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.1</version>
<configuration/>
</plugin>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<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>
<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>
<!-- <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>javax.persistence</artifactId>
<version>2.0.3</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>
package de.hdm_stuttgart.mi.sda2.jpa.cd;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* A simple http://logging.apache.org/log4j/2.x demo,
* see file log4j2.xml for configuration options.
*
*/
public class App {
private static Logger log = LogManager.getLogger(App.class);
/**
* @param args Unused
*/
public static void main( String[] args ) {
System.out.println( "Hello World!" );
log.debug("With respect to logging you may want to configure file ");
log.debug("'src/main/resources/log4j2.xml' to suit your needs.");
log.debug("This config file 'log4j2.xml' will result in 'A1.log'");
log.debug("file containing logging output as well.");
}
}
package de.hdm_stuttgart.mi.sda2.jpa.cd;
import java.io.File;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.hdm_stuttgart.mi.sda2.jpa.cd.domain.Catalog;
import de.hdm_stuttgart.mi.sda2.jpa.cd.domain.Cd;
/**
* 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 JAXBContext cdContext = JAXBContext.newInstance("de.hdm_stuttgart.mi.sda2.jpa.cd.domain");
final Unmarshaller u = cdContext.createUnmarshaller();
final Catalog catalog = (Catalog) u.unmarshal(new File("cdData.xml"));
for (final Cd cd : catalog.getCd()) {
log.debug("CD:" + cd.getArtist());
}
toDatabase(catalog);
}
private static void toDatabase(final Catalog catalog) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
EntityManager manager = factory.createEntityManager();
EntityTransaction tx = manager.getTransaction();
tx.begin();
try {
for (final Cd cd : catalog.getCd()) {
manager.persist(cd);
}
} catch (Exception e) {
e.printStackTrace();
}
tx.commit();
}
}
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2014.11.07 at 01:02:00 AM CET
//
package de.hdm_stuttgart.mi.sda2.jpa.cd.domain;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType>
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element ref="{}cd" maxOccurs="unbounded" minOccurs="0"/>
* &lt;/sequence>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"cd"
})
@XmlRootElement(name = "catalog")
public class Catalog {
protected List<Cd> cd;
/**
* Gets the value of the cd property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the cd property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getCd().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link Cd }
*
*
*/
public List<Cd> getCd() {
if (cd == null) {
cd = new ArrayList<Cd>();
}
return this.cd;
}
}
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2014.11.07 at 01:02:00 AM CET
//
package de.hdm_stuttgart.mi.sda2.jpa.cd.domain;
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType>
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="title" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;element name="artist" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;element name="country" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;element name="company" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;element name="price" type="{}money"/>
* &lt;element name="year" type="{http://www.w3.org/2001/XMLSchema}int"/>
* &lt;/sequence>
* &lt;attribute name="id" use="required" type="{http://www.w3.org/2001/XMLSchema}int" />
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* </pre>
*
*
*/
@Entity
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"title",
"artist",
"country",
"company",
"price",
"year"
})
@XmlRootElement(name = "cd")
public class Cd {
@Id
@GeneratedValue
@XmlAttribute(name = "id", required = true)
protected int id;
@XmlElement(required = true)
protected String title;
@XmlElement(required = true)
protected String artist;
@XmlElement(required = true)
protected String country;
@XmlElement(required = true)
protected String company;
@XmlElement(required = true)
protected BigDecimal price;
protected int year;
/**
* Gets the value of the title property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getTitle() {
return title;
}
/**
* Sets the value of the title property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setTitle(String value) {
this.title = value;
}
/**
* Gets the value of the artist property.
*
* @return
* possible object is
* {@link String }
*