NHibernate
- Metodele care testeaza insert-urile crapa cu "NHibernate.AssertionFailure: null identifier"
- La construirea instantei de Configuration ar trebui setat "connection.release_mode" pe "on_close".
- Mai multe detalii aici: http://www.codethinked.com/post/2008/10/19/NHibernate-20-SQLite-and-In-Memory-Databases.aspx
- Daca apar erori ciudate la stergerea/inserarea/actualizarea entitatilor cu un composite key
- Verificat daca entitatile sunt Serializable (ar mai trebui sa implementeze si Equals + GetHashCode).
- http://www.hibernate.org/hib_docs/nhibernate/html/components.html#components-compositeid
- Adaugarea unui element mapat in alta colectie, or something
- If you're an NHibernate veteran then you'll recognise this; however, if it's all new to you then let me explain: in a relationship where both sides are mapped, NHibernate needs you to set both sides before it will save correctly. So as to not have this extra code everywhere, it's been reduced to these extra methods in Store.
- http://wiki.fluentnhibernate.org/show/GettingStarted:+First+Project
- Probleme cu cascade-ul pe bazele de date generate direct din NHibernate
- Set-uri in fara Iesi.Collections
- In mare, putem inlocui ISet<T> cu ICollection<T>, dar va trebui sa deployam Iesi.Collections in continuare (dar nu mai e nevoie sa il referentiem).
- http://ayende.com/Blog/archive/2009/04/23/nhibernate-tidbit-ndash-using-ltsetgt-without-referencing-iesi.collections.aspx
- ISession.Get versus ISession.Load
- Din cate inteleg, Load nu trimite neaparat un query in baza (de ex, daca facem ceva in genul s.Save(new Order {Amount = amount, customer = s.Load<Customer>(1) });
- http://ayende.com/Blog/archive/2009/04/30/nhibernate-ndash-the-difference-between-get-load-and-querying-by.aspx
- Query Cache
- <property name='prepare_sql'>true</property>
- http://ayende.com/Blog/archive/2009/05/02/nhibernate-the-database-query-cache-and-parameter-sizes.aspx
- Cum activez statement batching?
- ?
- Selectarea unei singure coloane (sau a unui subset de coloane) dintr-o entitate folosind Criteria API
- .SetProjection(Projections.Property("<coloana>"));
- .SetProjection(Projections.ProjectionList().Add(Projections.Property("<coloana1>")).Add(Projections.Property("<coloana2>")))
- Daca vreau sa intorc o lista de entitati, avand doar proprietatile specificate completate, atunci folosesc .SetResultTransformer(Transformers.AliasToBean(typeof (<Entitate>))) => ca sa mearga chestia asta, trebuie specificate si alias-urile pentru fiecare proprietate in ProjectionList.
- http://stackoverflow.com/questions/695917/nhibernate-only-retrieve-specific-columns-when-using-critera-queries
- One-to-One
- Fiecare entitate trebuie sa o referentieze pe cealalta (HasOne)
- Legatura se face prin id.
- Ca sa mearga insert-ul cum trebuie, am pus id-ul entitatii "parinte" ca fiind identity, iar id-ul copilului Id(x => x.Id).GeneratedBy.Foreign("Parinte").
- E posibil si sa implementez un generator custom de identitati, dar e urat si poate cauza erori. Si e urat.
- NHibernate.Exceptions.GenericADOException: could not update dupa ce se executa un select in baza
- Cel mai probabil select-ul intoarce niste valori null, care valori nu sunt declarate ca nullable in entitate => sunt completate cu valoarea default => NHibernate detecteaza o schimbare in valori => incearca sa actualizeze entitatea
- http://nhforge.org/blogs/nhibernate/archive/2008/10/20/how-test-your-mappings-the-ghostbuster.aspx
- Caching
- http://nhforge.org/blogs/nhibernate/archive/2009/02/09/quickly-setting-up-and-using-nhibernate-s-second-level-cache.aspx
- http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/11/09/first-and-second-level-caching-in-nhibernate.aspx
- http://ayende.com/Blog/archive/2006/09/29/NHibernateCachingTheSecongLevelCacheSpaceIsShared.aspx
- http://ayende.com/Blog/archive/2009/04/24/nhibernate-2nd-level-cache.aspx
- Maparea unor campuri de tip enum ca avand valori de tip int
- Varianta simpla: Map(x => x.Tip).CustomType<int>()
- Varianta mai complicata: http://stackoverflow.com/questions/439003/how-do-you-map-an-enum-as-an-int-value-with-fluent-nhibernate
- One-To-Many
- Cel mai probabil o sa vreau ceva in genul HasMany(x => x.Children).KeyColumn("IdParent").Cascade.All().Inverse();
- cascade-all ii indica sa salveze/actualizeze/stearga copiii in functie de modificarile asupra parintelui -> http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx
- inverse ii indica sa nu actualizeze campul FK al copilului cu valoarea din parinte; practic, ii spune ca copilul se va ocupa de managementul relatiei - cred; -> http://eashi.wordpress.com/2008/08/22/nhibernate-inverse-attribute/
- Inverse
- Inverse should be set on OneToMany's when the Child has a reference back to the parent (bi-directional). If the child does not have a reference back to the parent, then inverse should be false, signifying that the Parent now needs to manage the foreign key association with the child.
- http://groups.google.com/group/fluent-nhibernate/browse_thread/thread/299ed70f04075c9b/cf1aed8a72cae5a4?#cf1aed8a72cae5a4
- Subqueries pentru a obtine detaliile unei singure entitati
SessionNH .CreateCriteria<Product>("p") .SetMaxResults(rows) .SetFirstResult(startRow) .CreateCriteria("Versions", JoinType.LeftOuterJoin) .Add(Subqueries.PropertyIn("Id", DetachedCriteria .For<ProductVersion>() .SetProjection(Projections.Id()) .Add(Restrictions.EqProperty("Product.Id", "p.Id")) .AddOrder(Order.Desc("DateReleased")) .SetMaxResults(1) )) .List<Product>();
rezulta in
SELECT TOP ( 5 /* @p0 */ ) this_.Id as Id3_2_, this_.Name as Name3_2_, this_.Description as Descript3_3_2_, this_.Logo as Logo3_2_, this_.ProductCategoryId as ProductC5_3_2_, productver1_.ProductId as ProductId4_, productver1_.Id as Id4_, productver1_.Id as Id5_0_, productver1_.Name as Name5_0_, productver1_.Description as Descript3_5_0_, productver1_.DateReleased as DateRele4_5_0_, productver1_.ProductId as ProductId5_0_ FROM Products this_ left outer join ProductVersions productver1_ on this_.Id = productver1_.ProductId WHERE productver1_.Id in (SELECT TOP ( 1 /* @p1 */ ) this_0_.Id as y0_ FROM ProductVersions this_0_ WHERE this_0_.ProductId = this_.Id ORDER BY this_0_.DateReleased desc)
- Hidratarea proxy-urilor lazy-loaded
- NHibernateUtil.Initialize(entity.LazyLoadedSubEntities);
- http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading.aspx
- Enumeration classes
- Fetch pe subentitati in QueryOver
- http://stackoverflow.com/questions/4801235/nhibernate-3-alternatives-to-thenfetch-in-queryover
- Session.QueryOver<Foo>().Fetch(x => x.A).Fetch(x => x.A.B).Fetch(x => x.A.B.C)
- Session.QueryOver<Foo>().JoinAlias(x => x.A, () => a).JoinAlias(() => a.B, () => b).JoinAlias(() => b.C, () => c)
- object references an unsaved transient instance - save the transient instance before flushing
- Cascade = all pe referinta proaspat adaugata entitatii salvate :) (nu trebuie neaparat sa fie pe o colectie).
- Rezultate duplicate cand folosesc Fetch(<collection>
- SetResultsTransformer(Transformers.DistinctRootEntity);
page revision: 26, last edited: 25 Jan 2012 15:33