2009-07-22 50 views
2

我需要將兩個不同的類映射到同一個視圖,並收到存在重複映射的錯誤。這在NHibernate中可能嗎?如果不是的話,任何人都可以指導我如何解決這個映射問題。NHibernate:將多個類映射到相同的視圖

我與設置在石頭上的意見工作。一個視圖帶回需要分成兩個類的數據。該視圖的樣子:

vw_player_points 
---------------- 
Id 
GameID 
PlayerID 
Points 

的類必須是「球員」,與遊戲列表發揮

select gameid from vw_player_points where playerid = <PlayerID> 

而且每個「遊戲」,需要的玩家和他們點的列表:

select playerid, points from vw_player_points where gameid = <GameID> 

我試過表每個具體類繼承藏漢作爲映射了同樣的觀點兩次,但都沒有喜悅:(

以下是放在一個XML片段中的「粗略」映射。請注意,我還需要映射到

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DomainModel" namespace="Test"> 

    <class name="IPlayer" abstract="true"> 

    <id name="Id" column="id"> 
     <generator class="assigned"/> 
    </id> 

    <union-subclass name="Player" table="vw_player"> 
     <bag name="Games"> 
      <key column="player_id"/> 
      <one-to-many class="Test.IGame" not-found="ignore"/> 
     </bag> 
    </union-subclass> 

    </class> 

    <class name="IGame" abstract="true"> 

    <id name="Id" column="game_id"> 
     <generator class="assigned"/> 
    </id> 

    <union-subclass name="Game" table="vw_player_points"> 
     <bag name="Points"> 
      <key column="game_id"/> 
      <one-to-many class="Test.IPlayerPoints" not-found="ignore"/> 
     </bag> 
    </union-subclass> 

    </class> 

    <class name="IPlayerPoints" abstract="true"> 

    <id name="Id" column="id"> 
     <generator class="assigned"/> 
    </id> 

    <union-subclass name="PlayerPoints" table="vw_player_points"> 
     <property not-null="false" name="PlayerId" column="player_id"/> 
     <property not-null="false" name="Points" column="points"/> 
    </union-subclass> 

    </class> 

</hibernate-mapping> 
+0

你可以發佈你的每個類的映射嗎? – 2009-07-22 13:34:28

+0

映射張貼:) – theGecko 2009-07-22 15:45:06

回答

1

它似乎是不可能的多個類映射到單個視圖中使用的形式的抽象/子類的映射時的接口(工作):

<class name="I[Entity]" abstract="true"> 

    <union-subclass name="[Entity]"> 
     ... 
    </union-subclass> 

</class> 

最後我直接映射到具體類使用proprty-ref屬性,其正常工作:

<class name="Game" table="vw_player_points"> 

    <id name="Id" column="id"> 
     <generator class="hilo"/> 
    </id> 

    <property not-null="false" name="GameId" column="gameid"/> 

    <bag name="Points"> 
     <key column="gameid" property-ref="GameId"/> 
     <one-to-many class="PlayerPoints" not-found="ignore"/> 
    </bag> 

</class> 

<class name="PlayerPoints" table="vw_player_points"> 

    <id name="Id" column="id"> 
     <generator class="hilo"/> 
    </id> 

    <property not-null="false" name="PlayerId" column="playerid"/> 
    <property not-null="false" name="Points" column="points"/> 

</class> 

也許一個「抽象」子映射類型將否定利用工會子劈米應用於接口。