Thank you awesome NHibernate developers.
P.S. Source code
01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.Reflection;
04.
05.
using
NHibernate;
06.
using
NHibernate.Cfg;
07.
using
NH3Tests.SimpleModel;
08.
using
NHibernate.Criterion;
09.
10.
namespace
NH3Tests
11.
{
12.
public
class
Program
13.
{
14.
static
ISessionFactory factory;
15.
16.
public
static
void
Main(
string
[] args)
17.
{
18.
log4net.Config.XmlConfigurator.Configure();
19.
20.
IList rooms =
null
;
21.
using
(ISession session = OpenSession())
22.
{
23.
ICriteria query = session.CreateCriterial;()
24.
.Add(Expression.In(
25.
Projections.SqlFunction(
"substring"
,
26.
NHibernateUtil.String,
27.
Projections.Property(
"Code"
),
28.
Projections.Constant(1),
29.
Projections.Constant(2)),
30.
new
string
[] {
"A1"
,
"A2"
}));
31.
32.
rooms = query.List();
33.
}
34.
35.
Console.WriteLine(
"Done."
);
36.
}
37.
38.
public
static
ISession OpenSession()
39.
{
40.
if
(factory ==
null
)
41.
{
42.
Configuration c =
new
Configuration();
43.
c.AddAssembly(Assembly.GetCallingAssembly());
44.
factory = c.BuildSessionFactory();
45.
}
46.
return
factory.OpenSession();
47.
}
48.
}
49.
}
POCO and mapping file:
01.
using
System;
02.
using
System.Collections.Generic;
03.
04.
namespace
NH3Tests.SimpleModel
05.
{
06.
public
class
Room
07.
{
08.
private
int
_id = 0;
09.
private
string
_code =
null
;
10.
private
string
_description =
null
;
11.
12.
public
Room()
13.
{
14.
}
15.
16.
public
int
Id
17.
{
18.
get
{
return
_id; }
19.
set
{ _id = value; }
20.
}
21.
22.
public
string
Code
23.
{
24.
get
{
return
_code; }
25.
set
{ _code = value; }
26.
}
27.
28.
public
string
Description
29.
{
30.
get
{
return
_description; }
31.
set
{ _description = value; }
32.
}
33.
}
34.
}
01.
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<
hibernate-mapping
xmlns
=
"urn:nhibernate-mapping-2.2"
auto-import
=
"true"
>
03.
<
class
name
=
"NH3Tests.SimpleModel.Room, NH3Tests"
table
=
"room"
lazy
=
"false"
>
04.
<
id
name
=
"Id"
access
=
"field.camelcase-underscore"
column
=
"room_id"
>
05.
<
generator
class
=
"native"
/>
06.
id
>
07.
<
property
name
=
"Code"
access
=
"field.camelcase-underscore"
column
=
"code"
/>
08.
<
property
name
=
"Description"
access
=
"field.camelcase-underscore"
column
=
"description"
/>
09.
class
>
10.
hibernate-mapping
>
And config:
01.
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<
configuration
>
03.
<
configSections
>
04.
<
section
name
=
"hibernate-configuration"
05.
type
=
"NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
/>
06.
<
section
name
=
"log4net"
07.
type
=
"log4net.Config.Log4NetConfigurationSectionHandler,log4net"
/>
08.
configSections
>
09.
<
hibernate-configuration
xmlns
=
"urn:nhibernate-configuration-2.2"
>
10.
<
session-factory
>
11.
<
property
name
=
"connection.provider"
>
12.
NHibernate.Connection.DriverConnectionProvider
13.
property
>
14.
<
property
name
=
"connection.driver_class"
>
15.
NHibernate.Driver.SqlClientDriver
16.
property
>
17.
<
property
name
=
"connection.connection_string"
>
18.
Server=(local);database=NH3Tests;Integrated Security=SSPI;
19.
property
>
20.
<
property
name
=
"dialect"
>
21.
NHibernate.Dialect.MsSql2008Dialect
22.
property
>
23.
<
property
name
=
"show_sql"
>trueproperty
>
24.
<
property
name
=
'proxyfactory.factory_class'
>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFuproperty
>
25.
session-factory
>
26.
hibernate-configuration
>
27.
<
log4net
>
28.
<
appender
name
=
"ConsoleAppender"
29.
type
=
"log4net.Appender.ConsoleAppender, log4net"
>
30.
<
layout
type
=
"log4net.Layout.PatternLayout, log4net"
>
31.
<
param
name
=
"ConversionPattern"
value
=
"%m\n"
/>
32.
layout
>
33.
appender
>
34.
<
root
>
35.
<
priority
value
=
"INFO"
/>
36.
<
appender-ref
ref
=
"ConsoleAppender"
/>
37.
root
>
38.
log4net
>
39.
configuration
>
And room table:
01.
CREATE
TABLE
[dbo].[room](
02.
[room_id] [
int
] IDENTITY(1,1)
NOT
NULL
,
03.
[code] [nvarchar](16)
NOT
NULL
,
04.
[description] [nvarchar](256)
NULL
,
05.
CONSTRAINT
[PK_room]
PRIMARY
KEY
CLUSTERED
06.
(
07.
[room_id]
ASC
08.
)
WITH
(PAD_INDEX =
OFF
, STATISTICS_NORECOMPUTE =
OFF
, IGNORE_DUP_KEY =
OFF
, ALLOW_ROW_LOCKS =
ON
, ALLOW_PAGE_LOCKS =
ON
)
ON
[
PRIMARY
]
09.
)
ON
[
PRIMARY
]
1 comment:
Some more fun with Nhibernate. learned it recently its simply awesome
http://d4dilip.wordpress.com/2012/05/13/mapping-enum-in-nhibernate/
Post a Comment