NHibernate
  • 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
  • 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.
  • 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)
  • 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);
  • Optimizat startup time-ul
    • <add key="hibernate.use_reflection_optimizer" value="false" /> <- NH nu mai genereaza clase proxy la startup, ci acceseaza membrii prin reflection
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License