Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate, ant task, hbm2java, code generation and circular dendency?

 
Andy Westley
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I'm trying to get up to speed with hibernate and am following the basic tutorial, the tools tutorial, whilst trying use it on a project I'm working on. I'm trying to set up a bidirectional one-many relationship between a customer and a job. A customer will have many jobs, but job only have one customer. From every customer I want to be able to get to their job objects, and from every job I want access to the customer object. I'm using the hibernatetool ant task to generate class source code based on my mapping files:


This is a snippet from my ant target



When I run that target, I get the following output

[hibernatetool] Executing Hibernate Tool with a Standard Configuration
[hibernatetool] 1. task: hbm2java (Generates a set of .java files)
[hibernatetool] log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
[hibernatetool] log4j:WARN Please initialize the log4j system properly.
[hibernatetool] An exception occurred while running exporter #2:hbm2java (Generates a set of .java files)
[hibernatetool] To get the full stack trace run ant with -verbose
[hibernatetool] Failed in building configuration when adding /Users/andrew/Documents/workspace/JobRecordKeeper/conf/hibernate/data/Job.hbm.xml
[hibernatetool] org.hibernate.InvalidMappingException: Could not parse mapping document from file /Users/andrew/Documents/workspace/JobRecordKeeper/conf/hibernate/data/Job.hbm.xml
[hibernatetool] org.hibernate.MappingException: class uk.co.blueballoon.jobrecordkeeper.core.Job not found while looking for property: customer
[hibernatetool] java.lang.ClassNotFoundException: uk.co.blueballoon.jobrecordkeeper.core.Job
[hibernatetool] A class were not found in the classpath of the Ant task.
[hibernatetool] Ensure that the classpath contains the classes needed for Hibernate and your code are in the classpath.

BUILD FAILED
/Users/andrew/Documents/workspace/JobRecordKeeper/build.xml:155: Failed in building configuration when adding /Users/andrew/Documents/workspace/JobRecordKeeper/conf/hibernate/data/Job.hbm.xml

My guess is that it's telling me there is a problem which I can only describe as a circular dependency. When generating the code for the Job class, the task references the Customer class, which in turn references the Job class which is "under construction".

Is my understanding correct?

If it is, how do I solve the problem? I'm guessing that I'm complicating things by trying to generate my code rather than write it all from scratch - I know I'm running before I can walk, but the code generation seems to be a big advantage.

Can anyone shed any light for me?
 
Kadle Rajesh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This issue is not due to circular dependency, but due to the fact that Ant is trying to create Customer class, which needs a reference to Job class. Since Job class is not yet created we have this error.

I would suggest, you change the order so that, Job class (without any depenency) gets generated first and then Customer class (with dependency on Job) gets generated next.

This way we should be able to generate the Java code.

You can run before you walk
 
Andy Westley
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the speedy response.

Is changing the order a simple question of explicitly referencing the mapping files one by one in my ant fileset? At the moment I just include *.hbm.xml. It would be great if it could resolve those kind of dependencies.
[ October 20, 2008: Message edited by: Andy Westley ]
 
Andy Westley
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I tried the following and I get the same error:




Should I be adding a classpath somewhere so that I hbm2java knows where to look for references to othe generated classes it might need? None of the other examples I've seen do aything other thean *.hbm.xml. Surely I'm not doing anything that radical?!?
[ October 20, 2008: Message edited by: Andy Westley ]
 
Andy Westley
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, it turns out that if I specify the class of my many-to-one it all generates OK



Still happily using *.hbm.xml in my hibernatetool target.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic