Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Due to the fact that the error is already indicating that NHibernate does not support that feature. I would create a named query and solve the equation within a query. I have tested it with MySQL (using a common username and password comparison as example) and the following statement returns the desired row (password is a BINARY(32) field):</p> <pre><code>SELECT * FROM `user` WHERE `password` = MD5('test'); </code></pre> <p>Using MSSQL you can do:</p> <pre><code>SELECT * FROM [user] WHERE [password] = HASHBYTES('MD5', 'test') </code></pre> <p>So to extend this to a named query you would create an .hbm.xml file like 'User.hbm.xml' with the following content:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="My.Model" namespace="My.Model"&gt; &lt;sql-query name="GetUserByCredentials"&gt; &lt;return class="My.Model.User, My.Model" /&gt; &lt;![CDATA[ SELECT * FROM User WHERE Username = :Username AND Password = MD5(:Password) ]]&gt; &lt;/sql-query&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>To configure this I used Fluent NHibernate but something similar would be possible with just plain NHibernate:</p> <pre><code>Fluently.Configure() .Database(MySqlConfiguration.Standard .ConnectionString(x =&gt; x.FromConnectionStringWithKey("Test")) .AdoNetBatchSize(50)) .Cache(c =&gt; c .UseQueryCache() .ProviderClass&lt;HashtableCacheProvider&gt;()) .Mappings(m =&gt; { m.FluentMappings.AddFromAssemblyOf&lt;IHaveFluentNHibernateMappings&gt;().Conventions.Add(ForeignKey.EndsWith("Id")); m.HbmMappings.AddFromAssemblyOf&lt;IHaveFluentNHibernateMappings&gt;(); }) .BuildConfiguration(); </code></pre> <p>This statement looks for the ".hbm.xml" files in the assembly with the interface named "IHaveFluentNHibernateMappings"</p> <p>With this in place you can do the following at session level:</p> <pre><code>public User GetUserByCredentials(string username, string password) { IQuery query = Session.GetNamedQuery("GetUserByCredentials"); query.SetParameter("Username", username); query.SetParameter("Password", password); return query.UniqueResult&lt;User&gt;(); } </code></pre> <p>And by calling the the GetUserByCredentials method, the custom query will be executed.</p> <p>As you can see password is a string, so you need to convert your MD5 byte array to a string first by using:</p> <pre><code>System.Text.StringBuilder s = new System.Text.StringBuilder(); foreach (byte b in md5ByteArray) { s.Append(b.ToString("x2").ToLower()); } password = s.ToString(); </code></pre> <p>Good luck!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload