kopf.lastig Alles mögliche zu Themen wie Familie, Beruf, Medien etc. …

2005-09-26

„Hibernate – A Developers Notebook“ – migrating to Hibernate 3.0, Chapter 6

Filed under: Java — Schlagwörter: , , — Erik @ 09:47

You can skip the entire chapter 6 if you use Hibernate 3. It is based on the interface PersistenceEnum which already became deprecated in Hibernate 2 as the author points out in the errata. The interface has apparently removed in Hibernate 3.

„Hibernate – A Developers Notebook“ – migrating to Hibernate 3.0, Chapter 5

Filed under: Java — Schlagwörter: , , — Erik @ 09:44

As in chapter 4, copy the hbm.xml-files and change the DTD-reference. Leave CreateTest.java, QueryTest.java and QueryTest2.java alone – they will still compile. Copy the AlbumTest.java file, change the hibernate-imports and the constructor calls as well as int parameters to Integer.

This should be easy as we have done that before.

2005-09-15

„Hibernate – A Developers Notebook“ – migrating to Hibernate 3.0, Chapter 4

Filed under: Java — Schlagwörter: , , — Erik @ 16:26

In this chapter, we add an Entity called „Artist“ as well as an entity called „Comments“.

Copy both Track.hbm.xml and Artist.hbm.xml from the examples-distribution and remember to change the DTD-reference if necessary.
When using „ant schema„, SAX complained it couldn’t find ${src.root}/com/oreilly/hh/hibernate-mapping-2.0.dtd for Artist.hbm.xml.
That’s strange because it didn’t complain about it in Track.hbm.xml before. However, I place the DTD file in ${src.root}/com/oreilly/hh
and the prepare-target now copies *.dtd file, too.

The code generation is no problem. I has changed CreateTest.java again according to Java-beans coding style.
The package name in QueryTest.java and QueryTest2.java needs to be changed again from „net.sf.hibernate...“ to „org.hibernate...“ and everything works.

So that’s it for chapter 4.

2005-09-12

JSSE 1.0 bug keeps me busy

Filed under: Java — Schlagwörter: — Erik @ 17:16

A bug in JSSE 1.0.3 keeps me busy the whole monday – ending up with migrating from JDK1.2.2 to JDK1.4.2 „out of the cold“.

In one of my maintenance project that realize some kind of an online-shop, „we“ use a third-party website for payment. Last friday afternoon they installed a new SSL-certificate. Since then, the shop couldn’t connect to that website with JSSE 1.0.3 (itself running an old setup with JDK1.2.2 and JRun – to be overhauled next month).

Tracing revealed „issuer != subject DN“ – even for JDK1.4.2 . I couldn’t figure out why. I tried to store the certificate in a keystore and cacerts but it wouldn’t work either. So I tried a workaround using a dummy trust manager. It worked for JDK1.4.2 but not for JDK1.2.2.

In the end i figured out the server certificate had something like
O=#0C294A3...
in the subject which JSSE 1.0 cannot parse. So even without evaluating the certificate it didn’t work. I was stuck here.

So we had no choice but to immediately upgrade to JDK1.4.2 since otherwise there was no payment possible. Luckily, it worked great! No problems so far!

If the thirdparty had noticed us about the new certificate, I had detected the problem as early as friday afternoon. But that’s life I guess.

2005-09-08

„Hibernate – A Developers Notebook“ – migrating to Hibernate 3.0, Chapter 3

Filed under: Java — Schlagwörter: , , — Erik @ 17:07

In chapter 3 (you can read it online) we use the Track table to insert some data and to query data.

We use CreateTest to create and QueryTest to query data. Some simple adaptions are necessary:

  • change package name of hibernate classes to org.hibernate
  • replace the use of the all-attributes-as-parameters-constructor with Java-Bean style use of empty constructors and setters
  • change the type of „volume“ attribute from short to java.lang.Short

Here is the changed part of CreateTest.java:
[java]
Track track = new Track();
track.setTitle(„Russian Trance“);
track.setFilePath(„vol2/album610/track02.mp3“);
track.setPlayTime(Time.valueOf(„00:03:30“));
track.setAdded(new Date());
track.setVolume(new Short((short)0));
session.save(track);

track = new Track();
track.setTitle(„Video Killed the Radio Star“);
track.setFilePath(„vol2/album611/track12.mp3“);
track.setPlayTime(Time.valueOf(„00:03:49“));
track.setAdded(new Date());
track.setVolume(new Short((short)0));
session.save(track);

track = new Track();
track.setTitle(„Gravity’s Angel“);
track.setFilePath(„vol2/album175/track03.mp3“);
track.setPlayTime(Time.valueOf(„00:06:06“));
track.setAdded(new Date());
track.setVolume(new Short((short)0));
session.save(track);
[/java]

QueryTest needs only substitution of hibernate package names.

I also created a ant-target called „clean-db“ which deletes the entire schema in order to avoid multiple insertion of the same data as well as an ant-target called „clean“ which simply deletes the „classes“-folder.

[xml]









[/xml]

So now you can say

  ant clean-db clean schema codegen ctest qtest

and you get a clean db with a new schema, freshly generated code, test data generation and query test – in one command.

2005-09-06

„Hibernate – A Developers Notebook“ – migrating to Hibernate 3.0, Chapter 2

Filed under: Java — Schlagwörter: , , — Erik @ 14:45

This is chapter 2 of rewriting the examples of O’Reillys book „Hibernate – A Developers Notebook“ for using Hibernate 3.0 instead of Hibernate 2.x . To get the context, read chapter 1.

The goal of chapter 2 is to write a hbm file for a single table, generating the corresponding java file and compile it.

A main difference when using Hibernate 3.0 is the ant task definition for hbm2java. It now looks like this:
[xml]








[/xml]
Also, the package-name of the schemaexport-task has changed:
[xml]

[/xml]

Another difference is the jdbc-url for HSQLDB 1.8. You have to insert „file“ as in:

jdbc:hsqldb:file:${data.dir}/music

(in ant task „db“) or

jdbc:hsqldb:file:data/music

in hibernate.properties

I developed offline so I ran into the problem that the XML parser wants to download the mapping-dtd as advertised in the DOCTYPE-section of the hbm.xml file. I stripped off the http://www.hibernate.org part of the url, downloaded the dtd and placed it in the root-directory.

After using Ant 1.6, everything worked fine. (Otherwise I got:
[java]
java.lang.NoSuchMethodError: org.apache.tools.ant.Project.createClassLoader
(Lorg/apache/tools/ant/types/Path;)Lorg/apache/tools/ant/AntClassLoader;
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:113)
)…
[/java]

The generated code reveals some differences:

  • The type of the volume attribute is java.lang.Short, not short „as advertised“. This is because of the xml-attribute „not-null“ set to null. However if you ommit this xml-attribute, java.lang.Short is used again.
  • There is no all-attributes-as-parameters-constructor.
  • There is no equals and no hash-code implementation.

I have not yet any idea about why’s that. Comments are welcome. Point 2 + 3 might be because different code templates.

2005-09-05

„Hibernate – A Developers Notebook“ – migrating to Hibernate 3.0, Chapter 1

Filed under: Java — Schlagwörter: , , — Erik @ 14:55

In this blog series I want to go through the examples in James Elliotts book „Hibernate – a developers notebook“ using
object/relation mapping service Hibernate 3.0 instead of Hibernate 2.x that this book was written about and noting the differences, i.e. what I had to change to get them working.

I enjoyed reading „Hibernate – a developers notebook“ by James Elliott . In order to put my hands on hibernate I want to try out the examples of the book. While it describes Hibernate 2 I want to try it out with Hibernate 3.0 . Not having used Hibernate before I’m a complete newbie and may stumble over things an experienced Hibernate-developer has no problems with.

In the first chapter we are encouraged to download Apache Ant, Hibernate and HSQLDB (a free pure-Java DBMS that is used throughout the book).

I didn’t download Ant in the first place because I already had Ant 1.5 installed. Foreshadowing part 2, it will turn out that Apache Ant 1.6 is required for the HibernateToolTask so I later installed that.

I then downloaded HSQLDB 1.8.0.2 without problems.

From some Java Magazin CD-ROM I used a source distribution of Hibernat 3.0.1, installed and compiled it. In order to create
java-files from hbm-files we need hibernate-tools.jar which we get by downloading hibernate-tools-3.1.0.alpha5.zip and extracting it from org.hibernate.eclipse_3.1.0.alpha5.jar that lives within that zip-file.

Finally, I ended up with the following files in the examples lib directory:

ant-1.6.2.jar
ant-antlr-1.6.2.jar
ant-junit-1.6.2.jar
ant-launcher-1.6.2.jar
antlr-2.7.5H3.jar
asm-attrs.jar
asm.jar
cglib-2.1.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.jar
ehcache-1.1.jar
hibernate-tools.jar
hibernate3.jar
hsqldb.jar
jdbc2_0-stdext.jar
jta.jar
jtidy-r8-21122004.jar
log4j-1.2.9.jar
velocity-1.4.jar
velocity-tools-generic-1.1.jar
xerces-2.6.2.jar
xml-apis.jar

I’m not sure if every single jar is required (especially the ant-xxx.jar files).

2005-09-01

Tomcat and syslog and access.log

Filed under: Java — Schlagwörter: — Erik @ 13:34

A colleague of mine asked me how to route logging output of Jakarta Tomcat 5.0 to Unix syslog. This is rather easy since tomcat uses a library called „jakarta commons logging“ which in turn can use „log4j“ which itself can log to Unix Syslog.

First, download log4j and commons-logging. Now copy log4j-1.2.x.jar and commons-logging.jar to tomcats common/lib directory. Create a file „log4j.properties“ in tomcats common/classes directory with the following content:

log4j.rootLogger=INFO,sysout,syslog

# sysout = Konsole
log4j.appender.sysout=org.apache.log4j.ConsoleAppender
log4j.appender.sysout.layout=org.apache.log4j.PatternLayout
log4j.appender.sysout.layout.ConversionPattern=%d{ISO8601} %-5p [%t] [%c] %m%n

####
# Configuration for syslog
##
# syslog = Syslog
log4j.appender.syslog=org.apache.log4j.net.SyslogAppender
# which Syslog-Facility is going to be used
# for valid values see
# http://logging.apache.org/log4j/docs/api/apache/log4j/net/SyslogAppender.html#getFacility(java.lang.String)
log4j.appender.syslog.facility=LOCAL0
log4j.appender.syslog.layout.ConversionPattern=%-5p [%t] [%c]: %m%n
log4j.appender.syslog.layout=org.apache.log4j.PatternLayout
# enter your host here
log4j.appender.syslog.SyslogHost=localhost

This log4j.properties configures log4j for writing to standard-output as well as to syslog-daemon, facility LOCAL0. You may change the logging format according to the documentation. If you want to disable logging to standard-output, remove „sysout“ from the first line in log4j.properties.

There are more tips as well as a list of Log-Identifiers in Tomcat 5.0.27 .

My colleague had one more wish: to redirect the access.log to syslog. This is more of a problem since the default tomcat implementation of access-logging (called AccessLogValve) uses direct file access instead of using common-loggings. Since it is open source, I took that implementation and replaced that file access to a common-loggings call. Download this tiny jar and place it in tomcats server/lib (not common/lib !!). Replace your usual „org.apache.catalina.valves.AccessLogValve“ with „de.epischel.tomcat.LoggingAccessLogValve“. The logging name „tomcat.accesslog“ and INFO-level is used and you can assign a different output destination (called appender) in log4j.properties:

log4j.additivity.tomcat.accesslog=false
log4j.logger.tomcat.accesslog=INFO,syslog2

where syslog2 may e.g. be a syslog appender like „syslog“ above but with a different facility or a different syslog-host.

Task done. 🙂

Powered by WordPress