Generatinga clean and believable-looking hibernate mapping, although not yet proven to work.

This commit is contained in:
sb 2008-02-07 17:45:32 +00:00
parent f64f137a67
commit 4c4a16631c
2 changed files with 93 additions and 41 deletions

View file

@ -16,8 +16,8 @@
are generated.
$Author: sb $
$Revision: 1.4 $
$Date: 2008-02-07 16:35:00 $
$Revision: 1.5 $
$Date: 2008-02-07 17:45:32 $
-->
<xsl:stylesheet version="1.0"
@ -47,7 +47,7 @@
* THIS FILE IS AUTOMATICALLY GENERATED AND SHOULD NOT
* BE MANUALLY EDITED.
*
* Generated using adl2canonical.xsl revision <xsl:value-of select="substring('$Revision: 1.4 $', 12)"/>
* Generated using adl2canonical.xsl revision <xsl:value-of select="substring('$Revision: 1.5 $', 12)"/>
*
***************************************************************************
</xsl:comment>
@ -134,6 +134,9 @@
<xsl:value-of select="normalize-space( $key)"/>
</xsl:attribute>
<generator action="native"/>
<documentation>
Auto-generated abstract primary key
</documentation>
</property>
</key>
<xsl:apply-templates select="adl:property | adl:one-to-many | adl:many-to-many | adl:many-to-one"/>

View file

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns="urn:nhibernate-mapping-2.2"
xmlns:adl="http://cygnets.co.uk/schemas/adl-1.2"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:hbn="urn:nhibernate-mapping-2.2">
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
Application Description Framework
adl2hibernate.xsl
@ -12,7 +12,7 @@
Transform ADL to Hibernate
$Author: sb $
$Revision: 1.2 $
$Revision: 1.3 $
-->
<!--
@ -24,7 +24,7 @@
-->
<xsl:param name="abstract-key-name-convention" select="Id"/>
<xsl:output indent="yes" method="xml" encoding="UTF-8"/>
<xsl:output indent="no" method="xml" encoding="UTF-8"/>
<!-- NOTE! indent="no" because hibernate falls over if there is whitespace inside
a 'key' or 'one-to-many' element, and the printer used by the NAnt 'style' task
does not tag-minimize on output. If you change this the build will break, you
@ -36,7 +36,7 @@
<xsl:variable name="assembly">SRU.Hospitality.DataModel</xsl:variable>
<xsl:template match="adl:application">
<hbn:hibernate-mapping>
<hibernate-mapping>
<xsl:attribute name="namespace">
<xsl:value-of select="$namespace"/>
</xsl:attribute>
@ -53,12 +53,12 @@
* THIS FILE IS AUTOMATICALLY GENERATED AND SHOULD NOT
* BE MANUALLY EDITED.
*
* Generated using adl2hibernate-mapping.xsl revision <xsl:value-of select="substring('$Revision: 1.2 $', 12)"/>
* Generated using adl2hibernate-mapping.xsl revision <xsl:value-of select="substring('$Revision: 1.3 $', 12)"/>
*
***************************************************************************
</xsl:comment>
<xsl:apply-templates select="adl:entity"/>
</hbn:hibernate-mapping>
</hibernate-mapping>
</xsl:template>
@ -66,7 +66,7 @@
<xsl:template match="adl:entity">
<xsl:apply-templates select="adl:documentation"/>
<hbn:class>
<class>
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
@ -75,14 +75,15 @@
</xsl:attribute>
<xsl:apply-templates select="adl:key"/>
<xsl:apply-templates select="adl:property"/>
</hbn:class>
</class>
</xsl:template>
<xsl:template match="adl:key">
<xsl:choose>
<xsl:when test="count( property) = 0"/>
<xsl:when test="count( property) = 1">
<hbn:id>
<xsl:when test="count( adl:property) = 0">
</xsl:when>
<xsl:when test="count( adl:property) = 1">
<id>
<xsl:attribute name="name">
<xsl:value-of select="adl:property[position()=1]/@name"/>
</xsl:attribute>
@ -101,17 +102,66 @@
<xsl:with-param name="property" select="adl:property[position()=1]"/>
</xsl:call-template>
</xsl:attribute>
<xsl:apply-templates select="adl:generator"/>
</hbn:id>
<xsl:apply-templates select="adl:property[position()=1]/adl:generator"/>
</id>
</xsl:when>
<xsl:otherwise>
<xsl:comment>Composite primary key - not yet implemented in adl2hibernate</xsl:comment>
<composite-id>
<xsl:attribute name="name">
<xsl:choose>
<xsl:when test="$abstract-key-name-convention='Name'">
<xsl:value-of select="ancestor::adl:entity/@name"/>
</xsl:when>
<xsl:when test="$abstract-key-name-convention = 'NameId'">
<xsl:value-of select="concat( ancestor::adl:entity/@name, 'Id')"/>
</xsl:when>
<xsl:when test="$abstract-key-name-convention = 'Name_Id'">
<xsl:value-of select="concat( ancestor::adl:entity/@name, '_Id')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'Id'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:for-each select="adl:property[not(@type='entity')]">
<key-property>
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
<xsl:attribute name="type">
<xsl:call-template name="csharp-type">
<xsl:with-param name="property" select="."/>
</xsl:call-template>
</xsl:attribute>
<xsl:apply-templates select="adl:documentation"/>
</key-property>
</xsl:for-each>
<xsl:for-each select="adl:property[@type='entity']">
<key-many-to-one>
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
<xsl:attribute name="class">
<xsl:value-of select="@entity"/>
</xsl:attribute>
<xsl:choose>
<xsl:when test="@cascade='manual'"/>
<xsl:when test="@cascade">
<xsl:attribute name="cascade">
<xsl:value-of select="@cascade"/>
</xsl:attribute>
</xsl:when>
</xsl:choose>
<xsl:apply-templates select="adl:documentation"/>
</key-many-to-one>
</xsl:for-each>
</composite-id>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="adl:generator">
<hbn:generator>
<generator>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="@action='manual'">
@ -122,7 +172,7 @@
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</hbn:generator>
</generator>
</xsl:template>
<xsl:template match="adl:property[@concrete='false']">
@ -132,8 +182,7 @@
<xsl:template match="adl:property[@type='entity']">
<!-- a property of type entity translates to a Hibernate many-to-one -->
<hbn:many-to-one>
<xsl:apply-templates select="adl:documentation"/>
<many-to-one>
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
@ -148,7 +197,8 @@
</xsl:attribute>
</xsl:when>
</xsl:choose>
</hbn:many-to-one>
<xsl:apply-templates select="adl:documentation"/>
</many-to-one>
</xsl:template>
<xsl:template match="adl:property[@type='list']">
@ -164,8 +214,7 @@
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<hbn:set>
<xsl:apply-templates select="adl:documentation"/>
<set>
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
@ -176,18 +225,18 @@
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:apply-templates select="adl:documentation"/>
<!-- careful with reformatting here:
'The element cannot contain white space. Content model is empty.' -->
<hbn:key><xsl:attribute name="column">
<key><xsl:attribute name="column">
<!-- this is the name of the farside foreign key field which points to me -->
<xsl:value-of select="$farkey"/>
</xsl:attribute>
</hbn:key>
<hbn:one-to-many>
</xsl:attribute></key>
<one-to-many>
<xsl:attribute name="class">
<xsl:value-of select="@entity"/>
</xsl:attribute>
</hbn:one-to-many>
</one-to-many>
<xsl:choose>
<xsl:when test="@cascade='manual'"/>
<xsl:when test="@cascade">
@ -196,7 +245,7 @@
</xsl:attribute>
</xsl:when>
</xsl:choose>
</hbn:set>
</set>
</xsl:template>
<xsl:template match="adl:property[@type='link']">
@ -218,7 +267,7 @@
</xsl:choose>
</xsl:variable>
<hbn:set>
<set>
<xsl:apply-templates select="adl:documentation"/>
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
@ -226,12 +275,12 @@
<xsl:attribute name="table">
<xsl:value-of select="$tablename"/>
</xsl:attribute>
<hbn:key>
<key>
<xsl:attribute name="column">
<xsl:value-of select="concat( ../@name, 'Id')"/>
</xsl:attribute>
</hbn:key>
<hbn:many-to-many>
</key>
<many-to-many>
<xsl:attribute name="column">
<xsl:choose>
<xsl:when test="../@name = @entity">
@ -245,16 +294,15 @@
<xsl:attribute name="class">
<xsl:value-of select="@entity"/>
</xsl:attribute>
</hbn:many-to-many>
</hbn:set>
</many-to-many>
</set>
</xsl:template>
<xsl:template match="adl:property">
<!-- tricky, this, because we're translating between ADL properties and
Hibernate properties, which are (slightly) different. There's potential
for confusion -->
<hbn:property>
<xsl:apply-templates select="adl:documentation"/>
<property>
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
@ -263,10 +311,11 @@
<xsl:with-param name="property" select="."/>
</xsl:call-template>
</xsl:attribute>
</hbn:property>
<xsl:apply-templates select="adl:documentation"/>
</property>
</xsl:template>
<xsl:template name="adl:documentation">
<xsl:template match="adl:documentation">
<xsl:comment>
<xsl:apply-templates/>
</xsl:comment>