The codegen target
at line 38 uses the new
hbm2java task to run Hibernate's
code generator on any mapping documents found in the
src tree, writing the corresponding Java source.
The pattern
"**/*.hbm.xml"
means "any file ending in
.hbm.xml, within the specified directory, or any
subdirectory, however deeply nested."
Let's try it! From within your top-level project
directory (the folder containing build.xml),
type the following command:
1 ant codegen
You should see output like this:
1 Buildfile: build.xml
2
3 codegen:
4 [hbm2java] Processing 1 files.
5 [hbm2java] Building hibernate objects
6 [hbm2java] log4j:WARN No appenders could be
found for logger (net.sf.
7 hibernate.util.DTDEntityResolver).
8 [hbm2java] log4j:WARN Please initialize the log4j
system properly.
The warnings are griping about the fact that we
haven't taken the trouble to set up the logging
environment that Hibernate expects. We'll see how to
do that in the next example. For now, if you look in the directory
src/com/oreilly/hh, you'll see
that a new file named Track.java has appeared,
with the content shown in Example 2-3.
Example 2-3. Code generated from the Track mapping document
1 package com.oreilly.hh;
2
3
4 import java.io.Serializable;
5 import java.util.Date;
6 import org.apache.commons.lang.builder.EqualsBuilder;
7 import org.apache.commons.lang.builder.HashCodeBuilder;
8 import org.apache.commons.lang.builder.ToStringBuilder;
9
10 /**
11 * Represents a single playable track in the music database.
12 * @author Jim Elliott (with help from Hibernate)
13 *
14 */
15 public class Track implements Serializable {
16
17 /** identifier field */
18 private Integer id;
19
20 /** persistent field */
21 private String title;
22
23 /** persistent field */
24 private String filePath;
25
26 /** nullable persistent field */
27 private Date playTime;
28
29 /** nullable persistent field */
30 private Date added;
31
32 /** nullable persistent field */
33 private short volume;
34
35 /** full constructor */
36 public Track(String title, String filePath, Date playTime,
37 Date added, short volume) {
38 this.title = title;
39 this.filePath = filePath;
40 this.playTime = playTime;
41 this.added = added;
42 this.volume = volume;
43 }
44
45 /** default constructor */
46 public Track( ) {
47 }
48
49 /** minimal constructor */
50 public Track(String title, String filePath) {
51 this.title = title;
52 this.filePath = filePath;
53 }
54
55 public Integer getId( ) {
56 return this.id;
57 }
58
59 protected void setId(Integer id) {
60 this.id = id;
61 }
62
63 public String getTitle( ) {
64 return this.title;
65 }
66
67 public void setTitle(String title) {
68 this.title = title;
69 }
70
71 public String getFilePath( ) {
72 return this.filePath;
73 }
74
75 public void setFilePath(String filePath) {
76 this.filePath = filePath;
77 }
78
79 /**
80 * Playing time
81 */
82 public Date getPlayTime( ) {
83 return this.playTime;
84 }
85
86 public void setPlayTime(Date playTime) {
87 this.playTime = playTime;
88 }
89
90 /**
91 * When the track was created
92 */
93 public Date getAdded( ) {
94 return this.added;
95 }
96
97 public void setAdded(Date added) {
98 this.added = added;
99 }
100
101 /**
102 * How loud to play the track
103 */
104 public short getVolume( ) {
105 return this.volume;
106 }
107
108 public void setVolume(short volume) {
109 this.volume = volume;
110 }
111
112 public String toString( ) {
113 return new ToStringBuilder(this)
114 .append("id", getId( ))
115 .toString( );
116 }
117
118 public boolean equals(Object other) {
119 if ( !(other instanceof Track) ) return false;
120 Track castOther = (Track) other;
121 return new EqualsBuilder( )
122 .append(this.getId( ), castOther.getId( ))
123 .isEquals( );
124 }
125
126 public int hashCode( ) {
127 return new HashCodeBuilder( )
128 .append(getId( ))
129 .toHashCode( );
130 }
131
132 }
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.