Is it possible to bind a scalar query result to a property in nhibernate?
I have an entity which has a property that does not get its value from the entity's table. But it could be calculated by a query if it is possible.
To clarify, I have an entity of an Entry and I want to check if the current user voted it or not. So for that reason I thought that I could add a property like "IsCurrentUserVoted" to the Entry entity. And if it is possible to bind its value from a query like "s开发者_StackOverflowelect count(*) from vote where userId = :currentUser
".
I wonder if nhibernate supports this kind of a feature or it is the only way to join the vote and entry tables? If it does not, what would your solution be in that case.
Thanks in advance.
Yes, I think you can do that with the formula property...
<property name="IsCurrentUserVoted" formula="select count(*) from vote where userId=Id" />
I'm not positive on the syntax, you might need to use a scalar function to wrap the query. Also, it might be best to run that query only when needed, unless you really need that value every time you get an Entry.
精彩评论