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
19 changes: 19 additions & 0 deletions maven/docs/examples/hbm2orm/with-user-type/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 compile org.hibernate.tool:hibernate-tools-maven:${hibernate.version}:hbm2orm`
26 changes: 26 additions & 0 deletions maven/docs/examples/hbm2orm/with-user-type/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,44 @@
/*
* Hibernate Tools, Tooling for your Hibernate Projects
*
* Copyright 2016-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.
*/
package org.foo;

import org.hibernate.usertype.UserType;

public class Bar implements UserType<Integer> {

@Override
public int getSqlType() {
return 0;
}

@Override
public Class<Integer> returnedClass() {
return null;
}

@Override
public Integer deepCopy(Integer integer) {
return 0;
}

@Override
public boolean isMutable() {
return false;
}

}
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="org.foo.Bar"/>
</class>

</hibernate-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ private void runTransformHbmToOrm() throws Exception {
assertFalse(ormXmlFile.exists());
new MavenCli().doMain(
new String[] {
"-Dmaven.repo.local=" + localRepo.getAbsolutePath(),
"org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm"
"-Dmaven.repo.local=" + localRepo.getAbsolutePath(),
"compile",
"org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm"
},
projectPath.toAbsolutePath().toString(),
null,
Expand Down
40 changes: 34 additions & 6 deletions maven/src/main/java/org/hibernate/tool/maven/TransformHbmMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serial;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.jaxb.Origin;
import org.hibernate.boot.jaxb.SourceType;
Expand Down Expand Up @@ -62,14 +67,25 @@ public class TransformHbmMojo extends AbstractMojo {
@Parameter(defaultValue = "true")
private boolean format;

@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;

@Override
public void execute() {
MappingBinder mappingBinder = new MappingBinder(
MappingBinder.class.getClassLoader()::getResourceAsStream,
UnsupportedFeatureHandling.ERROR);
List<File> hbmFiles = getHbmFiles(inputFolder);
List<Binding<JaxbHbmHibernateMapping>> hbmMappings = getHbmMappings(hbmFiles, mappingBinder);
performTransformation(hbmMappings, mappingBinder, createServiceRegistry());
ClassLoader original = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(createClassLoader(original));
getLog().info("Starting " + this.getClass().getSimpleName() + "...");
MappingBinder mappingBinder = new MappingBinder(
MappingBinder.class.getClassLoader()::getResourceAsStream,
UnsupportedFeatureHandling.ERROR);
List<File> hbmFiles = getHbmFiles(inputFolder);
List<Binding<JaxbHbmHibernateMapping>> hbmMappings = getHbmMappings(hbmFiles, mappingBinder);
performTransformation(hbmMappings, mappingBinder, createServiceRegistry());
getLog().info("Finished " + this.getClass().getSimpleName() + "!");
} finally {
Thread.currentThread().setContextClassLoader(original);
}
}

private ServiceRegistry createServiceRegistry() {
Expand Down Expand Up @@ -174,6 +190,18 @@ private List<File> getHbmFiles(File f) {
return result;
}

private ClassLoader createClassLoader(ClassLoader parent) {
ArrayList<URL> urls = new ArrayList<>();
try {
for (String cpe : project.getRuntimeClasspathElements()) {
urls.add(new File(cpe).toURI().toURL());
}
} catch (DependencyResolutionRequiredException | MalformedURLException e) {
throw new RuntimeException("Problem while constructing project classloader", e);
}
return new URLClassLoader(urls.toArray(new URL[0]), parent);
}

private static class HbmXmlOrigin extends Origin {

@Serial
Expand Down