Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions maven/docs/examples/hbm2orm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.mapping.xml
19 changes: 19 additions & 0 deletions maven/docs/examples/hbm2orm/simple-default/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!--
~ Copyright 2004 - 2025 Red Hat, Inc.
~
~ 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.
-->
To run this example:
- Have [Apache Maven](https://maven.apache.org) installed
- Issue the following commands from a command-line window opened in this folder:
`mvn org.hibernate.tool:hibernate-tools-maven:${hibernate.version}:hbm2orm`
26 changes: 26 additions & 0 deletions maven/docs/examples/hbm2orm/simple-default/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2018 - 2025 Red Hat, Inc.
~
~ 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.
-->
<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>org.hibernate.tool.maven.test</groupId>
<artifactId>hbm2orm-simple-default</artifactId>
<version>0.0.1-SNAPSHOT</version>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2004 - 2025 Red Hat, Inc.
~
~ 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.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"https://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="org.bar.Foo">
<id name="id" type="long">
<generator class="assigned"/>
</id>
<property name="name" type="string"/>
</class>

</hibernate-mapping>
5 changes: 4 additions & 1 deletion maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
</sources>
</configuration>
</execution>
<!-- add the examples as test resources -->
<!-- add the examples along with the other resources of the functional tests as test resources -->
<execution>
<id>add-test-resource</id>
<phase>generate-test-resources</phase>
Expand All @@ -205,6 +205,9 @@
<exclude>**/hibernate.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/functionalTest/resources</directory>
</resource>
</resources>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.hibernate.tool.api.version.Version;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand All @@ -12,10 +13,12 @@
import org.apache.maven.cli.MavenCli;

import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Objects;

public class ExamplesTestIT {

Expand All @@ -24,7 +27,6 @@ public class ExamplesTestIT {
private static File localRepo;

private File projectFolder;
private MavenCli mavenCli;

@TempDir
private File tempFolder;
Expand Down Expand Up @@ -94,7 +96,7 @@ public void testOutputDirectory() throws Exception {
assertFalse(outputDirectory.exists());
assertFalse(personFile.exists());
runGenerateSources();
assertEquals(1, outputDirectory.list().length); // 1 file is generated in 'generated-classes'
assertEquals(1, Objects.requireNonNull(outputDirectory.list()).length); // 1 file is generated in 'generated-classes'
assertTrue(personFile.exists()); // The Person.java file should have been generated
}

Expand Down Expand Up @@ -122,6 +124,17 @@ public void testUseGenerics() throws Exception {
assertGeneratedContains("Person.java", "Set<Item>");
}

@Test
public void testHbm2OrmSimpleDefault() throws Exception {
projectFolder = new File(baseFolder, "hbm2orm/simple-default");
File ormXmlFile = new File(projectFolder, "src/main/resources/simple.mapping.xml");
assertFalse(ormXmlFile.exists());
runMavenCommand("org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm");
assertTrue(ormXmlFile.exists());
String ormXmlContents = Files.readString( ormXmlFile.toPath() );
assertTrue(ormXmlContents.contains("entity-mappings"));
}

private void prepareProject(String projectName) throws Exception {
projectFolder = new File(baseFolder, projectName);
assertTrue(projectFolder.exists());
Expand All @@ -132,7 +145,7 @@ private void prepareProject(String projectName) throws Exception {

private void createHibernatePropertiesFile(File projectFolder) throws Exception {
File projectResourcesFolder = new File(projectFolder, "src/main/resources");
projectResourcesFolder.mkdirs();
assertTrue(projectResourcesFolder.mkdirs());
File hibernatePropertiesFile = new File(projectResourcesFolder, "hibernate.properties");
assertFalse(hibernatePropertiesFile.exists());
String hibernatePropertiesFileContents =
Expand All @@ -147,8 +160,14 @@ private void createHibernatePropertiesFile(File projectFolder) throws Exception
}

private void runGenerateSources() {
runMavenCommand("generate-sources");
}

private void runMavenCommand(String command) {
new MavenCli().doMain(
new String[]{"-Dmaven.repo.local=" + localRepo.getAbsolutePath(), "generate-sources"},
new String[]{
"-Dmaven.repo.local=" + localRepo.getAbsolutePath(),
command},
projectFolder.getAbsolutePath(),
null,
null);
Expand All @@ -166,8 +185,11 @@ private void assertGeneratedDoesNotContain(String fileName, String contents) thr
assertFalse(readGeneratedContents(fileName).contains(contents));
}

private void assertNumberOfGeneratedFiles(int amount) throws Exception {
assertEquals(amount, new File(projectFolder, "target/generated-sources").list().length);
private void assertNumberOfGeneratedFiles(int amount) {
assertEquals(
amount,
Objects.requireNonNull(
new File(projectFolder, "target/generated-sources").list()).length);
}

private String readGeneratedContents(String fileName) throws Exception {
Expand All @@ -177,8 +199,14 @@ private String readGeneratedContents(String fileName) throws Exception {
}

private static File determineBaseFolder() throws Exception {
return new File(ExamplesTestIT.class.getClassLoader().getResource("5-minute-tutorial/pom.xml").toURI())
.getParentFile().getParentFile();
Class<?> thisClass = ExamplesTestIT.class;
URL classUrl = thisClass.getResource("/" + thisClass.getName().replace(".", "/") + ".class");
assert classUrl != null;
File result = new File(classUrl.toURI());
for (int i = 0; i < thisClass.getName().chars().filter(ch -> ch == '.').count() + 1; i++) {
result = result.getParentFile();
}
return result;
}

private void createDatabase() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.hibernate.tool.maven;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

import org.apache.maven.cli.MavenCli;
import org.hibernate.tool.api.version.Version;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class TransformHbmTestIT {

public static final String MVN_HOME = "maven.multiModuleProjectDirectory";
private static File localRepo;

@TempDir
private Path projectPath;

@BeforeAll
public static void beforeAll() throws Exception {
localRepo = new File(determineBaseFolder().getParentFile(), "local-repo");
}
@Test
public void testSimpleHbmTransformation() throws Exception {
System.setProperty(MVN_HOME, projectPath.toAbsolutePath().toString());
writePomFile();
copyHbmFile();
runTransformHbmToOrm();
}

private void writePomFile() throws Exception {
File pomFile = new File(projectPath.toFile(), "pom.xml");
assertFalse(pomFile.exists());
Path pomPath = projectPath.resolve("pom.xml");
Files.writeString(pomPath, simplePomContents);
assertTrue(pomFile.exists());
}

private void copyHbmFile() throws Exception {
URL originUrl = TransformHbmTestIT.class.getResource("simple.hbm.xml");
assertNotNull(originUrl);
Path originPath = Paths.get(Objects.requireNonNull(originUrl).toURI());
File destinationDir = new File(projectPath.toFile(), "src/main/resources/");
assertTrue(destinationDir.mkdirs());
File destinationFile = new File(destinationDir, "simple.hbm.xml");
assertFalse(destinationFile.exists());
Files.copy(originPath, destinationFile.toPath());
assertTrue(destinationFile.exists());
}

private void runTransformHbmToOrm() throws Exception {
File destinationDir = new File(projectPath.toFile(), "src/main/resources/");
File ormXmlFile = new File(destinationDir, "simple.mapping.xml");
assertFalse(ormXmlFile.exists());
new MavenCli().doMain(
new String[] {
"-Dmaven.repo.local=" + localRepo.getAbsolutePath(),
"org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm"
},
projectPath.toAbsolutePath().toString(),
null,
null);
// Check the existence of the transformed file
assertTrue(ormXmlFile.exists());
// Check if it's pretty printed
assertTrue(Files.readString(ormXmlFile.toPath()).contains("\n <table name=\"Foo\"/>\n"));
}

private static File determineBaseFolder() throws Exception {
Class<?> thisClass = TransformHbmTestIT.class;
URL classUrl = thisClass.getResource("/" + thisClass.getName().replace(".", "/") + ".class");
assert classUrl != null;
File result = new File(classUrl.toURI());
for (int i = 0; i < thisClass.getName().chars().filter(ch -> ch == '.').count() + 1; i++) {
result = result.getParentFile();
}
return result;
}

private static final String simplePomContents =
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.hibernate.tool.maven.test</groupId>
<artifactId>simplest</artifactId>
<version>0.1-SNAPSHOT</version>
</project>
""";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2004 - 2025 Red Hat, Inc.
~
~ 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.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"https://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="org.bar.Foo">
<id name="id" type="long">
<generator class="assigned"/>
</id>
<property name="name" type="string"/>
</class>

</hibernate-mapping>
15 changes: 13 additions & 2 deletions maven/src/main/java/org/hibernate/tool/maven/TransformHbmMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import org.hibernate.tool.api.xml.XMLPrettyPrinter;

@Mojo(
name = "hbm2orm",
Expand All @@ -58,6 +59,9 @@ public class TransformHbmMojo extends AbstractMojo {
@Parameter(defaultValue = "${project.basedir}/src/main/resources")
private File inputFolder;

@Parameter(defaultValue = "true")
private boolean format;

@Override
public void execute() {
MappingBinder mappingBinder = new MappingBinder(
Expand Down Expand Up @@ -119,12 +123,19 @@ private void marshall(Marshaller marshaller, JaxbEntityMappingsImpl mappings, Fi
getLog().info("Marshalling file: " + hbmXmlFile.getAbsolutePath() + " into " + mappingXmlFile.getAbsolutePath());
try {
marshaller.marshal( mappings, mappingXmlFile );
if (format) {
XMLPrettyPrinter.prettyPrintFile(mappingXmlFile);
}
}
catch (JAXBException e) {
throw new RuntimeException(
"Unable to marshall mapping JAXB representation to file `" + mappingXmlFile.getAbsolutePath() + "`",
e
);
e);
}
catch (IOException e) {
throw new RuntimeException(
"Unable to format XML file `" + mappingXmlFile.getAbsolutePath() + "`",
e);
}
}

Expand Down