In the mapping
<query name="MyEntity.Exists">
select e.id from MyEntity e where e.Prop1 = :pp1 and e.Prop2 = :pp2
</query>

In MyEntityDao
public bool Exists(MyEntity example)
{
return Session.GetNamedQuery("MyEntity.Exists")
.SetParameter("pp1",example.Prop1)
.SetParameter("pp2",example.Prop2)
.UniqueResult() != null;
}

More then one-code-line the query in the mapping is parsed/compiled/prepared
during BuildSessionFactory, in the Exists method execution NH only get the
prepared command, set parameters and execute the query.
The other advantage is that the "NaturalId" is in the mapping with all
others "persistence matters".

But... each one have his way... I did myyyyy waaaay

2008/12/18 epitka <[email protected]>

>
> This seems to work. Is there better way (don't worry about reflection)
>
> public bool EntityExists(PersistentObject exampleInstance, params
> string[] propertiesToInclude)
>        {
>            List<string> includedProperties  = new List<string>
> (propertiesToInclude);
>
>            Type exampleType = exampleInstance.GetType();
>            ICriteria criteria = NHSession.CreateCriteria
> (exampleType);
>            Example example = Example.Create(exampleInstance);
>
>            PropertyInfo[] properties = exampleType.GetProperties();
>
>            foreach (PropertyInfo pi  in properties)
>            {
>                if (includedProperties.Contains(pi.Name) == false)
>                {
>                    example.ExcludeProperty(pi.Name);
>                }
>            }
>
>            criteria.Add(example);
>            return criteria.SetProjection(Projections.Id()).List
> ().Count>0;
>         }
>
> On Dec 18, 11:08 am, "Fabio Maulo" <[email protected]> wrote:
> > 2008/12/18 epitka <[email protected]>
> >
> >
> >
> > > What is am after is just a simple way to confim that it either exists
> > > or not without hydrating entities. So select e.id would do it?
> >
> > "select e.id ..." return an instance of the id (in your case a string)
> if
> > present.
> >
> >
> >
> >
> >
> > > Example api does not provide IncludeProperties just ExcludeProperties.
> > > Is there a reason other then time?
> >
> > > On Dec 18, 10:41 am, "Fabio Maulo" <[email protected]> wrote:
> > > > I prefer use DAO/Repository that is the only tier knowing something
> about
> > > > persistence.Where I need a specific Exists method based on a
> NaturalId
> > > I'm
> > > > not using Count but only
> > > > select e.id from .... where .....
> >
> > > > 2008/12/18 epitka <[email protected]>
> >
> > > > > Thanks Fabio,
> > > > > I look into named query, I think I found another thread that talks
> > > > > about this.  I put this in the "service" rather then "repository".
> My
> > > > > repositories only fetch and persist. Anything else like get the
> total
> > > > > of something, check this or that go into service classes. Does this
> > > > > make sense?
> >
> > > > > On Dec 18, 10:08 am, "Fabio Maulo" <[email protected]> wrote:
> > > > > > - Create a method in your DAO/Repository named Exists(YourEntity
> > > example)
> > > > > -
> > > > > > In the mapping write a namedQuery named "YourEntity.Exists"
> > > > > > - the query may look like : select count(*) from YourEntity e
> where
> > > > > > e.NaturalIdProperty1 = :pp1 and e.NaturalIdProperty2 = :pp2
> > > > > > - Implements the Exists method using the named query
> >
> > > > > > If you don't want use HQL you can use Criteria but, in this case
> > > where
> > > > > you
> > > > > > have a static query, HQL is better candidate.
> >
> > > > > > 2008/12/18 epitka <[email protected]>
> >
> > > > > > > Hi,
> > > > > > > I use surrogate keys on my tables, but some tables have columns
> > > that
> > > > > > > are unique. For example one cannot create a project that has
> the
> > > same
> > > > > > > name. So I am wondering what is the best way to query using NH
> to
> > > > > > > check if name already exists in the table, so I can make a
> check
> > > > > > > rather then catching exception. I do not want to populate the
> > > entity
> > > > > > > with that name just return count. Can anybody give example.
> >
> > > > > > --
> > > > > > Fabio Maulo
> >
> > > > --
> > > > Fabio Maulo
> >
> > --
> > Fabio Maulo
> >
>


-- 
Fabio Maulo

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to