-->
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.  [ 1 post ] 
Author Message
 Post subject: Typesafe Object in HQL Select
PostPosted: Sat Jun 02, 2007 9:17 am 
Newbie

Joined: Thu Jan 25, 2007 7:43 pm
Posts: 2
Per the documentation in section 14.6 in hibernate docs:

http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#queryhql-select

Quote:
Code:
select new Family(mother, mate, offspr)
from DomesticCat as mother
    join mother.mate as mate
    left join mother.kittens as offspr

assuming that the class Family has an appropriate constructor.

Why does the constructor in this case has to public?

This method in ReflectHelper gets called:

Code:
   public static Constructor getConstructor(Class clazz, Type[] types) throws PropertyNotFoundException {
      final Constructor[] candidates = clazz.getConstructors();
      for ( int i=0; i<candidates.length; i++ ) {
         final Constructor constructor = candidates[i];
         final Class[] params = constructor.getParameterTypes();
         if ( params.length==types.length ) {
            boolean found = true;
            for ( int j=0; j<params.length; j++ ) {
               final boolean ok = params[j].isAssignableFrom( types[j].getReturnedClass() ) || (
                  types[j] instanceof PrimitiveType &&
                  params[j] == ( (PrimitiveType) types[j] ).getPrimitiveClass()
               );
               if (!ok) {
                  found = false;
                  break;
               }
            }
            if (found) {
               if ( !isPublic(clazz, constructor) ) constructor.setAccessible(true);
               return constructor;
            }
         }
      }
      throw new PropertyNotFoundException( "no appropriate constructor in class: " + clazz.getName() );
   }



Notice the use of clazz.getConstructors() which only returns an array of public constructors - and then later when a constructor is found, a check is done to see if the consturctor is public or not???

Why not use clazz.getDeclaredConstructors() which would return constructors declared with any access level (public,protected, package and private) -

This same logic is used when searching for a default constructor for a mapped class, why not use it in this case?

I am trying to keep my domain model clean and I don't want to create unnecessary public constructors -

Any suggestions?

Thanks,

-Mujahid


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

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.