<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE hibernate-mapping PUBLIC
    '-//Hibernate/Hibernate Mapping DTD 3.0//EN' 
    'http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd'>

<hibernate-mapping>

   <typedef class="org.alfresco.repo.domain.hibernate.QNameUserType" name="QName" />

   <class
         name="org.alfresco.repo.domain.hibernate.NodeImpl"
         proxy="org.alfresco.repo.domain.Node"
         table="alf_node"
         dynamic-update="false"
         dynamic-insert="false"
         select-before-update="false" 
         lazy="true" 
         optimistic-lock="version"  >
      <!-- auto-generated ID -->
      <id name="id" column="id" type="long" >
         <generator class="native" />
      </id>
      <!-- the natural ID of the node -->
      <natural-id mutable="true">
         <!-- forward assoc to store -->
         <many-to-one
               name="store"
               class="org.alfresco.repo.domain.hibernate.StoreImpl"
               not-null="true"
               lazy="proxy"
               optimistic-lock="true"
               fetch="join">
            <column name="protocol" not-null="true" />
            <column name="identifier" not-null="true" />
         </many-to-one>
         <!-- the store-unique identifier -->
         <property name="uuid" column="uuid" type="string" length="36" />
      </natural-id>
      <property name="typeQName" column="type_qname" type="QName" length="255" not-null="true" />
      <!-- forward assoc to access control list (optional) -->
      <many-to-one
            name="accessControlList"
            class="org.alfresco.repo.domain.hibernate.DbAccessControlListImpl"
            column="acl_id"
            lazy="proxy"
            fetch="select"
            unique="false"
            not-null="false"
            cascade="delete" />
      <!-- forward assoc to properties -->
      <map
            name="properties" 
            table="alf_node_properties"
            lazy="true" 
            fetch="select"  
            sort="unsorted"
            inverse="false" 
            optimistic-lock="true"
            cascade="delete" >
         <key column="node_id" not-null="true" />
         <map-key column="qname" type="QName" length="200" />
         <composite-element class="org.alfresco.repo.domain.PropertyValue" >
            <property name="actualType" column="actual_type" type="string" length="15" not-null="true" />
            <property name="multiValued" column="multi_valued" type="boolean" not-null="true" />
            <property name="persistedType" column="persisted_type" type="string" length="15" not-null="true" />
            <property name="booleanValue" column="boolean_value" type="boolean" />
            <property name="longValue" column="long_value" type="long" />
            <property name="floatValue" column="float_value" type="float" />
            <property name="doubleValue" column="double_value" type="double" />
            <property name="stringValue" column="string_value" type="string" length="1024"/>
            <property name="serializableValue" column="serializable_value" type="serializable" length="16384"/>
         </composite-element>
      </map>
      <!-- forward assoc to aspects -->
      <set
            name="aspects"
            table="alf_node_aspects"
            lazy="false"
            fetch="join"
            inverse="false" 
            sort="unsorted"
            optimistic-lock="true"
            cascade="delete" >
         <key column="node_id" not-null="true" />
         <element column="qname" type="QName" length="200"/>
      </set>
      <!-- inverse assoc to parent childassocs -->
      <set
            name="parentAssocs"
            inverse="true" 
            lazy="false"
            fetch="join"
            cascade="none"
            optimistic-lock="true" >
         <key column="child_node_id" />
         <one-to-many class="org.alfresco.repo.domain.hibernate.ChildAssocImpl" />
      </set>
   </class>

   <class
         name="org.alfresco.repo.domain.hibernate.NodeStatusImpl"
         proxy="org.alfresco.repo.domain.NodeStatus"
         table="alf_node_status"
         dynamic-update="false"
         dynamic-insert="false"
         select-before-update="false" 
         lazy="true" 
         optimistic-lock="version"  >
      <!-- composite PK -->
      <composite-id name="key" class="org.alfresco.repo.domain.NodeKey">
         <key-property name="protocol" length="50" />
         <key-property name="identifier" length="100" />
         <key-property name="guid" length="36" />
      </composite-id>
      <!-- forward assoc to node (optional) -->
      <many-to-one
            name="node"
            class="org.alfresco.repo.domain.hibernate.NodeImpl"
            column="node_id"
            lazy="false"
            fetch="join"
            unique="false"
            not-null="false" />
      <property name="changeTxnId" column="change_txn_id" type="string" length="56" not-null="true" index="CHANGE_TXN_ID"/>
   </class>
   
   <class
         name="org.alfresco.repo.domain.hibernate.ChildAssocImpl"
         proxy="org.alfresco.repo.domain.ChildAssoc"
         dynamic-insert="false"
         dynamic-update="false"
         lazy="true"
         optimistic-lock="version"
         table="alf_child_assoc" >
      <!-- auto-generated ID -->
      <id name="id" column="id" type="long" >
         <generator class="native" />
      </id>
      <natural-id mutable="true">
         <!-- forward assoc to parent node -->
         <many-to-one 
               name="parent"
               class="org.alfresco.repo.domain.hibernate.NodeImpl"
               lazy="proxy"
               fetch="select"
               optimistic-lock="true"
               not-null="true"
               unique-key="UIDX_CHILD_NAME" >
            <column name="parent_node_id" />
         </many-to-one>
         <!-- forward assoc to child node -->
         <many-to-one
               name="child" 
               lazy="proxy"
               fetch="select"
               class="org.alfresco.repo.domain.hibernate.NodeImpl"
               optimistic-lock="true"
               not-null="true" >
            <column name="child_node_id" />
         </many-to-one>
         <property name="typeQName" column="type_qname" type="QName" length="255" not-null="true" unique-key="UIDX_CHILD_NAME" />
      </natural-id>
      <property name="childNodeName" column="child_node_name" type="string" length="50" not-null="true" unique-key="UIDX_CHILD_NAME" />
      <property name="childNodeNameCrc" column="child_node_name_crc" type="long" not-null="true" unique-key="UIDX_CHILD_NAME" />
      <property name="qname" column="qname" type="QName" length="255" not-null="true" />
      <property name="isPrimary" column="is_primary" />
      <property name="index" column="assoc_index" />
   </class>

   <class
         name="org.alfresco.repo.domain.hibernate.NodeAssocImpl"
         proxy="org.alfresco.repo.domain.NodeAssoc"
         table="alf_node_assoc" >
      <!-- auto-generated ID -->
      <id name="id" column="id" type="long" >
         <generator class="native" />
      </id>
      <natural-id mutable="true">
         <!-- forward assoc to source node -->
         <many-to-one
               name="source"
               class="org.alfresco.repo.domain.hibernate.NodeImpl"
               lazy="false"
               fetch="join"
               not-null="true" >
            <column name="source_node_id" />
         </many-to-one>
         <!-- forward assoc to target node -->
         <many-to-one
               name="target"
               class="org.alfresco.repo.domain.hibernate.NodeImpl"
               lazy="false"
               fetch="join"
               not-null="true" >
            <column name="target_node_id" />
         </many-to-one>
         <property name="typeQName" column="type_qname" type="QName" length="255" not-null="true" />
      </natural-id>
   </class>
   
   <query name="store.GetAllStores">
      select
         store
      from
         org.alfresco.repo.domain.hibernate.StoreImpl as store
   </query>
   
   <query name="node.updateChildAssocName">
      update
         org.alfresco.repo.domain.hibernate.ChildAssocImpl assoc
      set
         assoc.childNodeName = :newName,
         assoc.childNodeNameCrc = :newNameCrc
      where
         assoc.id = :childAssocId
   </query>
   
   <query name="node.GetChildAssocs">
      select
         assoc
      from
         org.alfresco.repo.domain.hibernate.ChildAssocImpl as assoc
      where
         assoc.parent.id = :parentId
      order by
         assoc.index,
         assoc.id
   </query>
   
   <query name="node.GetChildAssocByTypeAndName">
      select
         assoc
      from
         org.alfresco.repo.domain.hibernate.ChildAssocImpl as assoc
      where
         assoc.parent.id = :parentId and
         assoc.typeQName = :typeQName and
         assoc.childNodeName = :childNodeName and
         assoc.childNodeNameCrc = :childNodeNameCrc
      order by
         assoc.index,
         assoc.id
   </query>
   
   <query name="node.GetChildAssocRefs">
      select
         assoc.typeQName,
         assoc.qname,
         assoc.isPrimary,
         assoc.index,
         child.id,
         child.store.key.protocol,
         child.store.key.identifier,
         child.uuid as parentUuid
      from
         org.alfresco.repo.domain.hibernate.ChildAssocImpl as assoc
         join assoc.parent as parent
         join assoc.child as child
      where
         assoc.parent.id = :parentId
      order by
         assoc.index,
         assoc.id
   </query>
   
   <query name="node.GetNodeAssoc">
      select
         assoc
      from
         org.alfresco.repo.domain.hibernate.NodeAssocImpl as assoc
      where
         assoc.source.id = :sourceId and
         assoc.target.id = :targetId and
         assoc.typeQName = :assocTypeQName
   </query>
   
   <query name="node.GetNodeAssocsToAndFrom">
      select
         assoc
      from
         org.alfresco.repo.domain.hibernate.NodeAssocImpl as assoc
      where
         assoc.source.id = :nodeId or
         assoc.target.id = :nodeId
   </query>

   <query name="node.GetTargetAssocs">
      select
         assoc
      from
         org.alfresco.repo.domain.hibernate.NodeAssocImpl as assoc
         join assoc.source as source
         join assoc.target as target
      where
         assoc.source.id = :sourceId
   </query>
   
   <query name="node.GetSourceAssocs">
      select
         assoc
      from
         org.alfresco.repo.domain.hibernate.NodeAssocImpl as assoc
         join assoc.source as source
         join assoc.target as target
      where
         assoc.target.id = :targetId
   </query>
   
   <query name="node.GetNextChangeTxnIds">
      select distinct
         status.changeTxnId
      from
         org.alfresco.repo.domain.hibernate.NodeStatusImpl as status
      where
         status.changeTxnId > :currentTxnId
      order by
         status.changeTxnId
   </query>

   <query name="node.GetChangedNodeStatusesCount">
      select
         count(status.changeTxnId)
      from
         org.alfresco.repo.domain.hibernate.NodeStatusImpl as status
      where
         status.key.protocol = :storeProtocol and
         status.key.identifier = :storeIdentifier and
         status.node.id is not null and
         status.changeTxnId = :changeTxnId
   </query>

   <query name="node.GetChangedNodeStatuses">
      select
         status
      from
         org.alfresco.repo.domain.hibernate.NodeStatusImpl as status
      where
         status.key.protocol = :storeProtocol and
         status.key.identifier = :storeIdentifier and
         status.node.id is not null and
         status.changeTxnId = :changeTxnId
   </query>

   <query name="node.GetDeletedNodeStatuses">
      select
         status
      from
         org.alfresco.repo.domain.hibernate.NodeStatusImpl as status
      where
         status.key.protocol = :storeProtocol and
         status.key.identifier = :storeIdentifier and
         status.node.id is null and
         status.changeTxnId = :changeTxnId
   </query>

   <query name="node.GetContentDataStrings">
      select distinct
         props.stringValue
      from
         org.alfresco.repo.domain.hibernate.NodeImpl as node
      join
         node.properties props
      where
         props.stringValue like 'contentUrl%'
   </query>

   <query name="node.patch.GetNodesWithPersistedSerializableProperties">
      select distinct
         node
      from
         org.alfresco.repo.domain.hibernate.NodeImpl as node
      where
         node.properties.serializableValue is not null and
         node.properties.multiValued = false
   </query>

   <query name="node.patch.GetAssocsAndChildNames">
      <![CDATA[
      select
         assoc,
         child
      from
         org.alfresco.repo.domain.hibernate.ChildAssocImpl as assoc
         join assoc.child as child
      where
         assoc.id > :lastAssocId and
         assoc.typeQName = :assocTypeQName
      order by
         assoc.id
      ]]>
   </query>

</hibernate-mapping>
