Mappings from JPA-mapping-files are considered
authorKai Moritz <kai@juplo.de>
Sat, 19 Dec 2015 17:02:02 +0000 (18:02 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 7 Mar 2016 13:20:01 +0000 (14:20 +0100)
13 files changed:
src/it/multiple-persistence-units/pom.xml [new file with mode: 0644]
src/it/multiple-persistence-units/src/main/java/a/Employee.java [new file with mode: 0644]
src/it/multiple-persistence-units/src/main/java/b/Clerk.java [new file with mode: 0644]
src/it/multiple-persistence-units/src/main/java/b/Customer.java [new file with mode: 0644]
src/it/multiple-persistence-units/src/main/java/b/Person.java [new file with mode: 0644]
src/it/multiple-persistence-units/src/main/java/b/Sale.java [new file with mode: 0644]
src/it/multiple-persistence-units/src/main/resources/META-INF/clerk.xml [new file with mode: 0644]
src/it/multiple-persistence-units/src/main/resources/META-INF/customer.xml [new file with mode: 0644]
src/it/multiple-persistence-units/src/main/resources/META-INF/orm.xml [new file with mode: 0644]
src/it/multiple-persistence-units/src/main/resources/META-INF/persistence.xml [new file with mode: 0644]
src/it/multiple-persistence-units/src/main/resources/META-INF/person.xml [new file with mode: 0644]
src/it/multiple-persistence-units/src/main/resources/META-INF/sale.xml [new file with mode: 0644]
src/main/java/de/juplo/plugins/hibernate/AbstractSchemaMojo.java

diff --git a/src/it/multiple-persistence-units/pom.xml b/src/it/multiple-persistence-units/pom.xml
new file mode 100644 (file)
index 0000000..1ceb830
--- /dev/null
@@ -0,0 +1,77 @@
+<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.juplo</groupId>
+  <artifactId>multiple-persistence-units</artifactId>
+  <name>Hibernate 4 Dependency-Test</name>
+  <version>1.0-SNAPSHOT</version>
+
+  <properties>
+    <hibernate-maven-plugin.version>@project.version@</hibernate-maven-plugin.version>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.hibernate.javax.persistence</groupId>
+      <artifactId>hibernate-jpa-2.1-api</artifactId>
+      <version>1.0.0.Final</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>3.3</version>
+        <configuration>
+          <source>1.8</source>
+          <target>1.8</target>
+          <encoding>utf8</encoding>
+          <showWarnings>true</showWarnings>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>de.juplo</groupId>
+        <artifactId>hibernate-maven-plugin</artifactId>
+        <version>${hibernate-maven-plugin.version}</version>
+        <executions>
+          <execution>
+            <id>Create A</id>
+            <goals>
+              <goal>create</goal>
+            </goals>
+            <configuration>
+              <persistenceUnit>A</persistenceUnit>
+              <outputFile>a-create.sql</outputFile>
+            </configuration>
+          </execution>
+          <execution>
+            <id>Drop B</id>
+            <goals>
+              <goal>drop</goal>
+            </goals>
+            <configuration>
+              <persistenceUnit>B</persistenceUnit>
+              <outputFile>b-drop.sql</outputFile>
+            </configuration>
+          </execution>
+          <execution>
+            <id>Create B</id>
+            <goals>
+              <goal>create</goal>
+            </goals>
+            <configuration>
+              <persistenceUnit>B</persistenceUnit>
+              <outputFile>b-create.sql</outputFile>
+            </configuration>
+          </execution>
+        </executions>
+        <configuration>
+          <export>false</export>
+          <format>true</format>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/multiple-persistence-units/src/main/java/a/Employee.java b/src/it/multiple-persistence-units/src/main/java/a/Employee.java
new file mode 100644 (file)
index 0000000..2232a64
--- /dev/null
@@ -0,0 +1,41 @@
+package a;
+
+import javax.persistence.Column;
+import javax.persistence.Id;
+
+public class Employee {    
+    private int empId;
+    private String empName;
+    private double empSalary;
+    public Employee() {
+    }
+    public Employee(int empId, String empName, double empSalary) {
+        this.empId = empId;
+        this.empName = empName;
+        this.empSalary = empSalary;
+    }
+    @Id
+    @Column(name = "WRONG_NAME")
+    public int getEmpId() {
+        return empId;
+    }
+    public void setEmpId(int empId) {
+        this.empId = empId;
+    }
+    public String getEmpName() {
+        return empName;
+    }
+    public void setEmpName(String empName) {
+        this.empName = empName;
+    }
+    public double getEmpSalary() {
+        return empSalary;
+    }
+    public void setEmpSalary(double empSalary) {
+        this.empSalary = empSalary;
+    }
+    @Override
+    public String toString() {
+        return "Employee Id:="+empId+" Employee Name:="+empName+" Employee Salary:="+empSalary;
+    }
+}//End of Employee.java
diff --git a/src/it/multiple-persistence-units/src/main/java/b/Clerk.java b/src/it/multiple-persistence-units/src/main/java/b/Clerk.java
new file mode 100644 (file)
index 0000000..39d97e7
--- /dev/null
@@ -0,0 +1,12 @@
+package b;
+
+import java.util.Date;
+import java.util.Set;
+
+
+public class Clerk extends Person
+{
+  private Set<Sale> sales;
+  private Date hireDate;
+  private Date termDate;
+}
diff --git a/src/it/multiple-persistence-units/src/main/java/b/Customer.java b/src/it/multiple-persistence-units/src/main/java/b/Customer.java
new file mode 100644 (file)
index 0000000..8b17b5b
--- /dev/null
@@ -0,0 +1,14 @@
+package b;
+
+import java.util.Set;
+import javax.persistence.Table;
+
+
+@Table(name = "WRONG_NAME")
+public class Customer extends Person
+{
+  public enum CustomerLevel { BRONZE, SILVER, GOLD };
+  private Set<Sale> purchases;
+  private String email;
+  private CustomerLevel level = CustomerLevel.BRONZE;
+}
diff --git a/src/it/multiple-persistence-units/src/main/java/b/Person.java b/src/it/multiple-persistence-units/src/main/java/b/Person.java
new file mode 100644 (file)
index 0000000..82dde34
--- /dev/null
@@ -0,0 +1,8 @@
+package b;
+
+
+public class Person
+{
+  private int id;
+  private String name;
+}
diff --git a/src/it/multiple-persistence-units/src/main/java/b/Sale.java b/src/it/multiple-persistence-units/src/main/java/b/Sale.java
new file mode 100644 (file)
index 0000000..088f51d
--- /dev/null
@@ -0,0 +1,26 @@
+package b;
+
+import java.math.BigDecimal;
+import java.util.Date;
+import java.util.Set;
+import java.util.UUID;
+
+
+public class Sale
+{
+  private String id;
+  private BigDecimal amount;
+  private Date dateTime;
+  private Set<Clerk> salesClerks;
+  private Customer customer;
+
+  public Sale()
+  {
+    this(UUID.randomUUID().toString());
+  }
+
+  public Sale(String id)
+  {
+    this.id = id;
+  }
+}
\ No newline at end of file
diff --git a/src/it/multiple-persistence-units/src/main/resources/META-INF/clerk.xml b/src/it/multiple-persistence-units/src/main/resources/META-INF/clerk.xml
new file mode 100644 (file)
index 0000000..3f1c618
--- /dev/null
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
+
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
+    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm 
+
+        http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
+
+    version="1.0">
+
+
+
+    <!-- id and name mapped in superclass ORM -->    
+
+    <entity class="b.Clerk"
+
+        access="FIELD">
+
+        <table name="HMIG_CLERK"/>
+
+        <attributes>
+
+            <basic name="hireDate" optional="false">
+
+                <column name="HIRE_DATE"/>
+
+                <temporal>DATE</temporal>
+
+            </basic>
+
+            <basic name="termDate">
+
+                <column name="TERM_DATE"/>
+
+                <temporal>DATE</temporal>
+
+            </basic>
+
+            <many-to-many name="sales" mapped-by="salesClerks"/>
+
+        </attributes>
+
+    </entity>
+
+</entity-mappings>
diff --git a/src/it/multiple-persistence-units/src/main/resources/META-INF/customer.xml b/src/it/multiple-persistence-units/src/main/resources/META-INF/customer.xml
new file mode 100644 (file)
index 0000000..c21eabc
--- /dev/null
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
+
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
+    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm 
+
+        http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
+
+    version="1.0">
+
+    
+
+    <!-- id and name mapped in superclass ORM -->    
+
+    <entity class="b.Customer"
+
+        access="FIELD">
+
+        <table name="HMIG_CUSTOMER"/>
+
+        <attributes>
+
+            <basic name="email">
+
+                <column length="32"/>
+
+            </basic>
+
+            <basic name="level">
+
+                <column length="8"/>
+
+                <enumerated>STRING</enumerated>
+
+            </basic>
+
+            
+
+            <one-to-many name="purchases" mapped-by="customer"/>
+
+        </attributes>
+
+    </entity>
+
+</entity-mappings>
+
diff --git a/src/it/multiple-persistence-units/src/main/resources/META-INF/orm.xml b/src/it/multiple-persistence-units/src/main/resources/META-INF/orm.xml
new file mode 100644 (file)
index 0000000..fa5412b
--- /dev/null
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<entity-mappings
+    xmlns="http://java.sun.com/xml/ns/persistence/orm"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
+    version="1.0">
+  <description>My First JPA XML Application</description>
+  <package>a</package> 
+  <entity class="a.Employee" name="Employee">    
+    <table name="EMPLOYEETABLE"/>
+    <attributes>
+      <id name="empId">
+        <generated-value strategy="TABLE"/>
+      </id>
+      <basic name="empName">
+        <column name="EMP_NAME" length="100"/>
+      </basic>
+      <basic name="empSalary">
+      </basic>
+    </attributes>
+   </entity>
+</entity-mappings>
diff --git a/src/it/multiple-persistence-units/src/main/resources/META-INF/persistence.xml b/src/it/multiple-persistence-units/src/main/resources/META-INF/persistence.xml
new file mode 100644 (file)
index 0000000..bf8c923
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<persistence
+    xmlns="http://java.sun.com/xml/ns/persistence"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    version="1.0">
+
+  <!-- taken from https://dzone.com/articles/persisting-entity-classes -->
+  <persistence-unit name="A">
+    <class>a.Employee</class>
+    <exclude-unlisted-classes>true</exclude-unlisted-classes>
+    <properties>
+      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
+    </properties>
+  </persistence-unit>
+
+  <!-- taken from http://webdev.jhuep.com/~jcs/legacy-ejava-javaee/coursedocs/605-784-site/docs/content/html/hibernate-migration-orm.html -->
+  <persistence-unit name="B">
+    <mapping-file>META-INF/sale.xml</mapping-file>
+    <mapping-file>META-INF/person.xml</mapping-file>
+    <mapping-file>META-INF/clerk.xml</mapping-file>
+    <mapping-file>META-INF/customer.xml</mapping-file>
+    <exclude-unlisted-classes>true</exclude-unlisted-classes>
+    <properties>
+      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
+    </properties>
+  </persistence-unit>
+
+</persistence>
diff --git a/src/it/multiple-persistence-units/src/main/resources/META-INF/person.xml b/src/it/multiple-persistence-units/src/main/resources/META-INF/person.xml
new file mode 100644 (file)
index 0000000..79a92ed
--- /dev/null
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
+
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
+    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm 
+
+        http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
+
+    version="1.0">
+
+    
+
+    <mapped-superclass class="b.Person"
+
+        access="FIELD">
+
+        <attributes>
+
+            <id name="id">
+
+                <generated-value strategy="IDENTITY"/>
+
+            </id>
+
+            <basic name="name" optional="false">
+
+                <column length="32"/>
+
+            </basic>
+
+        </attributes>
+
+    </mapped-superclass>
+
+</entity-mappings>
diff --git a/src/it/multiple-persistence-units/src/main/resources/META-INF/sale.xml b/src/it/multiple-persistence-units/src/main/resources/META-INF/sale.xml
new file mode 100644 (file)
index 0000000..57e848e
--- /dev/null
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm 
+        http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
+    version="1.0">
+    
+    <package>b</package>
+    <access>FIELD</access>
+    
+    <entity class="Sale">
+        <table name="HMIG_SALE"/>
+        <attributes>
+            <id name="id">
+                <column length="36"/>
+            </id>
+            <basic name="amount" optional="false">
+                <column precision="7" scale="2"/>
+            </basic>
+            <basic name="dateTime" optional="false">
+                <column name="SALE_TIME"/>
+                <temporal>TIMESTAMP</temporal>
+            </basic>
+
+            <many-to-one name="customer" optional="false">
+                <join-column name="CUSTOMER_ID"/>
+            </many-to-one>
+            <many-to-many name="salesClerks">
+                <join-table name="HMIG_SALE_CLERK">
+                    <join-column name="SALE_ID"/>
+                    <inverse-join-column name="CLERK_ID"/>
+                </join-table>
+            </many-to-many>
+        </attributes>
+    </entity>
+</entity-mappings>
index 09bd83d..2295968 100644 (file)
@@ -542,6 +542,34 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
         classes = scanUrls(urls);
         for (String className : unit.getManagedClassNames())
           classes.add(className);
+        /**
+         * Add mappings from the default mapping-file
+         * <code>META-INF/orm.xml</code>, if present
+         */
+        try
+        {
+          InputStream is = classLoader.getResourceAsStream("META-INF/orm.xml");
+          if (is != null)
+          {
+            getLog().info("Adding default JPA-XML-mapping from META-INF/orm.xml");
+            tracker.track("META-INF/orm.xml", is);
+            sources.addResource("META-INF/orm.xml");
+          }
+          /**
+           * Add mappings from files, that are explicitly configured in the
+           * persistence unit
+           */
+          for (String mapping : unit.getMappingFileNames())
+          {
+            getLog().info("Adding explicitly configured mapping from " + mapping);
+            tracker.track(mapping, classLoader.getResourceAsStream(mapping));
+            sources.addResource(mapping);
+          }
+        }
+        catch (IOException e)
+        {
+          throw new MojoFailureException("Error reading XML-mappings", e);
+        }
       }
 
       /** Add the configured/collected annotated classes */
@@ -1053,7 +1081,7 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
             getLog().debug("New or modified package: " + packageName);
           else
            getLog().debug("Unchanged package: " + packageName);
-          getLog().info("Adding annotated package " + packageName);
+          getLog().info("Adding annotations from package " + packageName);
           sources.addPackage(packageName);
         }
         packages.add(packageName);