Skip to content

Commit 8284d44

Browse files
committed
HBX-3130: Take into account the 'hibernate.cfg.xml' file in the Ant reverse engineering task
- Initial attempt, the test fails and is disabled Signed-off-by: Koen Aers <[email protected]>
1 parent bab07a6 commit 8284d44

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.apache.tools.ant.Project;
66
import org.apache.tools.ant.ProjectHelper;
77
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Disabled;
89
import org.junit.jupiter.api.Test;
910

1011
import java.io.ByteArrayOutputStream;
@@ -45,6 +46,18 @@ public void test5MinuteTutorial() throws Exception {
4546
assertTrue(personFile.exists());
4647
}
4748

49+
@Disabled
50+
@Test
51+
public void testCfgXml() throws Exception {
52+
File buildFile = new File(baseFolder, "cfgxml/build.xml");
53+
Project project = createProject(buildFile);
54+
assertNotNull(project);
55+
File personFile = new File(baseFolder, "cfgxml/generated/Person.java");
56+
assertFalse(personFile.exists());
57+
project.executeTarget("reveng");
58+
assertTrue(personFile.exists());
59+
}
60+
4861
@Test
4962
public void testClasspath() throws Exception {
5063
PrintStream savedOut = System.out;

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.apache.tools.ant.BuildException;
2525
import org.apache.tools.ant.Project;
2626
import org.apache.tools.ant.types.Path;
27+
import org.hibernate.boot.cfgxml.internal.ConfigLoader;
28+
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
2729
import org.hibernate.tool.api.metadata.MetadataDescriptor;
2830
import org.hibernate.tool.api.metadata.MetadataDescriptorFactory;
2931
import org.hibernate.tool.api.metadata.MetadataConstants;
@@ -53,7 +55,7 @@ public JDBCConfigurationTask() {
5355
setDescription("JDBC Configuration (for reverse engineering)");
5456
}
5557
protected MetadataDescriptor createMetadataDescriptor() {
56-
Properties properties = loadPropertiesFile();
58+
Properties properties = loadProperties();
5759
RevengStrategy res = createReverseEngineeringStrategy();
5860
properties.put(MetadataConstants.PREFER_BASIC_COMPOSITE_IDS, preferBasicCompositeIds);
5961
return MetadataDescriptorFactory
@@ -144,4 +146,20 @@ private RevengStrategy loadreverseEngineeringStrategy(final String className, Re
144146
throw new BuildException("Could not create or find " + className + " with one argument delegate constructor", e);
145147
}
146148
}
149+
150+
private Properties loadCfgXmlFile() {
151+
return new ConfigLoader(new BootstrapServiceRegistryBuilder().build())
152+
.loadProperties(getConfigurationFile());
153+
}
154+
155+
private Properties loadProperties() {
156+
Properties result = new Properties();
157+
if (getPropertyFile() != null) {
158+
result.putAll(loadPropertiesFile());
159+
}
160+
if (getConfigurationFile() != null) {
161+
result.putAll(loadCfgXmlFile());
162+
}
163+
return result;
164+
}
147165
}

0 commit comments

Comments
 (0)