I'm connecting to a production Oracle 10g database and attempting to generate a Hibernate configuration file using the
hibernate3-maven-plugin running in Maven.
However when I run the
hibernate3:hbm2cfgxml goal I get the following error:
Code:
An association from the table FOO refers to the unmapped class com.whatever.domain.Bar
My maven project file looks like this:
Code:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<components>
<component>
<name>hbm2cfgxml</name>
<implementation>jdbcconfiguration</implementation>
<outputDirectory>target/classes</outputDirectory>
</component>
</components>
<componentProperties>
<propertyfile>src/main/resources/database.properties</propertyfile>
</componentProperties>
</configuration>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>11.1.0.6.0</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.1_3</version>
</dependency>
</dependencies>
</plugin>
My
database.properties file contains the following:
Code:
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.url=jdbc:oracle:thin:@<server>:<host>:<db>
hibernate.connection.username=<username>
hibernate.connection.password=<password>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.default_schema=<schema>
Because this is a production database, then I assume that the schema is sound and expect the
hibernate3-maven-plugin to extract that schema and generate a hibernate configuration file without error.
I've narrowed down the offending tables to the following:
FOO
- foreign key 'a' in BAR
- foreign key 'b' in BAR
- foreign key 'c' in QUUX
- primary key is composite of foreign keys 'a', 'b' in BAR and foreign key 'c' in QUUX
BAR
- foreign key 'a' in BAZ
- foreign key 'b' in QUX
- primary key is a composite of the foreign key 'a' in BAZ and foreign key 'b' in QUX
BAZ
- primary key 'a'
- no foreign keys
QUX
- primary key 'b'
- no foreign keys
QUUX
- primary key 'c'
- no foreign keys
If I specify the tables FOO, BAR and BAZ in a
reveng.xml file, then the
hbm2cfgxml goal is successful. If I specify the tables FOO, BAR, and QUX in a
reveng.xml file, then the
hbm2cfgxml goal is successful. But if tables FOO, BAR, BAZ, and QUX are specified then the
hbm2cfgxml goal fails with the aforementoned association error between FOO and com.whatever.domain.Bar file. That is, if both tables representing the foreign keys in BAR are present, then the goal fails. If just one foreign key table is present, then the goal succeeds.
Any suggestions on what is causing the problem, and how to fix it?
I'm thinking that one solution may be to create a
*.reveng.file and list each table in the correct order. Or perhaps run two iterations: one where the
reveng.xml file specifies all tables except BAZ, and one where the
reveng.xml specifies only the table BAZ.
But I'd really prefer not to do that since I am trying to automate the code generation as much as possible, and my
reveng.xml file should only specify the schema and not the tables within that schema.
edit: I have a partial solution that is an ugly hack: I set up two profiles in maven, where each profile points to a different
reveng.xml file. The first profile specifies a
reveng.xml file that includes all tables in the schema except for the table BAZ. The second profile specifies a
reveng.xml file that only includes the table BAZ. I then run
mvn hibernate3:hbm2java -Pprofile1 and
mvn hibernate3:hbm2java -Pprofile2 to get the full set of java files.
This will not work for the
hbm2cfgxml goal, since it will generate different
hibernate.cfg.xml files. There must be a way to generate a
hibernate.cfg.xml file that simply reads the database properties without contacting the database, and which can also specify a package name for mapping?
edit: creating two profiles so that file generation is partitioned is not be a good hack, either. I am getting the association error when loading the annotated classes into Hibernate. And if I try generating mapping files then I get a "foreign key must have same number of columns as referenced primary key" error. It could be that some information is lost this way, information that is needed for consistency. Sigh.
edit: I'm wondering if the problem here is that FOO references foreign keys 'a' and 'b' in BAR, but those columns in BAR are foreign keys in BAZ and QUX respectively. Would the problem be solved if FOO references foreign key 'a' in BAZ and 'b' in QUX directly?
edit: I also ran hibernate tools under ant and got the same problem, so it's not a maven or hibernate3 plugin issue.