<?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 default-lazy="false" default-cascade="save-update">
    <class name="gov.va.med.tfs.tfdm.TemplateMetaData" table="TEMPLATE_SCHEMA" lazy="false">
        <id type="java.lang.Long" >
            <column name="TEMPLATE_SCHEMA_ID" />
 			<generator class="sequence">
				<param name="sequence">template_schema_seq_id</param>
			</generator>
        </id>



		<property name="templateName" column="TEMPLATE_NAME" type="java.lang.String" />
		<property name="templateId" column="TEMPLATE_ID" type="java.lang.String" />
		<property name="templateDescription" column="TEMPLATE_DESCRIPTION" type="java.lang.String" />
		<property name="testOnly" column="TEST_ONLY" type="true_false" />

		<property name="operation" column="REQUEST_TYPE" type="java.lang.String" />
<!-- 
		<property name="operation">
			<column name="REQUEST_TYPE"></column>
			<type name="org.hibernate.type.EnumType">
				<param name="enumClass">gov.va.med.tfs.tfdm.Operation</param>
				<param name="type">12</param>
			</type>
		</property>
 -->
 
		<property name="dateAdded" column="DATE_ADDED" type="java.util.Date" />
		<property name="dateActivated" column="DATE_ACTIVATED" type="java.util.Date" />
		<property name="dateDeactivated" column="DATE_DEACTIVATED" type="java.util.Date" />
		<property name="dateDeprecated" column="DATE_DEPRECATED" type="java.util.Date" />
		<property name="dateUpdated" column="DATE_UPDATED" type="java.util.Date" />
		
		<property name="status" column="STATUS" type="java.lang.String" />
<!-- 
		<property name="status">
			<column name="STATUS"></column>
			<type name="org.hibernate.type.EnumType">
				<param name="enumClass">gov.va.med.tfs.tfdm.TfsStatus</param>
				<param name="type">12</param>
			</type>
		</property>
 -->
		
		<property name="blob" column="TEMPLATE_SCHEMA_BLOB" type="java.sql.Blob" />


		<set name="domainEntryPoints" table="TEMPLATE_SCHEMA_ENTRY_POINT"  >
			<key column="TEMPLATE_SCHEMA_ID" not-null="true"/>
			<many-to-many column="DOMAIN_ENTRY_POINT_ID" class="gov.va.med.tfs.tfdm.DomainEntryPoint" />
        </set>

	    <set name="vhimVersionWrappers" table="TEMPLATE_SCHEMA_VHIM_VERSION">
	        <key column="TEMPLATE_SCHEMA_ID" not-null="true"/>
	        <many-to-many column="VHIM_VERSION_ID" unique="true"
	            class="gov.va.med.tfs.tfdm.VhimVersionWrapper"/>
	    </set>

  <!--   
      
		<set name="vhimVersions" table="TEMPLATE_SCHEMA_VHIM_VERSION"  >
			<key column="TEMPLATE_SCHEMA_ID" not-null="true" update="false" />
			<element type="string" column="VHIM_VERSION_TXT"/>
        </set>
     -->    
<!--  for enum types  -->
<!-- 
	    <set name="vhimVersions" table="TEMPLATE_SCHEMA_VHIM_VERSION">
	        <key column="TEMPLATE_SCHEMA_ID"/>
	        <many-to-many column="VHIM_VERSION_ID" 
	            class="gov.va.med.tfs.tfdm.VhimVersionWrapper"/>
	    </set>
	
	    <set name="domainEntryPoints" table="TEMPLATE_SCHEMA_ENTRY_POINT">
	        <key column="TEMPLATE_SCHEMA_ID"/>
	        <many-to-many column="DOMAIN_ENTRY_POINT_ID" 
	            class="gov.va.med.tfs.tfdm.DomainEntryPointWrapper"/>
	    </set>
 -->
    </class>

<!-- 
		where tmd.status = TfsStatus.ACTIVE
		where tmd.vhimVersions.contains( :vhimVersion )
		
			
select template_Id from template_schema ts, vhim_version vv, template_schema_vhim_Version tsvv
where TS.TEMPLATE_SCHEMA_ID = TSVV.TEMPLATE_SCHEMA_ID
and VV.VHIM_VERSION_ID = TSVV.VHIM_VERSION_ID
and VV.VHIM_VERSION_TXT ='Vhim_4_00';
		
		and tmd.TEMPLATE_SCHEMA_ID = TSVV.TEMPLATE_SCHEMA_ID
		and TSVV.VHIM_VERSION_ID = VV.VHIM_VERSION_ID
		and VV.VHIM_VERSION_TXT = :vhimVersion


		and elements(tmd.vhimVersions) in ( :vhimVersion )


		and vv.version = :vhimVersion
		and tmd.vhimVersions['Vhim_4_00'] = vv.version
		
		= some elements(tmd.vhimVersions).version
		
		
		select tmd.templateId from TemplateMetaData tmd
		where tmd.status = :status
		
		and :vhimVersion in  elements(tmd.vhimVersions) 
		
 -->

    <query name="TemplateSchemaId">
		<![CDATA[
		from TemplateMetaData tmd
		where tmd.templateId = :templateId
      ]]>
	</query>
	
    <query name="TemplateSchemaIdByStatus">
		<![CDATA[
		from TemplateMetaData tmd
		where tmd.templateId = :templateId
		and tmd.status = :status
      ]]>
	</query>
	
	<sql-query name="TemplateSchemaIdsByStatus">
		SELECT template_Id
  		FROM template_schema ts, vhim_version vv, template_schema_vhim_Version tsvv
 		WHERE TS.TEMPLATE_SCHEMA_ID = TSVV.TEMPLATE_SCHEMA_ID
       	AND VV.VHIM_VERSION_ID = TSVV.VHIM_VERSION_ID
        AND VV.VHIM_VERSION_TXT = :vhimVersion
        AND TS.STATUS = :status
        ORDER BY TS.TEMPLATE_ID ASC
	</sql-query>
	

<!-- 
select mother from Eg.Cat as mother, Eg.Cat as kit
where kit in elements(mother.Kittens)

select p from Eg.NameList list, Eg.Person p
where p.Name = some elements(list.Names)

from Eg.Cat cat where exists elements(cat.Kittens)

from Eg.Player p where 3 > all elements(p.Scores)

from Eg.Show show where 'fizard' in indices(show.Acts)
Note that these constructs - size, elements, indices, minIndex, maxIndex, minElement, maxElement - have certain usage restrictions: 

in a where clause: only for databases with subselects 

in a select clause: only elements and indices make sense 

Elements of indexed collections (arrays, lists, maps) may be referred to by index (in a where clause only): 

from Order order where order.Items[0].id = 1234

select person from Person person, Calendar calendar
where calendar.Holidays['national day'] = person.BirthDay
    and person.Nationality.Calendar = calendar
=======
select cust
from Product prod,
    Store store
    inner join store.Customers cust
where prod.Name = 'widget'
    and store.Location.Name in ( 'Melbourne', 'Sydney' )
    and prod = all elements(cust.CurrentOrder.LineItems)
 -->


    <query name="AllTemplateSchemaIds">
		<![CDATA[
		select templateId from TemplateMetaData
      ]]>
	</query>

	<sql-query name="AllVhimVersions">
		<![CDATA[
		select vhim_version_txt from Vhim_Version
      ]]>
	</sql-query>

</hibernate-mapping>
