|
From: Bill S. <bil...@ya...> - 2006-07-26 04:05:53
Attachments:
HibernateTemplate.patch
|
Hi, I have a question about the load() methods on hibernate3's HibernateTemplate. Spring's javadoc for load() say that an exception will be thrown if the object is not found. However, from what I understand, Hibernate 3 changed its default behavior to lazy load all classes. This means that if you try to load an object with an ID that does not exist, a Hibernate error is not thrown because you get a proxy to the object that does not exist. It is only upon the first method call to that proxy that Hibernate's ObjectNotFoundException is thrown. I would think that in Spring's load() methods should force initilization of that object so that trying to load an object that does not exist immediately throws the correct Spring exception. I have attached a patch that should do the trick. I have never made a patch before, nor have I ever looked through the source of spring or hibernate, so please excuse any stupid mistakes :-) Thanks for a great product, Bill Six __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com |
|
From: Juergen H. <ju...@in...> - 2006-07-28 21:20:47
|
Interesting suggestion... We usually stick to Hibernate semantics for those methods, that is, do exactly what the Hibernate Session does - just with different exceptions thrown. So in this case, it's recommended to turn Hibernate3's lazy loading off to get an actual loaded object back there. Still, we could consider enforcing initialization ourselves there... Juergen -----Original Message----- From: spr...@li... [mailto:spr...@li...] On Behalf Of Bill Six Sent: Wednesday, July 26, 2006 4:08 AM To: spr...@li... Subject: [Springframework-developer] Question/ potential patch fororg.springframework.orm.hibernate3.HibernateTemplate Hi, I have a question about the load() methods on hibernate3's HibernateTemplate. Spring's javadoc for load() say that an exception will be thrown if the object is not found. However, from what I understand, Hibernate 3 changed its default behavior to lazy load all classes. This means that if you try to load an object with an ID that does not exist, a Hibernate error is not thrown because you get a proxy to the object that does not exist. It is only upon the first method call to that proxy that Hibernate's ObjectNotFoundException is thrown. I would think that in Spring's load() methods should force initilization of that object so that trying to load an object that does not exist immediately throws the correct Spring exception. I have attached a patch that should do the trick. I have never made a patch before, nor have I ever looked through the source of spring or hibernate, so please excuse any stupid mistakes :-) Thanks for a great product, Bill Six __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com |
|
From: Colin Y. <col...@gm...> - 2006-07-28 22:06:24
|
Hi Bill, Hibernate3 only makes relationships lazy by default, not the class. This is sensible (in my opinion) because most of the time there is no real reason to load the class in seperate chunks (aka field groups). I believe that they actually mention in their documentation that they only support lazy loading of the class in order to "tick a box" :) Also, checking out the hibernate document for the hibernate Session ( http://hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#load(java.lang.Class,%20java.io.Serializable)) indicates that the load methods *do* check that the object exists. So hibernate will throw its HibernateException and Spring will then convert this into one of its own DataAccessException. HTH. On 26/07/06, Bill Six <bil...@ya...> wrote: > > Hi, > > I have a question about the load() methods on > hibernate3's HibernateTemplate. > > Spring's javadoc for load() say that an exception will > be thrown if the object is not found. However, from > what I understand, Hibernate 3 changed its default > behavior to lazy load all classes. This means that if > you try to load an object with an ID that does not > exist, a Hibernate error is not thrown because you get > a proxy to the object that does not exist. It is only > upon the first method call to that proxy that > Hibernate's ObjectNotFoundException is thrown. I > would think that in Spring's load() methods should > force initilization of that object so that trying to > load an object that does not exist immediately throws > the correct Spring exception. > > I have attached a patch that should do the trick. I > have never made a patch before, nor have I ever looked > through the source of spring or hibernate, so please > excuse any stupid mistakes :-) > > Thanks for a great product, > > Bill Six > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to share > your > opinions on IT & business topics through brief surveys -- and earn cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > > _______________________________________________ > Springframework-developer mailing list > Spr...@li... > https://lists.sourceforge.net/lists/listinfo/springframework-developer > > > > |
|
From: Colin Y. <col...@gm...> - 2006-07-27 17:15:16
|
Actually, althought the javadoc doesn't explicitly mention it (and is quite badly worded actually) load won't hit the database if your class is proxied. This is mentioned in the reference documentation (section 10.3) and is encapsulated in the org.hibernate.event.def.DefaultLoadEventListener. Given these semantics are defined by Hibernate I am not really sure that Spring should change the behaviour as it would quite unintuitive. To our original question, I believe even if the class *is* lazy loaded, load would still hit the database to confirm identity but that is an assumption and I haven't traced it through to verify that behaviour. The Javadocs do make it clear that load should only be used for known persistent objects, if in doubt, use get. Col P.S. I think I will write a couple of tests to verify this behaviour and will get back to you. On 26/07/06, Colin Yates <col...@gm...> wrote: > > Hi Bill, > > Hibernate3 only makes relationships lazy by default, not the class. This > is sensible (in my opinion) because most of the time there is no real reason > to load the class in seperate chunks (aka field groups). I believe that > they actually mention in their documentation that they only support lazy > loading of the class in order to "tick a box" :) > > Also, checking out the hibernate document for the hibernate Session ( http://hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#load(java.lang.Class,%20java.io.Serializable) > > <http://hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#load%28java.lang.Class,%20java.io.Serializable%29>) > indicates that the load methods *do* check that the object exists. So > hibernate will throw its HibernateException and Spring will then convert > this into one of its own DataAccessException. > > HTH. > > On 26/07/06, Bill Six <bil...@ya...> wrote: > > > Hi, > > I have a question about the load() methods on > hibernate3's HibernateTemplate. > > Spring's javadoc for load() say that an exception will > be thrown if the object is not found. However, from > what I understand, Hibernate 3 changed its default > behavior to lazy load all classes. This means that if > you try to load an object with an ID that does not > exist, a Hibernate error is not thrown because you get > a proxy to the object that does not exist. It is only > upon the first method call to that proxy that > Hibernate's ObjectNotFoundException is thrown. I > would think that in Spring's load() methods should > force initilization of that object so that trying to > load an object that does not exist immediately throws > the correct Spring exception. > > I have attached a patch that should do the trick. I > have never made a patch before, nor have I ever looked > through the source of spring or hibernate, so please > excuse any stupid mistakes :-) > > Thanks for a great product, > > Bill Six > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to share > your > opinions on IT & business topics through brief surveys -- and earn cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > > _______________________________________________ > Springframework-developer mailing list > Spr...@li... > https://lists.sourceforge.net/lists/listinfo/springframework-developer > > > > > |
|
From: Bill S. <bil...@ya...> - 2006-07-28 04:36:36
|
Comments inline: --- Colin Yates <col...@gm...> wrote: > Actually, althought the javadoc doesn't explicitly > mention it (and is quite > badly worded actually) load won't hit the database > if your class is proxied. > This is mentioned in the reference documentation > (section 10.3) and is > encapsulated in the > org.hibernate.event.def.DefaultLoadEventListener. > > Given these semantics are defined by Hibernate I am > not really sure that > Spring should change the behaviour as it would quite > unintuitive. > > To our original question, I believe even if the > class *is* lazy loaded, load > would still hit the database to confirm identity but > that is an assumption > and I haven't traced it through to verify that > behaviour. The Javadocs do > make it clear that load should only be used for > known persistent objects, if > in doubt, use get. > Ok cool, thanks for your help - I appreciate it. I can send you my code that gave me this error if you'd like. > Col > > P.S. I think I will write a couple of tests to > verify this behaviour and > will get back to you. That'd be great. Thanks again, Bill __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com |
|
From: Colin Y. <col...@gm...> - 2006-07-28 20:17:52
|
> Ok cool, thanks for your help - I appreciate it. No need to thank me....I learned something along the way, so thank you ;) I can send you my code that gave me this error if > you'd like. Please. Can you zip up all the files (without the hibernate/spring dependencies) and put them somewhere I/we can download? Thanks, Col > Col > > > > P.S. I think I will write a couple of tests to > > verify this behaviour and > > will get back to you. > > That'd be great. > > Thanks again, > > Bill > > > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to share > your > opinions on IT & business topics through brief surveys -- and earn cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > _______________________________________________ > Springframework-developer mailing list > Spr...@li... > https://lists.sourceforge.net/lists/listinfo/springframework-developer > |
|
From: Colin Y. <col...@gm...> - 2006-07-28 21:20:26
|
OK, so I learned something today :) Calling session.load (and hibernateTemplate.load) for a single non-final, non-proxied class does *not* hit the database even though the javadocs imply it will, so the loading of the class is (as you original stated) lazy. Marking the class as lazy=false forces hibernate to check that the row exists, but by default it doesn't. If I understand this right, the only time and exception is thrown on session.load is if the class is not lazy..... I still think Spring is doing the right thing in being consistent with Hibernate's behaviour. I learn something new every day :) Sorry for the noise. Col On 26/07/06, Colin Yates <col...@gm...> wrote: > > Actually, althought the javadoc doesn't explicitly mention it (and is > quite badly worded actually) load won't hit the database if your class is > proxied. This is mentioned in the reference documentation (section 10.3) > and is encapsulated in the > org.hibernate.event.def.DefaultLoadEventListener. > > Given these semantics are defined by Hibernate I am not really sure that > Spring should change the behaviour as it would quite unintuitive. > > To our original question, I believe even if the class *is* lazy loaded, > load would still hit the database to confirm identity but that is an > assumption and I haven't traced it through to verify that behaviour. The > Javadocs do make it clear that load should only be used for known persistent > objects, if in doubt, use get. > > Col > > P.S. I think I will write a couple of tests to verify this behaviour and > will get back to you. > > > On 26/07/06, Colin Yates < col...@gm...> wrote: > > > > Hi Bill, > > > > Hibernate3 only makes relationships lazy by default, not the class. > > This is sensible (in my opinion) because most of the time there is no real > > reason to load the class in seperate chunks (aka field groups). I > > believe that they actually mention in their documentation that they only > > support lazy loading of the class in order to "tick a box" :) > > > > Also, checking out the hibernate document for the hibernate Session ( http://hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#load(java.lang.Class,%20java.io.Serializable) > > > > <http://hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#load%28java.lang.Class,%20java.io.Serializable%29>) > > indicates that the load methods *do* check that the object exists. So > > hibernate will throw its HibernateException and Spring will then convert > > this into one of its own DataAccessException. > > > > HTH. > > > > On 26/07/06, Bill Six <bil...@ya...> wrote: > > > > > Hi, > > > > I have a question about the load() methods on > > hibernate3's HibernateTemplate. > > > > Spring's javadoc for load() say that an exception will > > be thrown if the object is not found. However, from > > what I understand, Hibernate 3 changed its default > > behavior to lazy load all classes. This means that if > > you try to load an object with an ID that does not > > exist, a Hibernate error is not thrown because you get > > a proxy to the object that does not exist. It is only > > upon the first method call to that proxy that > > Hibernate's ObjectNotFoundException is thrown. I > > would think that in Spring's load() methods should > > force initilization of that object so that trying to > > load an object that does not exist immediately throws > > the correct Spring exception. > > > > I have attached a patch that should do the trick. I > > have never made a patch before, nor have I ever looked > > through the source of spring or hibernate, so please > > excuse any stupid mistakes :-) > > > > Thanks for a great product, > > > > Bill Six > > > > __________________________________________________ > > Do You Yahoo!? > > Tired of spam? Yahoo! Mail has the best spam protection around > > http://mail.yahoo.com > > > > > > ------------------------------------------------------------------------- > > Take Surveys. Earn Cash. Influence the Future of IT > > Join SourceForge.net's Techsay panel and you'll get the chance to share > > your > > opinions on IT & business topics through brief surveys -- and earn cash > > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > > > > > > _______________________________________________ > > Springframework-developer mailing list > > Spr...@li... > > https://lists.sourceforge.net/lists/listinfo/springframework-developer > > > > > > > > > > > |
|
From: Colin S. <col...@ex...> - 2006-07-29 00:35:55
|
We need to maintain the same semantics as Hibernate here. It would introduce massive confusion if it behaved differently. And indeed, if the object is marked as lazy (to be proxied), load() from Hibernate will happily return a prox for an object which doesn't exist, throwing an exception only when you try to use the object. This was actually an unbelievably bad design decision on their part, as it makes the "throws exception on non-existence" part completely unreliable if you think somebody is going to turn on proxying for the objects, but anyway... On 7/26/2006 4:18 AM, Colin Yates wrote: > Actually, althought the javadoc doesn't explicitly mention it (and is > quite badly worded actually) load won't hit the database if your class > is proxied. This is mentioned in the reference documentation (section > 10.3) and is encapsulated in the > org.hibernate.event.def.DefaultLoadEventListener. > > Given these semantics are defined by Hibernate I am not really sure > that Spring should change the behaviour as it would quite unintuitive. > > To our original question, I believe even if the class *is* lazy > loaded, load would still hit the database to confirm identity but that > is an assumption and I haven't traced it through to verify that > behaviour. The Javadocs do make it clear that load should only be > used for known persistent objects, if in doubt, use get. > > Col > > P.S. I think I will write a couple of tests to verify this behaviour > and will get back to you. > > On 26/07/06, *Colin Yates* < col...@gm... > <mailto:col...@gm...>> wrote: > > Hi Bill, > > Hibernate3 only makes relationships lazy by default, not the > class. This is sensible (in my opinion) because most of the time > there is no real reason to load the class in seperate chunks (aka > field groups). I believe that they actually mention in their > documentation that they only support lazy loading of the class in > order to "tick a box" :) > > Also, checking out the hibernate document for the hibernate > Session ( > http://hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#load(java.lang.Class,%20java.io.Serializable) > <http://hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#load%28java.lang.Class,%20java.io.Serializable%29>) > indicates that the load methods *do* check that the object > exists. So hibernate will throw its HibernateException and Spring > will then convert this into one of its own DataAccessException. > > HTH. > > On 26/07/06, *Bill Six* <bil...@ya... > <mailto:bil...@ya...>> wrote: > Hi, > > I have a question about the load() methods on > hibernate3's HibernateTemplate. > > Spring's javadoc for load() say that an exception will > be thrown if the object is not found. However, from > what I understand, Hibernate 3 changed its default > behavior to lazy load all classes. This means that if > you try to load an object with an ID that does not > exist, a Hibernate error is not thrown because you get > a proxy to the object that does not exist. It is only > upon the first method call to that proxy that > Hibernate's ObjectNotFoundException is thrown. I > would think that in Spring's load() methods should > force initilization of that object so that trying to > load an object that does not exist immediately throws > the correct Spring exception. > > I have attached a patch that should do the trick. I > have never made a patch before, nor have I ever looked > through the source of spring or hibernate, so please > excuse any stupid mistakes :-) > > Thanks for a great product, > > Bill Six > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to > share your > opinions on IT & business topics through brief surveys -- and earn > cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > <http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV> > > _______________________________________________ > Springframework-developer mailing list > Spr...@li... > <mailto:Spr...@li...> > https://lists.sourceforge.net/lists/listinfo/springframework-developer > <https://lists.sourceforge.net/lists/listinfo/springframework-developer> > > > > > >------------------------------------------------------------------------ > >------------------------------------------------------------------------- >Take Surveys. Earn Cash. Influence the Future of IT >Join SourceForge.net's Techsay panel and you'll get the chance to share your >opinions on IT & business topics through brief surveys -- and earn cash >http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > >------------------------------------------------------------------------ > >_______________________________________________ >Springframework-developer mailing list >Spr...@li... >https://lists.sourceforge.net/lists/listinfo/springframework-developer > > |