Title: NHibernate: Mapping multiple classes to the same view
Tags: nhibernate;nhibernate-mapping
Question: I need to map two different classes to the same view and receive an error that a duplicate mapping exists. Is this possible in NHibernate? If not, can anyone give direction as to how I can solve this mapping problem.
I work with views which are set in stone. One view brings back data needed to split into two classes. The view looks like:
vw_player_points
----------------
GameID
PlayerID
Points
The classes need to be 'player', with a list of games played
select gameid from vw_player_points where playerid = <PlayerID&gt;
And each 'game' needs a list of players and their points:
select playerid, points from vw_player_points where gameid = <GameID&gt;
I've tried table-per-concrete class inheritance aswell as mapping to the same view twice, but have had no joy :(
Here's the 'rough' mappings put into one xml snippet. Notice I also need to map to an interface (which works)
<?xml version="1.0" encoding="utf-8" ?&gt;
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DomainModel" namespace="Test"&gt;
<class name="IPlayer" abstract="true"&gt;
<id name="Id" column="id"&gt;
<generator class="assigned"/&gt;
</id&gt;
<union-subclass name="Player" table="vw_player"&gt;
<bag name="Games"&gt;
<key column="player_id"/&gt;
<one-to-many class="Test.IGame" not-found="ignore"/&gt;
</bag&gt;
</union-subclass&gt;
</class&gt;
<class name="IGame" abstract="true"&gt;
<id name="Id" column="game_id"&gt;
<generator class="assigned"/&gt;
</id&gt;
<union-subclass name="Game" table="vw_player_points"&gt;
<bag name="Points"&gt;
<key column="game_id"/&gt;
<one-to-many class="Test.IPlayerPoints" not-found="ignore"/&gt;
</bag&gt;
</union-subclass&gt;
</class&gt;
<class name="IPlayerPoints" abstract="true"&gt;
<id name="Id" column="id"&gt;
<generator class="assigned"/&gt;
</id&gt;
<union-subclass name="PlayerPoints" table="vw_player_points"&gt;
<property not-null="false" name="PlayerId" column="player_id"/&gt;
<property not-null="false" name="Points" column="points"/&gt;
</union-subclass&gt;
</class&gt;
</hibernate-mapping&gt;

Here is the accepted answer: It seems impossible to map multiple classes to a single view when using abstract/sub-class mapping of the form:
<class name="I[Entity]" abstract="true"&gt;
<union-subclass name="[Entity]"&gt;
...
</union-subclass&gt;
</class&gt;
I ended up mapping directly to the concrete classes using the proprty-ref attribute which works correctly:
<class name="Game" table="vw_player_points"&gt;
<id name="Id" column="id"&gt;
<generator class="hilo"/&gt;
</id&gt;
<property not-null="false" name="GameId" column="gameid"/&gt;
<bag name="Points"&gt;
<key column="gameid" property-ref="GameId"/&gt;
<one-to-many class="PlayerPoints" not-found="ignore"/&gt;
</bag&gt;
</class&gt;
<class name="PlayerPoints" table="vw_player_points"&gt;
<id name="Id" column="id"&gt;
<generator class="hilo"/&gt;
</id&gt;
<property not-null="false" name="PlayerId" column="playerid"/&gt;
<property not-null="false" name="Points" column="points"/&gt;
</class&gt;
Perhaps an 'abstract' sub-mapping type would negate the use of the union-subclass hack mapping to interfaces.
Comment for this answer: Sorry, but shouldn't it be  in the bag?


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2009:00:07/22/09', 'FavoriteCount': 2.0, 'PostTypeId': 1, 'Tags': '<nhibernate><nhibernate-mapping>', 'Title': 'NHibernate: Mapping multiple classes to the same view', 'ViewCount': 4279, 'id': 1165255, 'user_AboutMe': 'Microsoft and Sun certified developer with ten years industry experience in software development.\r\nSpecialist in application architecture and user interface design.', 'user_AccountId': 17435.0, 'user_DisplayName': 'theGecko', 'user_Reputation': 983.0, 'user_Views': 513.0, 'user_id': 39022.0}}