Skip to content

Commit 522100a

Browse files
authored
HBX-3130: Take into account the 'hibernate.cfg.xml' file in the Ant reverse engineering task (#5527)
Signed-off-by: Koen Aers <[email protected]>
1 parent a5c536e commit 522100a

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

ant/docs/examples/cfgxml/build.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!--
2+
~ Copyright 2004 - 2025 Red Hat, Inc.
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" basis,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
<project name="5-minute-tutorial" default="reveng">
17+
18+
<!-- Include the 'hibernatetool' task definition from the file '../common/included.xml' -->
19+
<include file="../common/included.xml"/>
20+
21+
<target name="reveng" depends="common.clean">
22+
<!-- Generation of the artefacts in folder 'generated-sources' -->
23+
<hibernatetool destdir="generated">
24+
<!-- JDBC Configuration based on 'hibernate.cfg.xml' file -->
25+
<jdbcconfiguration configurationfile="hibernate.cfg.xml" />
26+
<!-- The Java file exporter -->
27+
<hbm2java/>
28+
</hibernatetool>
29+
</target>
30+
31+
</project>
32+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
3+
<hibernate-configuration>
4+
<session-factory>
5+
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
6+
<property name="hibernate.connection.url">jdbc:h2:tcp://localhost/./sakila</property>
7+
<property name="hibernate.connection.username">sa</property>
8+
<property name="hibernate.default_catalog">SAKILA</property>
9+
<property name="hibernate.default_schema">PUBLIC</property>
10+
</session-factory>
11+
</hibernate-configuration>

ant/src/it/java/org/hibernate/tool/ant/ExamplesTestIT.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ public void test5MinuteTutorial() throws Exception {
4545
assertTrue(personFile.exists());
4646
}
4747

48+
@Test
49+
public void testCfgXml() throws Exception {
50+
File buildFile = new File(baseFolder, "cfgxml/build.xml");
51+
File cfgXmlFile = new File(baseFolder, "cfgxml/hibernate.cfg.xml");
52+
String cfgXmlFileContents = Files.readString(cfgXmlFile.toPath())
53+
.replace("jdbc:h2:tcp://localhost/./sakila", constructJdbcConnectionString())
54+
.replace(">sa<", "><")
55+
.replace(">SAKILA<", ">TEST<");
56+
Files.writeString(cfgXmlFile.toPath(), cfgXmlFileContents);
57+
Project project = createProject(buildFile);
58+
assertNotNull(project);
59+
File personFile = new File(baseFolder, "cfgxml/generated/Person.java");
60+
assertFalse(personFile.exists());
61+
project.executeTarget("reveng");
62+
assertTrue(personFile.exists());
63+
}
64+
4865
@Test
4966
public void testClasspath() throws Exception {
5067
PrintStream savedOut = System.out;

ant/src/main/java/org/hibernate/tool/ant/JDBCConfigurationTask.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919

2020
import java.io.File;
2121
import java.lang.reflect.Constructor;
22+
import java.util.Map;
2223
import java.util.Properties;
2324

2425
import org.apache.tools.ant.BuildException;
2526
import org.apache.tools.ant.Project;
2627
import org.apache.tools.ant.types.Path;
28+
import org.hibernate.boot.cfgxml.internal.ConfigLoader;
29+
import org.hibernate.boot.cfgxml.spi.LoadedConfig;
30+
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
2731
import org.hibernate.tool.api.metadata.MetadataDescriptor;
2832
import org.hibernate.tool.api.metadata.MetadataDescriptorFactory;
2933
import org.hibernate.tool.api.metadata.MetadataConstants;
@@ -53,7 +57,7 @@ public JDBCConfigurationTask() {
5357
setDescription("JDBC Configuration (for reverse engineering)");
5458
}
5559
protected MetadataDescriptor createMetadataDescriptor() {
56-
Properties properties = loadPropertiesFile();
60+
Properties properties = loadProperties();
5761
RevengStrategy res = createReverseEngineeringStrategy();
5862
properties.put(MetadataConstants.PREFER_BASIC_COMPOSITE_IDS, preferBasicCompositeIds);
5963
return MetadataDescriptorFactory
@@ -119,8 +123,8 @@ public void setDetectManyToMany(boolean b) {
119123
public void setDetectOptimisticLock(boolean b) {
120124
detectOptimisticLock = b;
121125
}
122-
123-
private RevengStrategy loadreverseEngineeringStrategy(final String className, RevengStrategy delegate)
126+
127+
private RevengStrategy loadreverseEngineeringStrategy(final String className, RevengStrategy delegate)
124128
throws BuildException {
125129
try {
126130
Class<?> clazz = ReflectionUtil.classForName(className);
@@ -144,4 +148,21 @@ private RevengStrategy loadreverseEngineeringStrategy(final String className, Re
144148
throw new BuildException("Could not create or find " + className + " with one argument delegate constructor", e);
145149
}
146150
}
151+
152+
private Map<String, Object> loadCfgXmlFile() {
153+
return new ConfigLoader(new BootstrapServiceRegistryBuilder().build())
154+
.loadConfigXmlFile(getConfigurationFile())
155+
.getConfigurationValues();
156+
}
157+
158+
private Properties loadProperties() {
159+
Properties result = new Properties();
160+
if (getPropertyFile() != null) {
161+
result.putAll(loadPropertiesFile());
162+
}
163+
if (getConfigurationFile() != null) {
164+
result.putAll(loadCfgXmlFile());
165+
}
166+
return result;
167+
}
147168
}

0 commit comments

Comments
 (0)