hi
this is exactly the type of thing hibernate is supposed to do for you.
i don't write mapping files, so i can't help you if all you want to do is write the *.hbm.xml files. but you don't have to do that, take a look at xdoclet.
xdoclet is a code generator, that reads javadoc comments from your java files and can generate these clumsy mapping files for you. it's really nice, no need to write xml and keep those files in sync when you change your classes.
first you'll need to download xdoclet and place these files
xdoclet.jar
xdoclet-hibernate-module.jar
xdoclet-xdoclet-module.jar
xjavadoc.jar
in your class path. if you're using java 1.5 (you should be!) you'll have to download xjavadoc1-5.jar from their site seperately. once you have those files set, you'll need ant.
i hope you're using eclipse which has ant in it, if not you'll have to download it and use it from the command line.
here's the build.xml to run xdoclet (generate the .hbm.xml and the hibernate.cfg.xml) AND run the schema export task to create the tables and link them up for you (how nice is that?!)
Code:
<project>
<target name="generate-hbm">
<taskdef name="hibernatedoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref="project.classpath" />
<property file="${conf.dir}/hibernate.properties" />
<hibernatedoclet destdir="${classes.dir}"
verbose="true">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
<hibernate version="3.0" />
<hibernatecfg dialect="${hibernate.dialect}"
jdbcUrl="${hibernate.connection.url}"
driver="${hibernate.connection.driver_class}"
userName="${hibernate.connection.username}"
password="${hibernate.connection.password}"
showSql="true"
version="3.0" />
</hibernatedoclet>
</target>
<target name="schema-export" >
<taskdef name="schemaexport"
classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref="project.classpath" />
<schemaexport quiet="no"
output="${conf.dir}/schema-export.sql"
properties="${conf.dir}/hibernate.properties">
<fileset dir="${classes.dir}">
<include name="**/*.hbm.xml"/>
</fileset>
</schemaexport>
</target>
</project>
so if you run ant generate-hbm it will scan your java files and make those files for you. if you run ant schema-export it will create the tables and set all the foreign keys and stuff.
now to answer your question, to tell xdoclet what is what and how they relate, you have to comment your classes.
Code:
/**
* @hibernate.class
*/
public class ManyClass implements Serializable {
private Long id;
private String something;
private OneClass oneThing;
/**
* @hibernate.id generator-class="native"
*/
public Long getId() {
return id;
}
/**
* @hibernate.property
*/
public String getSomething() {
return something;
}
/**
* @hibernate.many-to-one column="oneClass_id"
*/
public OneClass getOneThing() {
return oneThing;
}
// setter methods for all properties ommitted
}
/**
* @hibernate.class
*/
public class OneThing implements Serializable {
private Long id;
private Date date;
/**
* @hibernate.id generator-class="native"
*/
public Long getId() {
return id;
}
/**
* @hibnerate.property
*/
public Date getDate() {
return date;
}
// setter methods
}