Skip to content

Commit 2b79417

Browse files
HBX-2840 Prevent Hibernate ORM from trying to load component classes
Co-authored-by: marko-bekhta <[email protected]> Co-authored-by: Koen Aers <[email protected]> Signed-off-by: Koen Aers <[email protected]>
1 parent fcddd8e commit 2b79417

File tree

6 files changed

+102
-1
lines changed

6 files changed

+102
-1
lines changed

orm/src/main/java/org/hibernate/tool/internal/reveng/binder/PrimaryKeyBinder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.hibernate.tool.api.reveng.TableIdentifier;
2525
import org.hibernate.tool.internal.reveng.RevengMetadataCollector;
2626
import org.hibernate.tool.internal.reveng.binder.ForeignKeyUtils.ForeignKeyForColumns;
27+
import org.hibernate.tool.internal.reveng.util.EnhancedComponent;
2728
import org.hibernate.tool.internal.reveng.util.RevengUtils;
2829

2930
class PrimaryKeyBinder extends AbstractBinder {
@@ -194,7 +195,7 @@ private SimpleValue handleCompositeKey(
194195
PersistentClass rc,
195196
Set<Column> processedColumns,
196197
List<Column> keyColumns) {
197-
Component result = new Component(getMetadataBuildingContext(), rc);
198+
Component result = new EnhancedComponent(getMetadataBuildingContext(), rc);
198199
result.setMetaAttributes(Collections.EMPTY_MAP);
199200
result.setEmbedded(false);
200201
result.setComponentClassName(getCompositeIdName(rc));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.hibernate.tool.internal.reveng.util;
2+
3+
import java.util.Properties;
4+
5+
import org.hibernate.MappingException;
6+
import org.hibernate.boot.spi.MetadataBuildingContext;
7+
import org.hibernate.mapping.Component;
8+
import org.hibernate.mapping.PersistentClass;
9+
10+
@SuppressWarnings("serial")
11+
public class EnhancedComponent extends Component {
12+
13+
public EnhancedComponent(MetadataBuildingContext metadata, PersistentClass owner) throws MappingException {
14+
super(metadata, owner);
15+
}
16+
17+
@Override
18+
public Class<?> getComponentClass() throws MappingException {
19+
// we prevent ORM from trying to load a component class by name,
20+
// since at the point when we are building these, a corresponding class is not yet created
21+
// (so can't even think about it being compiled and able to load via any classloader) ...
22+
return Object.class;
23+
}
24+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Hibernate Tools, Tooling for your Hibernate Projects
3+
*
4+
* Copyright 2004-2021 Red Hat, Inc.
5+
*
6+
* Licensed under the GNU Lesser General Public License (LGPL),
7+
* version 2.1 or later (the "License").
8+
* You may not use this file except in compliance with the License.
9+
* You may read the licence in the 'lgpl.txt' file in the root folder of
10+
* project or obtain a copy at
11+
*
12+
* http://www.gnu.org/licenses/lgpl-2.1.html
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" basis,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
package org.hibernate.tool.hbm2x.hbx2840;
21+
22+
import java.io.File;
23+
24+
import org.hibernate.tool.api.export.Exporter;
25+
import org.hibernate.tool.api.export.ExporterConstants;
26+
import org.hibernate.tool.api.export.ExporterFactory;
27+
import org.hibernate.tool.api.export.ExporterType;
28+
import org.hibernate.tool.api.metadata.MetadataDescriptorFactory;
29+
import org.hibernate.tools.test.util.JUnitUtil;
30+
import org.hibernate.tools.test.util.JdbcUtil;
31+
32+
import org.junit.jupiter.api.AfterEach;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.api.io.TempDir;
36+
37+
/**
38+
* Test verifies that a foreign key to a table with a composite ID works.
39+
*/
40+
public class TestCase {
41+
42+
@TempDir
43+
public File outputDir = new File( "output" );
44+
45+
@BeforeEach
46+
void setUp() {
47+
JdbcUtil.createDatabase( this );
48+
Exporter exporter = ExporterFactory.createExporter( ExporterType.JAVA );
49+
exporter.getProperties().put(
50+
ExporterConstants.METADATA_DESCRIPTOR,
51+
MetadataDescriptorFactory.createReverseEngineeringDescriptor( null, null )
52+
);
53+
exporter.getProperties().put( ExporterConstants.DESTINATION_FOLDER, outputDir );
54+
exporter.getProperties().put( ExporterConstants.TEMPLATE_PATH, new String[0] );
55+
exporter.getProperties().setProperty( "ejb3", "true" );
56+
exporter.start();
57+
}
58+
59+
@AfterEach
60+
void tearDown() {
61+
JdbcUtil.dropDatabase( this );
62+
}
63+
64+
@Test
65+
void testFileExistence() {
66+
JUnitUtil.assertIsNonEmptyFile( new File( outputDir.getAbsolutePath() + "/Parent.java" ) );
67+
JUnitUtil.assertIsNonEmptyFile( new File( outputDir.getAbsolutePath() + "/Child.java" ) );
68+
JUnitUtil.assertIsNonEmptyFile( new File( outputDir.getAbsolutePath() + "/ParentId.java" ) );
69+
JUnitUtil.assertIsNonEmptyFile( new File( outputDir.getAbsolutePath() + "/ChildId.java" ) );
70+
}
71+
}

test/common/src/main/java/org/hibernate/tool/test/db/DbTestSuite.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class DbTestSuite {
5353
@Nested public class GenerateFromJDBCWithJavaKeyword extends org.hibernate.tool.hbm2x.GenerateFromJDBCWithJavaKeyword.TestCase {}
5454
@Nested public class IncrementalSchemaReading extends org.hibernate.tool.hbm2x.IncrementalSchemaReading.TestCase {}
5555
@Nested public class JdbcHbm2JavaEjb3 extends org.hibernate.tool.hbm2x.JdbcHbm2JavaEjb3.TestCase {}
56+
@Nested public class HBX2840 extends org.hibernate.tool.hbm2x.hbx2840.TestCase {}
5657
@Nested public class QueryExporterTest extends org.hibernate.tool.hbm2x.query.QueryExporterTest.TestCase {}
5758
@Nested public class HbmLintTest extends org.hibernate.tool.hbmlint.HbmLintTest.TestCase {}
5859
@Nested public class SchemaAnalyzer extends org.hibernate.tool.hbmlint.SchemaAnalyzer.TestCase {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE TABLE PARENT(ID1_1 INT NOT NULL, ID1_2 INT NOT NULL, PRIMARY KEY (ID1_1, ID1_2));
2+
CREATE TABLE CHILD (ID2_1 INT NOT NULL, ID2_2 INT NOT NULL, PRIMARY KEY (ID2_1, ID2_2), FOREIGN KEY (ID2_1, ID2_2) REFERENCES PARENT (ID1_1, ID1_2));
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DROP TABLE CHILD
2+
DROP TABLE PARENT

0 commit comments

Comments
 (0)