-->
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.  [ 3 posts ] 
Author Message
 Post subject: Criteria, Example.create () and foreign keys...
PostPosted: Mon Mar 27, 2006 6:02 pm 
Beginner
Beginner

Joined: Sat Sep 17, 2005 10:41 am
Posts: 49
Maybe I'm using it wrong, but it seems that Example.create () only works for simple properties (Long/String/etc) and doesn't take into account foreign key relationships.

My use case:

Address a = new Address () ;
a.setCity ("somecity") ;
a.setState (new State ("AL")) ;

... = Example.create (a) ;

The State entity is never included in the generated SQL...

I can implement a workaround that uses reflection to include foreign key relations, but I haven't seen documentation on Example.create () to otherwise indicate that my usage isn't supported...

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 7:14 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Try this:
Example ex = Example.create (a).excludeNone();

or set your custom property selector Example.create (a).setPropertySelector( custom );

By default NOT_NULL is used and I suspect that your state field is the nullable one

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 8:26 pm 
Beginner
Beginner

Joined: Sat Sep 17, 2005 10:41 am
Posts: 49
I did not want to use "excludeNone()" as I wanted null properties to be ignored. In the end, I used the introspection workaround. For anyone who's interested:

Code:
  public <T extends BaseEntity> List<T> listByExample (T example)
  {
    /*
    return listByCriteria ((Class<T>) example.getClass(),
                           Example.create (example)
                             .ignoreCase()
                             .enableLike ()) ;
     */
    Class<T> clazz = (Class<T>) example.getClass() ;
    Criteria crit = getSession().createCriteria (clazz) ;
    crit.add (Example.create (example).ignoreCase().enableLike()) ;

    PropertyDescriptor [] pds = PropertyUtils.getPropertyDescriptors (example) ;
    for (PropertyDescriptor pd : pds) {
      Class<?> propertyClass = pd.getPropertyType () ;
     
      if (BaseEntity.class.isAssignableFrom (propertyClass)) {
        String propertyName = pd.getName () ;
       
        if (log.isDebugEnabled ())
          log.debug ("Found property '" + propertyName + "' of type " + propertyClass.getName ()) ;

        BaseEntity foreignEntity = null ;
        try {
          foreignEntity = (BaseEntity) PropertyUtils.getProperty (example, propertyName) ;
        }
        catch (Exception e) {
          log.error (e) ;
        }
       
        if (foreignEntity != null) {
          Serializable foreignId = foreignEntity.getId () ;
          if (foreignId != null) {
            // create sub-criteria based on id
            crit.createCriteria (propertyName)
              .add (Restrictions.idEq (foreignId)) ;
          }
          else {
            // create sub-criteria based on matching properties
            crit.createCriteria (propertyName)
              .add (Example.create (foreignEntity).ignoreCase().enableLike()) ;
          }
        }
      }
    }
   
    return crit.list () ;                       
  }



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