In order to generate the next SQL code:
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null);
create table user_roles(
username varchar(15) not null,
role_name varchar(15) not null,
primary key(usernmae, rolename)
);
You can use code such as:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="databaselayer.users.UserDB" table="users">
<id name="username" type="string" column="user_name">
<meta attribute="scope-set">public</meta>
</id>
<property name="password" type="string" column="user_pass" not-null="true"/>
<set name="roles" cascade="save-update" inverse="true">
<key column="user_name"/>
<one-to-many class="databaselayer.users.RoleDB"/>
</set>
</class>
<class name="databaselayer.users.RoleDB" table="user_roles">
<composite-id>
<key-many-to-one name="username" class="databaselayer.users.UserDB" column="user_name"/>
<key-property name="role" type="string" column="role_name"/>
</composite-id>
</class>
</hibernate-mapping>
You may need to have your classes as well as hbm mapping files in your classpath.
Next I post my Ant schema target:
Note ${basedir}/build/WEB-INF/classes contains both *.class and *.hbm.xml files.
<target name="schema" description="Generate DB schema from the O/R mapping files">
<hibernatetool destdir="${basedir}">
<classpath path="${basedir}/build/WEB-INF/classes">
<fileset dir="${basedir}/build/WEB-INF/classes">
<include name="**/*"/>
</fileset>
</classpath>
<configuration configurationfile="${basedir}/hibernate.cfg.xml"/>
<hbm2ddl
drop="true"
create="true"
export="true"
outputfilename="libamo2-ddl.sql"
delimiter=";"
format="true"/>
</hibernatetool>
</target>