Due to the fact that the error already indicates that NHibernate does not support this function. I would create a named query and resolve the equation in the query. I tested it using MySQL (using the example of sharing the username and password as an example), and the following statement returns the desired string (password is the BINARY field (32)):
SELECT * FROM `user` WHERE `password` = MD5('test');
Using MSSQL, you can:
SELECT * FROM [user] WHERE [password] = HASHBYTES('MD5', 'test')
, , .hbm.xml, "User.hbm.xml", :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="My.Model" namespace="My.Model">
<sql-query name="GetUserByCredentials">
<return class="My.Model.User, My.Model" />
<![CDATA[
SELECT * FROM User WHERE Username = :Username AND Password = MD5(:Password)
]]>
</sql-query>
</hibernate-mapping>
, Fluent NHibernate, - NHibernate:
Fluently.Configure()
.Database(MySqlConfiguration.Standard
.ConnectionString(x => x.FromConnectionStringWithKey("Test"))
.AdoNetBatchSize(50))
.Cache(c => c
.UseQueryCache()
.ProviderClass<HashtableCacheProvider>())
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<IHaveFluentNHibernateMappings>().Conventions.Add(ForeignKey.EndsWith("Id"));
m.HbmMappings.AddFromAssemblyOf<IHaveFluentNHibernateMappings>();
})
.BuildConfiguration();
".hbm.xml" "IHaveFluentNHibernateMappings"
:
public User GetUserByCredentials(string username, string password)
{
IQuery query = Session.GetNamedQuery("GetUserByCredentials");
query.SetParameter("Username", username);
query.SetParameter("Password", password);
return query.UniqueResult<User>();
}
GetUserByCredentials, .
, - , MD5 :
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in md5ByteArray)
{
s.Append(b.ToString("x2").ToLower());
}
password = s.ToString();
!