Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
Hi,
My hibernate mapping file,
Track.hbm.xml is given below:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.oreilly.hh.Track" table="TRACK">
<meta attribute="class-description">
Represents a single playable track in the music database.
@author Vara Prasad Bokka (with help from Hibernate)
</meta>
<id name="id" type="int" column="TRACK_ID">
<meta attribute="scope-set">protected</meta>
<generator class="assigned"/>
</id>
<property name="title" type="string" not-null="true"/>
<property name="filePath" type="string" not-null="true"/>
<property name="playTime" type="time">
<meta attribute="field-description">Playing time</meta>
</property>
<property name="added" type="date">
<meta attribute="field-description">When the track was created</meta>
</property>
<property name="volume" type="short" not-null="true">
<meta attribute="field-description">How loud to play the track</meta>
</property>
</class>
</hibernate-mapping>
My
build.xml is given below:
Code:
<?xml version="1.0"?>
<project name="Harnessing Hibernate: The Developer's Notebook" default="db" basedir=".">
<!-- Set up properties containing important project directories -->
<property name="source.root" value="src"/>
<property name="class.root" value="classes"/>
<property name="lib.dir" value="lib"/>
<property name="data.dir" value="data"/>
<!-- Set up the class path for compilation and execution -->
<path id="project.class.path">
<!-- Include our own classes, of course -->
<pathelement location="${class.root}" />
<!-- Include jars in the project library directory -->
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<target name="db" description="Runs HSQLDB database management UI
against the database file--use when application is not running">
<java classname="org.hsqldb.util.DatabaseManager" fork="yes">
<classpath refid="project.class.path"/>
<arg value="-driver"/>
<arg value="org.hsqldb.jdbcDriver"/>
<arg value="-url"/>
<arg value="jdbc:hsqldb:${data.dir}/music"/>
<arg value="-user"/>
<arg value="sa"/>
</java>
</target>
<!-- Teach Ant how to use Hibernate's code generation tool -->
<taskdef name="hbm2java"
classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
classpathref="project.class.path"/>
<!-- Generate the java code for all mapping files in our source tree -->
<target name="codegen" description="Generate Java source from the O/R mapping files">
<hbm2java output="${source.root}">
<fileset dir="${source.root}">
<include name="**/*.hbm.xml"/>
</fileset>
</hbm2java>
</target>
</project>
When I run the build file, it generates the Track.java file as given below:
Code:
package com.oreilly.hh;
import java.io.Serializable;
import java.util.Date;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
* Represents a single playable track in the music database.
* @author Vara Prasad Bokka (with help from Hibernate)
*
*/
public class Track implements Serializable {
/** identifier field */
private Integer id;
/** persistent field */
private String title;
/** persistent field */
private String filePath;
/** nullable persistent field */
private Date playTime;
/** nullable persistent field */
private Date added;
/** persistent field */
private short volume;
/** full constructor */
public Track(Integer id, String title, String filePath, Date playTime, Date added, short volume) {
this.id = id;
this.title = title;
this.filePath = filePath;
this.playTime = playTime;
this.added = added;
this.volume = volume;
}
/** default constructor */
public Track() {
}
/** minimal constructor */
public Track(Integer id, String title, String filePath, short volume) {
this.id = id;
this.title = title;
this.filePath = filePath;
this.volume = volume;
}
public Integer getId() {
return this.id;
}
protected void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFilePath() {
return this.filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
/**
* Playing time
*/
public Date getPlayTime() {
return this.playTime;
}
public void setPlayTime(Date playTime) {
this.playTime = playTime;
}
/**
* When the track was created
*/
public Date getAdded() {
return this.added;
}
public void setAdded(Date added) {
this.added = added;
}
/**
* How loud to play the track
*/
public short getVolume() {
return this.volume;
}
public void setVolume(short volume) {
this.volume = volume;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
}
Now the problem is that it doesn't generate the following methods:
Code:
public boolean equals(Object other) {
if(!(other instanceof Track)) return false;
Track castOther = (Track) other;
return new EqualsBuilder().append(this.getId(), castOther.getId()).isEquals();
}
Code:
public int hashCode() {
return new HashCodeBuilder().append(getId()).toHashCode();
}
And also it gives the following error message:
import org.apache.commons.lang.builder.ToStringBuilder;
The import org.apache.commons cannot be resolved
What might be the problem?
Please help?
Regards,
Vara Prasad Bokka,