Stop thinking relational!

If you think in joins, you don't need AR or NH at all.

Your model should be instead:
class Board:
  int Id
  string Name
  int Size

class Game:
  int Id
  *Board* Board // not int BoardId, we have objects here, not relations!
  Player Player // same as before, player should be an entity
  DateTime SolvedTimestamp

Now, you should create a Game only when a Game has been solved by a player.

So when you want to display all games solved by a player, you can look up
all the Game entities created for a given player. Using HQL as in
http://nhforge.org/doc/nh/en/index.html#queryhql you'll get:
from Game
join fetch Board
where Player.id = ?

The join fetch board is called eager loading and used to prevent AR from
loading each Board entity in a single DB call (N+1 behaviour).

If you want to query the non-solved boards the query is a bit more
complicated:
select board
from
  Board board,
  Game game
where
  game.Player.id = ? and
  board not in elements(game.Board)

If you have a query that you can't solve with HQL, Linq or Criteria
expressions, you can always create a view in the DB and map it to a view
model class with AR. However, AR can't create the DB schema for this class.

-Markus

2010/5/12 Zvi Balas <[email protected]>

> Thanks,
> that's helped me to do one of the things i need.
> but i now understand that i need more complex join.
>
>  suppose we have two table:
> Board:
>    int ID
>    string Name
>    int BoardSize
>
> game:
>   int ID
>   int BoardID
>   string PlayerName
>   string solveDate
>
> now, i want to join those two tables by the BoardID, and to display in
> datagrid to the user only the games he solved,
> and all the boards that he didn't solved yes.
>
> so i need a join that give me all the record from table Board with the
> records from table game where PlayerName = username.
>
> can you assist me with this also?
>
> Thanks a lot
> Zvi
>
>
>
> On Wed, May 12, 2010 at 2:52 AM, Belvasis <[email protected]>wrote:
>
>> You could use something like:
>>
>> [ActiveRecord("Game")]
>> class Game : ActiveRecordBase
>> {
>>    [BelongsTo("BoardID")]
>>    public Board OwnerBoard
>>    {
>>        get;
>>        set;
>>    }
>> }
>>
>> [ActiveRecord("Board")]
>> class Board : ActiveRecordBase
>> {
>> }
>>
>> Using one of the Find*(typeof(Game)) Methods you get an array of Games
>> with the correct OwnerBoard.
>> In this case you can simply bind Game.solveDate or Game.OwnerBoard.ID to
>> the datagrid, assuming
>> that the OwnerBoard never has a null value.
>>
>> For viewing purposes you can also simply build a view with the necessary
>> properties and map it to a
>> new class:
>>
>> [ActiveRecord("View_Game_Board")]
>> class ViewGame : ActiveRecordBase
>> {
>> }
>>
>> or simply use ADO.NET to show the data and for editing purposes or
>> something like that load the entity
>> via FindByPrimaryKey...Board.FindByPrimaryKey(BoardID).
>>
>>
>> 2010/5/11 Zvi Balas <[email protected]>
>>
>>>  Thanks,
>>> but this is not exactly what i'm looking for.
>>>
>>> suppose we have two table:
>>> Board:
>>>    int ID
>>>    string Name
>>>    int BoardSize
>>>
>>> game:
>>>   int ID
>>>   int BoardID
>>>   string solveDate
>>>
>>> BoardID is ID of table Board.
>>> i have many entries in game that link to the same Board.
>>>
>>> i want to make a join that will return me a list with the following
>>> fields:
>>> gameID, BoardID, Name, BoardSize, SolveDate
>>>
>>> i'm planning to take this list and put it in a wpf datagrid fot the user
>>> to see.
>>>
>>> Can you assist me with this join operation?
>>>
>>> Thanks,
>>> Zvi
>>>
>>>
>>> On Tue, May 11, 2010 at 2:44 AM, Belvasis <[email protected]>wrote:
>>>
>>>> Since we are talking about OR/M, a join between two tables usally
>>>> represents a collection property
>>>> of an root entity. So may an Order Entity have a collection of
>>>> OrderItems Entities, the RDMS specifics
>>>> are in the mapping of those classes.
>>>>
>>>> [ActiveRecord("TBL_ORDER")]
>>>> class Order
>>>> {
>>>>   [HasMany(typeof(OrderItem), Table="TBL_ORDERITEM",
>>>> ColumnKey="ORDER_ID")]
>>>>   public IList OrderItems
>>>> ..
>>>> }
>>>>
>>>> So the list of OrderItems can simply retrieved using
>>>>
>>>> IList pItems =  pOrderEntity.OrderItems;
>>>>
>>>> Beside this there are several ways for querys:
>>>> - FindbyPrimaryKey
>>>> - FindAll
>>>> - FindFirst
>>>> - SimplyQuery
>>>> - ScalarQuery
>>>> - ICriteria
>>>> etc.
>>>>
>>>> I don't know if this answers your question, since all of this is good
>>>> described in the documentation.
>>>>
>>>> Regards
>>>>
>>>> 2010/5/10 Zvi Balas <[email protected]>
>>>>
>>>> Hi all,
>>>>> i'm new to AR,
>>>>>
>>>>> I created two table with relation of HasMany between them
>>>>> Now, i want to make a simple fetch that return me s list collection of
>>>>> a join operation between the tables.
>>>>>
>>>>> Can anyone assist me with it?
>>>>>
>>>>> i tried to find help in the documentation/google with no luck.
>>>>> please help me with examples or any other references.
>>>>>
>>>>> Thanks
>>>>> Zvi
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Castle Project Users" group.
>>>>> To post to this group, send email to
>>>>> [email protected].
>>>>> To unsubscribe from this group, send email to
>>>>> [email protected]<castle-project-users%[email protected]>
>>>>> .
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/castle-project-users?hl=en.
>>>>>
>>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Castle Project Users" group.
>>>> To post to this group, send email to
>>>> [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected]<castle-project-users%[email protected]>
>>>> .
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/castle-project-users?hl=en.
>>>>
>>>
>>>  --
>>> You received this message because you are subscribed to the Google Groups
>>> "Castle Project Users" group.
>>> To post to this group, send email to
>>> [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected]<castle-project-users%[email protected]>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/castle-project-users?hl=en.
>>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Castle Project Users" group.
>> To post to this group, send email to
>> [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<castle-project-users%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/castle-project-users?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Castle Project Users" group.
> To post to this group, send email to [email protected]
> .
> To unsubscribe from this group, send email to
> [email protected]<castle-project-users%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/castle-project-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.

Reply via email to