Skip to content
Snippets Groups Projects
Commit 87fad754 authored by Goik Martin's avatar Goik Martin
Browse files

Demo StudyCourse Project

parent d692dde0
No related branches found
No related tags found
No related merge requests found
/target/
/.settings/
.classpath
.project
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries
# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
# Gradle:
.idea/**/gradle.xml
.idea/**/libraries
# CMake
cmake-build-debug/
# Mongo Explorer plugin:
.idea/**/mongoSettings.xml
## File-based project format:
*.iws
## Plugin-specific files:
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
\ No newline at end of file
Hibernate JPA 2 project template
================================
Usage:
------
A basic JPA CRUD example
General:
--------
This project contains:
* `src/main/resources/META-INF/persistence.xml`
Two persistence units offering a »drop-and-create«
(re-creating schema) and a »strategy_none« (don't touch)JDBC database
connection to a Mysql 5 server. You may want to adjust both database
type and connection parameters.
* Important classes (package root `de.hdm_stuttgart.mi.sda1` omitted
for brevity):
|File / Class |Description |
|--------------------------|-------------------------------------------|
|`...model.StudyCourse` |Class representing study courses |
|`...CreateStudyCourse` |Persisting a study course |
|`...ReadStudyCourseById` |Retrieving a study course database instance by primary key |
* `src/main/resources/log4j2.xml` Configuring the log4j subsystem.
On executing i.e. »`mvn install`« Maven will
[generate]
(https://docs.jboss.org/hibernate/orm/current/topical/html_single/metamodelgen/MetamodelGenerator.html)
metamodel classes like `StudyCourse_.java` during the
»`generate-sources`« phase. These classes are being required for
[typesafe criteria based queries]
(http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/querycriteria.html#querycriteria-typedquery)
not being used in this project.
Targets:
--------
* `mvn test`
Creating and re-reading a database instance of `StudyCourse`.
Connection parameters being defined twice in two persistence units in
`src/main/resources/META-INF/persistence.xml`.
* `mvn package`
Creating an executable jar persisting an `StudyCourse` instance to be
run like e.g.:
java -jar target/intro-1.0.jar
\ No newline at end of file
<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>studycourse_basic</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>studycourse_basic</name>
<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-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</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.10.0</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>
<version>2.9.3</version>
</dependency>
<!--
https://stackoverflow.com/questions/27412287/junit-tests-fail-with-java-error-when-using-intellij-within-maven-module-after-a#answer-34374834
-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.2.12.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.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>
</plugin>
<!-- Build helper plugin to add generated sources to classpath -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</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>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>de.hdm_stuttgart.mi.sda1.CreateStudyCourse</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.sda1;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.hdm_stuttgart.mi.sda1.model.StudyCourse;
/**
* Create a transient {@link StudyCourse} instance and persist it to the database.
*/
public class CreateStudyCourse {
static private final Logger log = LogManager.getLogger(CreateStudyCourse.class);
/**
* @param args Unused
*/
public static void main( String[] args ) {
final EntityManagerFactory factory = Persistence.createEntityManagerFactory(
"strategy_drop-and-create");
final EntityManager em = factory.createEntityManager();
log.info("Begin transaction");
final EntityTransaction tx = em.getTransaction();
tx.begin();
final StudyCourse csm = new StudyCourse(
"CSM", "Computer Science an Media");
em.persist(csm);
tx.commit();
log.info("Committed transaction");
// See http://stackoverflow.com/questions/21645516/program-using-hibernate-does-not-terminate
factory.close();
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sda1;
import de.hdm_stuttgart.mi.sda1.model.StudyCourse;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
/**
* Read a {@link StudyCourse} by its primary key value.
*/
public class ReadStudyCourseById {
/**
* @param args unused
*/
public static void main( String[] args ) {
final EntityManagerFactory factory = Persistence.createEntityManagerFactory(
"strategy_none");
final EntityManager em = factory.createEntityManager();
final EntityTransaction tx = em.getTransaction();
final StudyCourse studyCourse;
tx.begin();
studyCourse = em.find(StudyCourse.class, "CSM");
tx.commit();
System.out.println("Read + '" + studyCourse + "'");
// See http://stackoverflow.com/questions/21645516/program-using-hibernate-does-not-terminate
factory.close();
}
}
\ No newline at end of file
<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>sda1.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.cj.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"/>
<!-- Choose Mysql. Set »innodb« backend by system property -->
<property name = "hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect" />
<!-- 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.cj.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 = "false" />
<property name = "hibernate.format_sql" value="true"/>
<property name = "hibernate.use_sql_comments" value="true"/>
<property name = "hibernate.dialect.storage_engine" value="innodb" />
<property name = "hibernate.archive.autodetection" value="class"/>
</properties>
</persistence-unit>
</persistence>
<?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 may want to enable more detailed hibernate logging -->
<Logger name="org.hibernate" level="info">
<AppenderRef ref="A1"/>
</Logger>
<Root level="info">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment