Oh ja, natürlich.. Gute Idee, hätt ich ja fast mal selbst drauf kommen können.
Also.
Als erstes meine Customer-Tabelle (Customer.hbm.xml
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>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="de.nfranze.sf.model.Customer"
table="customer"
>
<id
name="id"
type="java.lang.Integer"
column="id"
>
<generator class="native" />
</id>
<property
name="state"
type="short"
column="state"
not-null="true"
length="10"
/>
<property
name="nick"
type="java.lang.String"
column="nick"
not-null="true"
length="40"
/>
<property
name="name"
type="java.lang.String"
column="name"
not-null="true"
length="50"
/>
<property
name="firstname"
type="java.lang.String"
column="firstname"
not-null="true"
length="40"
/>
<property
name="birthdate"
type="java.sql.Date"
column="birthdate"
length="10"
/>
<!-- Associations -->
<!-- bi-directional one-to-one association to Custcontact -->
<one-to-one
name="custcontact"
class="de.nfranze.sf.model.Custcontact"
outer-join="auto"
constrained="true"
/>
</class>
</hibernate-mapping>
Und nun die Custcontact.hbm.xml, welche die Kontaktdaten zu diesem Kontakt enthält.
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>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="de.nfranze.sf.model.Custcontact"
table="custcontact"
>
<id
name="customerid"
type="java.lang.Integer"
column="customerid"
>
<generator class="native" />
</id>
<property
name="phone"
type="java.lang.String"
column="phone"
length="40"
/>
<property
name="fax"
type="java.lang.String"
column="fax"
length="40"
/>
<property
name="mail"
type="java.lang.String"
column="mail"
length="40"
/>
<property
name="mobile"
type="java.lang.String"
column="mobile"
not-null="true"
length="45"
/>
<!-- Associations -->
<!-- bi-directional one-to-one association to Customer -->
<one-to-one
name="customer"
class="de.nfranze.sf.model.Customer"
outer-join="auto"
/>
</class>
</hibernate-mapping>
Wobei mir auffällt.. Wo steht denn eigentlich, über welche Spalten die Schlüssel verbunden sind?
Ich generiere alle meine Dateien mit Ant. Hier mal der Auszug meiner Build.xml, welcher diese hbms generiert.
Code:
<target name="Generate Database Objects" description="Middlegen hbm-Generation from MySQL">
<taskdef name="middlegen" classname="middlegen.MiddlegenTask">
<classpath refid="master-classpath"/>
</taskdef>
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask">
<classpath refid="master-classpath"/>
</taskdef>
<middlegen
appname="Sf"
gui="false"
databaseurl="jdbc:mysql://fileserver/sf"
driver="com.mysql.jdbc.Driver"
username="root"
password="blabla"
schema="sf"
includeViews="false"
>
<hibernate
destination="${web.dir}/src"
package="de.nfranze.sf.model"
standardGeneratorScheme="native"
javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper"/>
</middlegen>
<hibernatetool destdir="${web.dir}/src">
<configuration>
<fileset dir="${model.dir}">
<include name="**/*.hbm.xml"/>
</fileset>
</configuration>
<classpath refid="master-classpath"/>
<hbm2java jdk5="true"/>
</hibernatetool>
</target>
Es wäre schön, wenn die Dateien gleich richtig generiert werden würden.[/code]