mapping by code nhibernate 3.2
Bonjour,
Ma femme et moi nous nous lançons dans NHibernate 3.2.
Notre soucis est que nous n'arrivons pas à obtenir le résultat d'un queryover.
(On fait du mapping)
Je vous donne le code car nous avons du mal à définir pourquoi il ne cale pas la session comme il se doit.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
class Program
{
static void Main(string[] args)
{
try
{
Program programtest = new Program();
// programtest.InitializeDbConnection();
//NhibernateHelper.OpenSession();
NHibernate.ISession _session = NhibernateHelper.OpenSession();
PersonneQuery _lecture = new PersonneQuery(_session);
}
catch(Exception ex)
{
Console.WriteLine(string.Format("{0} {1}",ex.Message, ex.StackTrace));
}
}
}
public class NhibernateHelper
{
private static ISessionFactory _sessionFactory;
public static ISessionFactory SessionFactory
{
get
{
/*if (_sessionFactory == null)
{
InitializeSessionFactory();
}*/
return InitializeSessionFactory();
}
}
private static ISessionFactory InitializeSessionFactory()
{
// ModelMapper : modèle de mapping qui peut éventuellement contenir des règles génériques (tag <hibernate-mapping>).
var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
HbmMapping domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
// Config accès DB en elle-même.
var configuration = new Configuration();
configuration.DataBaseIntegration(c =>
{
c.Dialect<MsSql2008Dialect>();
c.ConnectionString = @"Data Source=192.168.1.201;Initial Catalog=testing;Integrated Security=false;User ID=toto;Password=totoenslip;Pooling=False";
c.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
//permet d'afficher le sql généré par nhibernate
c.LogFormattedSql = true;
c.LogSqlInConsole = true;
c.AutoCommentSql = true;
});
// Session d'accès à la DB : ready to use.
_sessionFactory = configuration.BuildSessionFactory();
return _sessionFactory;
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
public class Personne
{
public virtual int Id { get; set; }
public virtual string Nom { get; set; }
public virtual string Prenom { get; set; }
}
public class PersonneMap<T> : ClassMapping<T> where T : Personne
{
public PersonneMap()
{
Id(x => x.Id);
Property(x => x.Nom);
Property(x => x.Prenom, map => map.Type(NHibernateUtil.String));
Table("test_nhibernate");
}
}
public class PersonneQuery
{
public PersonneQuery(NHibernate.ISession session_)
{
Console.WriteLine("lisons la DB maintenant");
IList<Personne> _listresult = session_
.QueryOver<Personne>()
.Where(p => p.Nom =="toto")
.List();
Console.WriteLine("{0}", _listresult.Count);
foreach (var personne in _listresult)
{
Console.WriteLine("Prenom:{0}", personne.Prenom);
}
}
public static void ListPersonnes(ISession session_)
{
IList<Personne> lstPersonne = session_
.QueryOver<Personne>()
.List();
Console.WriteLine("{0}", lstPersonne.Count);
foreach (var personne in lstPersonne)
{
Console.WriteLine("Prenom:{0}", personne.Prenom);
}
}
} |
Edit: je vous met le repository gti: http://babi.homelinux.net/gitweb/?p=....git;a=summary
Merci.