Hibernate Upgrade (1.2.4 to 2.0 rc1) Redux
My last upgrade from Hibernate 1.2.x to 2.0.x was a little rocky, so I'm going to document it again today - this time with a Hibernate 2.0-enabled version of XDoclet, and code samples from last time. Today I'm upgrading from 1.2.4 to 2.0 rc1.
1. First, I downloaded Hibernate 2.0 rc1. BTW, I also updated to the latest version of XDoclet from CVS.
2. Next, I copied it to my project's lib directory, deleted bin, doc, and src directories. The only files I need are hibernate2.jar and (in the lib folder) c3p0.jar, cglib.jar, dom4j.jar, jcs.jar, jdom.jar and odmg.jar. One thing to note is with Hibernate 1.2.x, I didn't need dom4j.jar, jdom.jar or cglib.jar in my deployed WEB-INF/lib folder. However, with the new version, I did need to include them.
3. Then I updated lib/lib.properties and began changing my code with gool ol' Homesite.
4. Hey, step 4 is the same as last time! Edit build.xml file to pick up the new DTD. I changed <hibernate/>
to be <hibernate validatexml="true" version="2.0"/>
.
5. Using HomeSite, I did s/cirrus.hibernate/net.sf.hibernate/g
for all my .java and .xml files. Don't forget to change your log4.properties
and hibernate.properties
files.
6. In hibernate.cfg.xml
I changed cirrus.hibernate.sql.OracleDialect to net.sf.hibernate.dialect.OracleDialect. I also updated the DTD to 2.0 and moved all properties inside the <session-factory> element. Here's a sample hibernate.cfg.xml file from struts-resume.
7. In my ServiceLocator.java class, I changed the following from (this is so JUnit Tests can run w/o JNDI):
Datastore datastore = Hibernate.createDatastore();
datastore.storeClass(package.MyClass.class); // lots of these
sf = datastore.buildSessionFactory();
To:
sf = new Configuration().addClass(MyClass.class) // lots of these
.buildSessionFactory();
8. Time to bring Eclipse to the table. I refreshed the project and added the new hibernate2.jar to the "Java Build Path."
9. In my StartupListener.java (which initializes Hibernate for Tomcat), I changed the following code:
// Configure Hibernate. try { Hibernate.configure(); if (log.isDebugEnabled()) { log.debug("Hibernate configuration completed."); } } catch (HibernateException h) { log.fatal("Error configuring Hibernate!", h); }
To:
try { SessionFactory sf = new Configuration().configure().buildSessionFactory(); if (log.isDebugEnabled()) { log.debug("Hibernate configuration completed."); } } catch (HibernateException h) { log.fatal("Error configuring Hibernate!", h); }
10. Changed all instances of readonly="true" to inverse="false" inverse="true" for XDoclet tags. Also changed role to name.
11. Removed length attribute from any <key-property ... /> elements in my .hbm.xml files. These were rudely put there by the ReverseGenerator. Not all my mapping files are generated by XDoclet, if I have one with a composite-id, I just create the mapping file manually. I also had the change the DTD manually in these files.
12. In my build.xml
, I changed the SchemaExport class from net.sf.hibernate.tools to net.sf.hibernate.tool.hbm2ddl.
13. OK, here goes, my first attempt at compiling (honestly!). Darn - I forgot to save StartupListener.java - trying again (this time with "ant clean" first). Sweeeet - it worked!
14. Hmmm, some log messages I wasn't expecting. I forgot to change my log4j.properties
file from cirrus.hibernate to net.sf.hibernate. I'll add this above (step 5) to make this how to more accurate.
15. Running persistence tests - since I keep my Oracle driver (ojdbc14.jar) in ${hibernate.dir}/lib, I had to move it to the new directory. Now this is a strange error:
[junit] 2003-04-29 13:14:50,968 INFO [main] Configuration.addClass(264) | Mapping resource: com/ .../persistence/ChangeRequest.hbm.xml [junit] 2003-04-29 13:14:51,234 ERROR [main] XMLHelper$ErrorLogger.error(37) | Error parsing XML : XML InputStream(19) [junit] org.xml.sax.SAXParseException: Attribute "name" is required and must be specified for el ement type "param".
My guess is that it's being caused by:
<generator class="sequence"> <param>cr_master_sq</param> </generator>
After reading the Hibernate2 Porting Guidelines, looks like I need to add a name attribute. Didn't the exception tell me that! ;0)
I'll update this post when I figure out the answer to my question on Hibernate's forums...
Update: I had to 1) upgrade my XDoclet JARs, and 2) replace the following XDoclet tags:
* @hibernate.id column="cr_id" unsaved-value="null" * generator-class="sequence" generator-parameter-1="cr_master_sq"
with:
* @hibernate.id column="cr_id" unsaved-value="null" generator-class="sequence" * @hibernate.generator-param name="sequence" value="cr_master_sq"
Now I'm experiencing this issue.
Update 2: I discovered (from Gavin) that readonly="true" is the same as inverse="true". I've updated step 10 above, and now my upgrade is complete!