Added the goal "update"
authorKai Moritz <kai@juplo.de>
Sun, 6 Nov 2016 15:15:51 +0000 (16:15 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 13 Nov 2016 16:37:15 +0000 (17:37 +0100)
This new goal is not as useful, as it looks, because the quality of the
generated update-script heavily depends on the support for the JDBC metadata
API of the used driver.

As the added integration-test shows, this support is rather bad for the
H2-database, that is used in the test: a change to the length of one
column is ignored. Only the rename is reflected.

The same seems to hold for MySQL, as can be read here:
http://stackoverflow.com/a/12405147/247276

src/it/update/db/test.mv.db [new file with mode: 0644]
src/it/update/pom.xml [new file with mode: 0644]
src/it/update/src/main/java/org/hibernate/tutorial/annotations/Event.java [new file with mode: 0644]
src/it/update/src/main/resources/hibernate.cfg.xml [new file with mode: 0644]
src/it/update/update.sql [new file with mode: 0644]
src/it/update/verify.bsh [new file with mode: 0644]
src/main/java/de/juplo/plugins/hibernate/UpdateMojo.java [new file with mode: 0644]

diff --git a/src/it/update/db/test.mv.db b/src/it/update/db/test.mv.db
new file mode 100644 (file)
index 0000000..8a0b2ab
Binary files /dev/null and b/src/it/update/db/test.mv.db differ
diff --git a/src/it/update/pom.xml b/src/it/update/pom.xml
new file mode 100644 (file)
index 0000000..b953737
--- /dev/null
@@ -0,0 +1,95 @@
+<?xml version="1.0"?>
+<!--
+  ~ Hibernate, Relational Persistence for Idiomatic Java
+  ~
+  ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+  ~ indicated by the @author tags or express copyright attribution
+  ~ statements applied by the authors.  All third-party contributions are
+  ~ distributed under license by Red Hat Inc.
+  ~
+  ~ This copyrighted material is made available to anyone wishing to use, modify,
+  ~ copy, or redistribute it subject to the terms and conditions of the GNU
+  ~ Lesser General Public License, as published by the Free Software Foundation.
+  ~
+  ~ This program is distributed in the hope that it will be useful,
+  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+  ~ or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+  ~ for more details.
+  ~
+  ~ You should have received a copy of the GNU Lesser General Public License
+  ~ along with this distribution; if not, write to:
+  ~ Free Software Foundation, Inc.
+  ~ 51 Franklin Street, Fifth Floor
+  ~ Boston, MA  02110-1301  USA
+  -->
+<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>update-test</artifactId>
+    <name>Hibernate Annotations Tutorial -- Modified to proove the new update-goal</name>
+    <version>0.0.0.0.1b-SNAPSHOT</version>
+
+    <properties>
+        <!-- Skip artifact deployment -->
+        <maven.deploy.skip>true</maven.deploy.skip>
+        <plugin.version>@project.version@</plugin.version>
+    </properties>
+
+
+    <dependencies>
+        <dependency>
+            <groupId>org.hibernate</groupId>
+            <artifactId>hibernate-core</artifactId>
+            <version>5.2.4.Final</version>
+        </dependency>
+        <!-- The tutorials use the H2 in-memory database -->
+        <dependency>
+            <groupId>com.h2database</groupId>
+            <artifactId>h2</artifactId>
+            <version>1.4.187</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <testResources>
+            <testResource>
+                <directory>src/test/resources</directory>
+                <filtering>true</filtering>
+            </testResource>
+        </testResources>
+        <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>${plugin.version}</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>update</goal>
+                        </goals>
+                    </execution>
+                </executions>
+                <configuration>
+                    <scanTestClasses>true</scanTestClasses>
+                    <format>true</format>
+                    <show>true</show>
+                    <execute>false</execute>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
diff --git a/src/it/update/src/main/java/org/hibernate/tutorial/annotations/Event.java b/src/it/update/src/main/java/org/hibernate/tutorial/annotations/Event.java
new file mode 100644 (file)
index 0000000..7e0f936
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.tutorial.annotations;
+
+import java.util.Date;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+
+import org.hibernate.annotations.GenericGenerator;
+
+@Entity
+@Table( name = "EVENTS" )
+public class Event {
+    private Long id;
+
+    private String title;
+    private Date date;
+
+       public Event() {
+               // this form used by Hibernate
+       }
+
+       public Event(String title, Date date) {
+               // for application use, to create new events
+               this.title = title;
+               this.date = date;
+       }
+
+       @Id
+       @GeneratedValue(generator="increment")
+       @GenericGenerator(name="increment", strategy = "increment")
+    public Long getId() {
+               return id;
+    }
+
+    private void setId(Long id) {
+               this.id = id;
+    }
+
+       @Temporal(TemporalType.TIMESTAMP)
+       @Column(name = "MY_NEW_EVENT_DATE_COLUMN")
+    public Date getDate() {
+               return date;
+    }
+
+    public void setDate(Date date) {
+               this.date = date;
+    }
+
+       @Column(length=1023) // Ignored, due to lack of metadata-support in h2 (See: http://stackoverflow.com/a/12405147/247276 )
+    public String getTitle() {
+               return title;
+    }
+
+    public void setTitle(String title) {
+               this.title = title;
+    }
+}
diff --git a/src/it/update/src/main/resources/hibernate.cfg.xml b/src/it/update/src/main/resources/hibernate.cfg.xml
new file mode 100644 (file)
index 0000000..7475f68
--- /dev/null
@@ -0,0 +1,56 @@
+<?xml version='1.0' encoding='utf-8'?>
+<!--
+  ~ Hibernate, Relational Persistence for Idiomatic Java
+  ~
+  ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+  ~ indicated by the @author tags or express copyright attribution
+  ~ statements applied by the authors.  All third-party contributions are
+  ~ distributed under license by Red Hat Inc.
+  ~
+  ~ This copyrighted material is made available to anyone wishing to use, modify,
+  ~ copy, or redistribute it subject to the terms and conditions of the GNU
+  ~ Lesser General Public License, as published by the Free Software Foundation.
+  ~
+  ~ This program is distributed in the hope that it will be useful,
+  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+  ~ or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+  ~ for more details.
+  ~
+  ~ You should have received a copy of the GNU Lesser General Public License
+  ~ along with this distribution; if not, write to:
+  ~ Free Software Foundation, Inc.
+  ~ 51 Franklin Street, Fifth Floor
+  ~ Boston, MA  02110-1301  USA
+  -->
+<!DOCTYPE hibernate-configuration PUBLIC
+        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
+        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
+
+<hibernate-configuration>
+
+    <session-factory>
+
+        <!-- Database connection settings -->
+        <property name="connection.driver_class">org.h2.Driver</property>
+        <property name="connection.url">jdbc:h2:${project.build.directory}/../db/test;MVCC=TRUE</property>
+        <property name="connection.username">sa</property>
+        <property name="connection.password"></property>
+
+        <!-- JDBC connection pool (use the built-in) -->
+        <property name="connection.pool_size">1</property>
+
+        <!-- SQL dialect -->
+        <property name="dialect">org.hibernate.dialect.H2Dialect</property>
+
+        <!-- Disable the second-level cache  -->
+        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
+
+        <!-- Echo all executed SQL to stdout -->
+        <property name="show_sql">true</property>
+
+        <!-- Names the annotated entity class -->
+        <mapping class="org.hibernate.tutorial.annotations.Event"/>
+
+    </session-factory>
+
+</hibernate-configuration>
diff --git a/src/it/update/update.sql b/src/it/update/update.sql
new file mode 100644 (file)
index 0000000..0c9457c
--- /dev/null
@@ -0,0 +1,3 @@
+
+    alter table EVENTS 
+        add column MY_NEW_EVENT_DATE_COLUMN timestamp;
diff --git a/src/it/update/verify.bsh b/src/it/update/verify.bsh
new file mode 100644 (file)
index 0000000..d517bc1
--- /dev/null
@@ -0,0 +1,7 @@
+import de.juplo.test.FileComparator;
+
+
+FileComparator comparator = new FileComparator(basedir);
+
+if (!comparator.isEqual("update.sql","target/update.sql"))
+  return false;
diff --git a/src/main/java/de/juplo/plugins/hibernate/UpdateMojo.java b/src/main/java/de/juplo/plugins/hibernate/UpdateMojo.java
new file mode 100644 (file)
index 0000000..91d978c
--- /dev/null
@@ -0,0 +1,81 @@
+package de.juplo.plugins.hibernate;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.util.Map;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.hibernate.boot.spi.MetadataImplementor;
+import org.hibernate.service.ServiceRegistry;
+import org.hibernate.tool.schema.spi.ExecutionOptions;
+import org.hibernate.tool.schema.spi.SchemaManagementTool;
+import org.hibernate.tool.schema.spi.TargetDescriptor;
+
+
+/**
+ * Goal which extracts the hibernate-mapping-configuration and
+ * exports an according SQL-database-schema.
+ *
+ * @goal update
+ * @phase process-classes
+ * @threadSafe
+ * @requiresDependencyResolution runtime
+ */
+public class UpdateMojo extends AbstractSchemaMojo
+{
+  /**
+   * Output file.
+   * <p>
+   * If the specified filename is not absolut, the file will be created
+   * relative to the project build directory
+   * (<code>project.build.directory</code>).
+   *
+   * @parameter property="hibernate.schema.update" default-value="update.sql"
+   * @since 1.0
+   */
+  private String outputFile;
+
+
+  @Override
+  public final void execute()
+    throws
+      MojoFailureException,
+      MojoExecutionException
+  {
+    super.execute(outputFile);
+  }
+
+
+  @Override
+  void build(
+      MetadataImplementor metadata,
+      ExecutionOptions options,
+      TargetDescriptor target
+      )
+      throws
+        MojoExecutionException,
+        MojoFailureException
+  {
+    ServiceRegistry service =
+        metadata.getMetadataBuildingOptions().getServiceRegistry();
+    SchemaManagementTool tool = service.getService(SchemaManagementTool.class);
+
+    Map config = options.getConfigurationValues();
+
+    tool.getSchemaMigrator(config).doMigration(metadata, options, target);
+  }
+}