From: Chris N. <ch...@si...> - 2002-11-05 23:27:41
|
I'd like to dynamically wrap my persistent classes using CGLIB or JDK1.3 proxies to provide things like getters for metadata which aren't in the compiled class file. I've done some searching and it appears that all of the persistent classes are loaded by Hibernate using cirrus.hibernate.helpers.ReflectHelper.classForName(String) which is implemented as: public static Class classForName(String name) throws ClassNotFoundException { try { return Thread.currentThread().getContextClassLoader().loadClass(name); } catch (Exception e) { return Class.forName(name); } } One option would be to try to set the current context class loader before Hibernate starts loading classes. But that is error-prone, I think. Another way would be to add a method to set the classloader to use. But really I'd just prefer a simple callback interface: public interface HibernateClassLoader { public Class loadClass(String name) throws ClassNotFoundException; } I guess the loader would be configurable per-datastore? If there's a better way, let me know. Thanks, Chris |
From: Gavin K. <ga...@ap...> - 2002-11-06 14:05:05
|
I think its better to do this kind of thing by subclassing EntityPersister and overriding instantiate(). However, that will not work properly in the current implentation (for example getPersister( object.getClass() ) would fail). Would you please investigate what kind of changes would be required to make this approach work? ----- Original Message ----- From: "Chris Nokleberg" <ch...@si...> To: <hib...@li...> Sent: Wednesday, November 06, 2002 10:27 AM Subject: [Hibernate] Hooking into Hibernate classloader > I'd like to dynamically wrap my persistent classes using CGLIB or JDK1.3 > proxies to provide things like getters for metadata which aren't in the > compiled class file. I've done some searching and it appears that all of the > persistent classes are loaded by Hibernate using > > cirrus.hibernate.helpers.ReflectHelper.classForName(String) > > which is implemented as: > > public static Class classForName(String name) throws ClassNotFoundException { > try { > return Thread.currentThread().getContextClassLoader().loadClass(name); > } catch (Exception e) { > return Class.forName(name); > } > } > > One option would be to try to set the current context class loader before > Hibernate starts loading classes. But that is error-prone, I think. Another > way would be to add a method to set the classloader to use. But really I'd > just prefer a simple callback interface: > > public interface HibernateClassLoader { > public Class loadClass(String name) throws ClassNotFoundException; > } > > I guess the loader would be configurable per-datastore? > If there's a better way, let me know. > > Thanks, > Chris > > > ------------------------------------------------------- > This sf.net email is sponsored by: See the NEW Palm > Tungsten T handheld. Power & Color in a compact size! > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0001en > _______________________________________________ > hibernate-devel mailing list > hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel |
From: Chris N. <ch...@si...> - 2002-11-07 00:07:10
|
On Thu, Nov 07, 2002 at 01:04:04AM +1100, Gavin King wrote: > I think its better to do this kind of thing by subclassing EntityPersister > and overriding instantiate(). However, that will not work properly in the > current implentation (for example getPersister( object.getClass() ) would > fail). Would you please investigate what kind of changes would be required > to make this approach work? In a nutshell, if goal is to allow instantiate to return any object as a substitute for the original object, then Hibernate needs a way to take that object and map back to the original object. I was toying around with requiring the substituted object to implement a special interface: public interface UserProxy { public Object getProxiedObject(); } Hibernate would dereference incoming objects that implemented the interface. For performance, maybe it would only do this after determining that it couldn't work with original object. But this doesn't handle things like SessionFactory.getClassMetadata, which takes a Class. Which leads me to believe that maybe it is better to keep this all out of Hibernate and just say that it's up to the application to wrap the Session, SessionFactory, and other public interfaces appropriately. -Chris |
From: Max R. A. <ma...@eo...> - 2002-11-07 08:23:44
|
> But this doesn't handle things like SessionFactory.getClassMetadata, > which takes a Class. Just asking out of personal interest :) why is everything in hibernate bound up onto Class objects, why not fully-qualifed-names in the form of strings instead ? That would make it more flexible and resistant to things that might not be availble on the classpath and e.g. make it possible to load internal mapping model and use it for codegeneration and other stuff which in its nature cannot rely on the classing being in the classpath. /max > Which leads me to believe that maybe it is better > to keep this all out of Hibernate and just say that it's up to the > application to wrap the Session, SessionFactory, and other public > interfaces appropriately. > > -Chris > > > > > ------------------------------------------------------- > This sf.net email is sponsored by: See the NEW Palm > Tungsten T handheld. Power & Color in a compact size! > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0001en > _______________________________________________ > hibernate-devel mailing list > hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel > |
From: Gavin K. <ga...@ap...> - 2002-11-07 08:34:59
|
Firstly, the API *should* use Class objects to reduce the possibility of runtime errors. I forget which project I saw recently that had used strings and was now moving to using class objects. Internally, we *could* use classnames, but that would be slower, right? Its much quicker to take an identityHashCode than to compute the hash of a (possibly quite long) String. Or am I wrong on this? As for why can't we compile mappings without the classes, thats because various things (property types, etc) may be determined by reflection. I still think this is a nice feature. ----- Original Message ----- From: "Max Rydahl Andersen" <ma...@eo...> Cc: "hibernate list" <hib...@li...> Sent: Thursday, November 07, 2002 7:23 PM Subject: Re: [Hibernate] Hooking into Hibernate classloader > > > But this doesn't handle things like SessionFactory.getClassMetadata, > > which takes a Class. > > Just asking out of personal interest :) > why is everything in hibernate bound up onto Class objects, why not > fully-qualifed-names in > the form of strings instead ? > > That would make it more flexible and resistant to things that might not be > availble on the classpath > and e.g. make it possible to load internal mapping model and use it for > codegeneration and other > stuff which in its nature cannot rely on the classing being in the > classpath. > > /max > > > Which leads me to believe that maybe it is better > > to keep this all out of Hibernate and just say that it's up to the > > application to wrap the Session, SessionFactory, and other public > > interfaces appropriately. > > > > -Chris > > > > > > > > > > ------------------------------------------------------- > > This sf.net email is sponsored by: See the NEW Palm > > Tungsten T handheld. Power & Color in a compact size! > > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0001en > > _______________________________________________ > > hibernate-devel mailing list > > hib...@li... > > https://lists.sourceforge.net/lists/listinfo/hibernate-devel > > > > > > ------------------------------------------------------- > This sf.net email is sponsored by: See the NEW Palm > Tungsten T handheld. Power & Color in a compact size! > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0001en > _______________________________________________ > hibernate-devel mailing list > hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel |
From: Juozas B. <ba...@ce...> - 2002-11-07 17:22:07
|
Is Reflect helper used to load classes ? It can be a problem on some app servers : public static Class classForName(String name) throws ClassNotFoundException { try { return Thread.currentThread().getContextClassLoader().loadClass(name); } catch (Exception e) { return Class.forName(name); } sometimes Thread.currentThread().getContextClassLoader() != Application.classLoader; I think it is on some tomcat versions. It is good to have optional classLoader parameter in SessionFactory, to load metadata and persistent classes. Default can be: "this.getClass().getClassLoader();" (the same to load application and SessionFactory implementation ). ----- Original Message ----- From: "Gavin King" <ga...@ap...> To: "Max Rydahl Andersen" <ma...@eo...> Cc: <hib...@li...> Sent: Thursday, November 07, 2002 9:37 AM Subject: Re: [Hibernate] Hooking into Hibernate classloader > Firstly, the API *should* use Class objects to reduce the possibility of > runtime errors. I forget which project I saw recently that had used strings > and was now moving to using class objects. Internally, we *could* use > classnames, but that would be slower, right? Its much quicker to take an > identityHashCode than to compute the hash of a (possibly quite long) String. > Or am I wrong on this? > > As for why can't we compile mappings without the classes, thats because > various things (property types, etc) may be determined by reflection. I > still think this is a nice feature. > > ----- Original Message ----- > From: "Max Rydahl Andersen" <ma...@eo...> > Cc: "hibernate list" <hib...@li...> > Sent: Thursday, November 07, 2002 7:23 PM > Subject: Re: [Hibernate] Hooking into Hibernate classloader > > > > > > > But this doesn't handle things like SessionFactory.getClassMetadata, > > > which takes a Class. > > > > Just asking out of personal interest :) > > why is everything in hibernate bound up onto Class objects, why not > > fully-qualifed-names in > > the form of strings instead ? > > > > That would make it more flexible and resistant to things that might not be > > availble on the classpath > > and e.g. make it possible to load internal mapping model and use it for > > codegeneration and other > > stuff which in its nature cannot rely on the classing being in the > > classpath. > > > > /max > > > > > Which leads me to believe that maybe it is better > > > to keep this all out of Hibernate and just say that it's up to the > > > application to wrap the Session, SessionFactory, and other public > > > interfaces appropriately. > > > > > > -Chris > > > > > > > > > > > > > > > ------------------------------------------------------- > > > This sf.net email is sponsored by: See the NEW Palm > > > Tungsten T handheld. Power & Color in a compact size! > > > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0001en > > > _______________________________________________ > > > hibernate-devel mailing list > > > hib...@li... > > > https://lists.sourceforge.net/lists/listinfo/hibernate-devel > > > > > > > > > > > ------------------------------------------------------- > > This sf.net email is sponsored by: See the NEW Palm > > Tungsten T handheld. Power & Color in a compact size! > > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0001en > > _______________________________________________ > > hibernate-devel mailing list > > hib...@li... > > https://lists.sourceforge.net/lists/listinfo/hibernate-devel > > > > ------------------------------------------------------- > This sf.net email is sponsored by: See the NEW Palm > Tungsten T handheld. Power & Color in a compact size! > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0001en > _______________________________________________ > hibernate-devel mailing list > hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel |