-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Ignore where clause
PostPosted: Mon Mar 23, 2009 5:20 am 
Newbie

Joined: Sun Mar 08, 2009 11:48 pm
Posts: 3
Location: Sweden
Hello people.

User.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                            assembly="hiddenDomain"
                            namespace="hidden.Domain.Entities.Users">
   <class name="User" table="`Users`" lazy="false" where="`Active` = 1">
      <id name="Id" column="`ID`">
         <generator class="identity"/>
      </id>
      <property name="Username" column="`Username`" unique="true" length="100" not-null="true"/>
      <property name="Password" column="`Password`" length="40" not-null="true"/>
      <property name="Email" column="`Email`" length="200" not-null="true"/>
      <property name="Realname" column="`Realname`" length="100" not-null="true"/>
      <property name="Phone" column="`Phone`" length="100"  not-null="true"/>
      <property name="RegisterDate" column="`RegisterDate`" not-null="true"/>
      <property name="IsActive" column="`Active`" not-null="true"/>
      <bag name="Roles" lazy="false" cascade="all" table="`UserHasrole`">
         <key column="`User`"/>
         <many-to-many class="Role" column="`Role`" lazy="false"/>
      </bag>
   </class>
</hibernate-mapping>


Nothing strange, works perfectly fine.

The thing is, there are one case when I don't want that WHERE clause. When I insert a new User I check if there's already a user with that email and username. But I want to check if there's an inactive user with that username and email too. So how can I tell NHibernate to ignore the where clause for this query?

Code to select...
Code:
var criteria = session.CreateCriteria ( typeof ( User ) )
               .Add ( Restrictions.And ( Restrictions.Eq ( "IsActive", false ), Restrictions.Eq ( "Email", email ) ) );

            return criteria.UniqueResult<User> ( );


Generated SQL:
Code:
SELECT this_.[ID] as ID1_0_0_, this_.[Username] as Username2_0_0_, this_.[Password] as Password3_0_0_, this_.[Email] as Email4_0_0_, this_.[Realname] as Realname5_0_0_, this_.[Phone] as Phone6_0_0_, this_.[RegisterDate] as Register7_0_0_, this_.[Active] as Active8_0_0_ FROM [Users] this_ WHERE ( this_.[Active] = 1) AND (this_.[Active] = @p0 and this_.[Username] = @p1); @p0 = 'False', @p1 = 'Erik'


Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 6:49 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have a look at the filter mechanism hibernate offers:

http://www.nhforge.org/doc/nh/en/index.html#filters

You can enable/disable the filter in each session separatly.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 8:09 am 
Regular
Regular

Joined: Wed Feb 11, 2009 10:58 am
Posts: 55
can I do the following?

Session.Get(...) // get some Data
Session.EnableFilter(...). ... // enable Filter
Session.Get(...) // new filtered result?
Session.DisableFilter(...). ... // disable Filter
Session.Get(...) // get some Data

My Question is can I enable and disable the filters in a session when I need them?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 8:23 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Afaik yes. But I never tried it.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.