From: Kai Moritz Date: Sun, 6 Nov 2016 15:15:51 +0000 (+0100) Subject: Added the goal "update" X-Git-Tag: hibernate-maven-plugin-2.1.0~15 X-Git-Url: http://juplo.de/gitweb/?p=hibernate4-maven-plugin;a=commitdiff_plain;h=9ef60a3f37e89f8a2417e340ef6c6b4a11e6013e Added the goal "update" 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 --- diff --git a/src/it/update/db/test.mv.db b/src/it/update/db/test.mv.db new file mode 100644 index 00000000..8a0b2ab5 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 index 00000000..b9537377 --- /dev/null +++ b/src/it/update/pom.xml @@ -0,0 +1,95 @@ + + + + + 4.0.0 + + de.juplo + update-test + Hibernate Annotations Tutorial -- Modified to proove the new update-goal + 0.0.0.0.1b-SNAPSHOT + + + + true + @project.version@ + + + + + + org.hibernate + hibernate-core + 5.2.4.Final + + + + com.h2database + h2 + 1.4.187 + + + + + + + src/test/resources + true + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + utf8 + true + + + + de.juplo + hibernate-maven-plugin + ${plugin.version} + + + + update + + + + + true + true + true + false + + + + + + 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 index 00000000..7e0f936d --- /dev/null +++ b/src/it/update/src/main/java/org/hibernate/tutorial/annotations/Event.java @@ -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 index 00000000..7475f68c --- /dev/null +++ b/src/it/update/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,56 @@ + + + + + + + + + + org.h2.Driver + jdbc:h2:${project.build.directory}/../db/test;MVCC=TRUE + sa + + + + 1 + + + org.hibernate.dialect.H2Dialect + + + org.hibernate.cache.internal.NoCacheProvider + + + true + + + + + + + diff --git a/src/it/update/update.sql b/src/it/update/update.sql new file mode 100644 index 00000000..0c9457ca --- /dev/null +++ b/src/it/update/update.sql @@ -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 index 00000000..d517bc1b --- /dev/null +++ b/src/it/update/verify.bsh @@ -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 index 00000000..91d978cf --- /dev/null +++ b/src/main/java/de/juplo/plugins/hibernate/UpdateMojo.java @@ -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. + *

+ * If the specified filename is not absolut, the file will be created + * relative to the project build directory + * (project.build.directory). + * + * @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); + } +}