-->
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.  [ 12 posts ] 
Author Message
 Post subject: Improvement suggestion: null values in properties mapping
PostPosted: Sun May 29, 2005 10:59 am 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
Hi,

What dou you think about adding a new attribute to the PROPERTY element in the Hibernate 3 mapping, so you are able to define what must be treated as a null value within a property?

Code:
<!ELEMENT property (meta*,(column|formula)*,type?)>
   <!ATTLIST property name CDATA #REQUIRED>
   <!ATTLIST property node CDATA #IMPLIED>
   <!ATTLIST property access CDATA #IMPLIED>
   <!ATTLIST property type CDATA #IMPLIED>
   <!ATTLIST property column CDATA #IMPLIED>
   <!ATTLIST property length CDATA #IMPLIED>
   <!ATTLIST property precision CDATA #IMPLIED>
   <!ATTLIST property scale CDATA #IMPLIED>
   <!ATTLIST property not-null (true|false) "false">
   <!ATTLIST property unique (true|false) "false">
   <!ATTLIST property update (true|false) "true">
   <!ATTLIST property insert (true|false) "true">
   <!ATTLIST property optimistic-lock (true|false) "true">
   <!ATTLIST property formula CDATA #IMPLIED>
   <!ATTLIST property index CDATA #IMPLIED>
   <!ATTLIST property lazy (true|false) "false">
   <!ATTLIST property null-value CDATA #IMPLIED> <-----THIS ONE

This would be very helpful, for example, when working with searching criteria represented as combos. Imagine you have a property which is an Integer, and the user has to select in a combo its value, but can also select all the possible values. If we could set its null-value property as follows:

Code:
<property name="country" type="java.lang.Integer" column="IDCOUNTRY" not-null="true" null-value="-1" />


Then we could add a new selection to the combo named "ALL THE COUNTRIES", which will be initialized to -1, so when the search criteria arrives to the DAO, then Hibernate knows it's like null, so Hibernate will just ignore it.

What do you think? Could this be interesting? If you agree I could post an improvement in JIRA :-)


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 1:00 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This is an interesting addition because it influence something I've been thinking about for a while: we could hide the ternary operators IS [NOT] NULL and make SQL a little bit easier to use. I didn't spend much time trying to figure out the consequences of this. Please post to JIRA as Improvement and include my response. Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 1:14 pm 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
OK. Done.

http://opensource.atlassian.com/project ... se/HHH-555


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 2:23 pm 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
Please, friends, vote it so we can get it done soon ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 2:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I cannot see how null-value='-1' is helpfull since the null value actually is not -1 its NULL....

and NULL is not equal to "ALL THE XXX", its actually the complete opposite.

what you are looking for (with respect to your search criteria example)sounds more like a custom Example.PropertySelector.
(and you can use <meta> to add your wanted behavior)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 2:38 pm 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
Well, it's not intended to substitute the NULL value, but to provide a mechanism to tell the Criteria API that if the <null-value> arrives, just skip it.

How could it be achieved then using the PropertySelector? And what is that <meta> about?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 2:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Exactly and it surely has nothing to do with NULL ;)

meta attribute is something you can apply to class'es and assocations, e.g.
Code:
<property name="prop">
  <meta attribute="criteria-all-value">0</meta>
</property>


and now that stuff is available via Property.getMetaAttributes()

PropertySelector is what is used to tell hibernate which attributes to consider in Criteria/Example queries:

s.createCriteria(Account.class)
.add( Example.create(accountBean). setPropertySelector(new YourCustomSelector());

Then your propery selector is passed each property name and the example value on which you can decide wether you want it included or not.

Hibernate already has a few built in which is activated by calling excludeZeroes(), and excludeNone().

So an example for your usecase would be:

class NotNullOrMinusPropertySelector implements PropertySelector {
public boolean include(Object object, String propertyName, Type type) {
return object!=null && (
!(object instanceof Number) || ( (Number) object ).longValue()!=-1
);
}
}

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 2:52 pm 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
OK. Did some investigation:

Code:
   private class PerfilPropertySelector implements Example.PropertySelector
   {

      public boolean include(Object propertyValue, String propertyName,
            Type type)
      {
         // Si se trata de la propiedad Perfil, no la incluimos si viene a
         // PerfilUsuarioEnum.TODOS
         if (propertyName.equals("perfil")
               && propertyValue == PerfilUsuarioEnum.TODOS)
         {
            return false;
         }

         return true;
      }

   }


I will complete this example with the use of a <meta> tag to define the "skip" value ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 3:00 pm 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
By the way, how can that Property.getMetaAttributes() method be accesed if I only have the property's name that the include method gives me as a parameter?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 3:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
its accessible via the SessionFactory getclassMetaData and you can then just pass in the correct metadata to your property selector.

/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 3:15 pm 
Beginner
Beginner

Joined: Fri Sep 24, 2004 7:15 am
Posts: 40
Sorry, but what method should I call in getClassMetadata to obtain those meta attributes?

Could you possibly provide a working example? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 3:34 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
sorry they are not on sessionfactory, but on the Configuration.

look at getClassMapping("whatever").getProperty("prop").getMetaAttribute("doh")

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.