|
From: Juergen H. <ju...@in...> - 2005-03-13 21:50:45
|
We've just received a request for further overloaded methods on HibernateTemplate: taking Hibernate3 entity names as alternative to Class arguments, analogous to the Hibernate3 Session. http://opensource.atlassian.com/projects/spring/browse/SPR-777 As I've replied there, I'm somewhat reluctant to add the overloaded operations for entity names to HibernateTemplate itself. HibernateTemplate already has quite a lot of operations on it; I'd like to avoid cluttering it with even more overloaded methods. Of course, like for all needs that HibernateTemplate's convenience methods do not cover, implement a custom HibernateCallback and use the Hibernate Session API natively. With an anonymous inner class, such a callback is straightforward to implement, but of course more verbose than a one-line convenience operation call. It's worth pointing out that there is an alternative now, enabled by Hibernate3's use of unchecked exceptions: direct Hibernate Session access via SessionFactoryUtils.getSession - if you can live with Hibernate3's unchecked HibernateException's getting thrown to callers. If you can guarantee to always execute within a transaction in such a scenario, you don't even need to call SessionFactoryUtils.closeSessionIfNecessary, which allows you to write straightforward direct Session access code. public class MyDao extends HibernateDaoSupport { public MyObject loadMyObject(String id) throws HibernateException { return (MyObject) getSession(false).load("myEntityName", id); } } The advantage of this style is that there is no wrapping of the Hibernate Session: It's plain Hibernate Session access; the only thing Spring provides is a lookup method that returns the Spring-managed transactional Hibernate Session. The disadvantage is, of course, that HibernateExceptions will get thrown as-is, which means that callers have to be coded against Hibernate-specific exceptions when trying to handle them. If those exceptions don't get handled explicitly in business facade code, exception mappings in error views are affected. At least callers don't need to declare the formerly checked HibernateException anymore: If they don't care about exceptions thrown by the DAO, they can simply let the now (since Hibernate3) unchecked HibernateException through - without explicitly catching it or declaring it in the "throws" clause. An interesting question is whether we should always strictly recommend the HibernateTemplate/DataAccessException approach, even in case of many custom HibernateCallbacks. In some applications, people might not care about generic data access exceptions, so Hibernate3's unchecked exceptions might be acceptable... The same applies to JDO: unchecked JDOExceptions might be acceptable as DAO exceptions in some scenarios. Like in the Hibernate3 case, this is only really interesting if always executing within a transaction, though: else, DAO implementations would get cluttered with try-finally blocks for closing resources. Thoughts? Opinions? Juergen |
|
From: Cameron B. <ca...@br...> - 2005-03-14 03:11:50
|
How is this for an idea : Have a configuration option that allows spring to wrap the hibernate session in a class that automatically converts the exceptions into spring exceptions. Cameron. > -----Original Message----- > From: spr...@li... > [mailto:spr...@li...] On Behalf > Of Juergen Hoeller > Sent: Monday, 14 March 2005 7:50 AM > To: spr...@li... > Subject: [Springframework-developer] Hibernate3 entity names, direct > Session access > > We've just received a request for further overloaded methods on > HibernateTemplate: taking Hibernate3 entity names as alternative to Class > arguments, analogous to the Hibernate3 Session. > > http://opensource.atlassian.com/projects/spring/browse/SPR-777 > > As I've replied there, I'm somewhat reluctant to add the overloaded > operations for entity names to HibernateTemplate itself. HibernateTemplate > already has quite a lot of operations on it; I'd like to avoid cluttering > it > with even more overloaded methods. > > Of course, like for all needs that HibernateTemplate's convenience methods > do not cover, implement a custom HibernateCallback and use the Hibernate > Session API natively. With an anonymous inner class, such a callback is > straightforward to implement, but of course more verbose than a one-line > convenience operation call. > > It's worth pointing out that there is an alternative now, enabled by > Hibernate3's use of unchecked exceptions: direct Hibernate Session access > via SessionFactoryUtils.getSession - if you can live with Hibernate3's > unchecked HibernateException's getting thrown to callers. > > If you can guarantee to always execute within a transaction in such a > scenario, you don't even need to call > SessionFactoryUtils.closeSessionIfNecessary, which allows you to write > straightforward direct Session access code. > > public class MyDao extends HibernateDaoSupport { > > public MyObject loadMyObject(String id) throws HibernateException { > return (MyObject) getSession(false).load("myEntityName", id); > } > } > > The advantage of this style is that there is no wrapping of the Hibernate > Session: It's plain Hibernate Session access; the only thing Spring > provides > is a lookup method that returns the Spring-managed transactional Hibernate > Session. > > The disadvantage is, of course, that HibernateExceptions will get thrown > as-is, which means that callers have to be coded against Hibernate- > specific > exceptions when trying to handle them. If those exceptions don't get > handled > explicitly in business facade code, exception mappings in error views are > affected. > > At least callers don't need to declare the formerly checked > HibernateException anymore: If they don't care about exceptions thrown by > the DAO, they can simply let the now (since Hibernate3) unchecked > HibernateException through - without explicitly catching it or declaring > it > in the "throws" clause. > > An interesting question is whether we should always strictly recommend the > HibernateTemplate/DataAccessException approach, even in case of many > custom > HibernateCallbacks. In some applications, people might not care about > generic data access exceptions, so Hibernate3's unchecked exceptions might > be acceptable... > > The same applies to JDO: unchecked JDOExceptions might be acceptable as > DAO > exceptions in some scenarios. Like in the Hibernate3 case, this is only > really interesting if always executing within a transaction, though: else, > DAO implementations would get cluttered with try-finally blocks for > closing > resources. > > Thoughts? Opinions? > > Juergen > > > > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from real users. > Discover which products truly live up to the hype. Start reading now. > http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click > _______________________________________________ > Springframework-developer mailing list > Spr...@li... > https://lists.sourceforge.net/lists/listinfo/springframework-developer |
|
From: Juergen H. <ju...@in...> - 2005-03-14 08:44:19
|
I've already thought of that, actually, but I think it's not feasible in general. We would not only need to proxy the Hibernate Session for this, but also all resources returned by it, such as Query instances and Criteria instances. And then there's the problem of interface contract: If the Hibernate Session interface says that it throws (unchecked) HibernateException, it would arguably be a bit odd to throw (unchecked) DataAccessException - from the contract point of view... Juergen -----Original Message----- From: spr...@li... [mailto:spr...@li...]On Behalf Of Cameron Braid Sent: Monday, March 14, 2005 4:11 AM To: spr...@li... Subject: RE: [Springframework-developer] Hibernate3 entity names, direct Session access How is this for an idea : Have a configuration option that allows spring to wrap the hibernate session in a class that automatically converts the exceptions into spring exceptions. Cameron. > -----Original Message----- > From: spr...@li... > [mailto:spr...@li...] On Behalf > Of Juergen Hoeller > Sent: Monday, 14 March 2005 7:50 AM > To: spr...@li... > Subject: [Springframework-developer] Hibernate3 entity names, direct > Session access > > We've just received a request for further overloaded methods on > HibernateTemplate: taking Hibernate3 entity names as alternative to Class > arguments, analogous to the Hibernate3 Session. > > http://opensource.atlassian.com/projects/spring/browse/SPR-777 > > As I've replied there, I'm somewhat reluctant to add the overloaded > operations for entity names to HibernateTemplate itself. HibernateTemplate > already has quite a lot of operations on it; I'd like to avoid cluttering > it > with even more overloaded methods. > > Of course, like for all needs that HibernateTemplate's convenience methods > do not cover, implement a custom HibernateCallback and use the Hibernate > Session API natively. With an anonymous inner class, such a callback is > straightforward to implement, but of course more verbose than a one-line > convenience operation call. > > It's worth pointing out that there is an alternative now, enabled by > Hibernate3's use of unchecked exceptions: direct Hibernate Session access > via SessionFactoryUtils.getSession - if you can live with Hibernate3's > unchecked HibernateException's getting thrown to callers. > > If you can guarantee to always execute within a transaction in such a > scenario, you don't even need to call > SessionFactoryUtils.closeSessionIfNecessary, which allows you to write > straightforward direct Session access code. > > public class MyDao extends HibernateDaoSupport { > > public MyObject loadMyObject(String id) throws HibernateException { > return (MyObject) getSession(false).load("myEntityName", id); > } > } > > The advantage of this style is that there is no wrapping of the Hibernate > Session: It's plain Hibernate Session access; the only thing Spring > provides > is a lookup method that returns the Spring-managed transactional Hibernate > Session. > > The disadvantage is, of course, that HibernateExceptions will get thrown > as-is, which means that callers have to be coded against Hibernate- > specific > exceptions when trying to handle them. If those exceptions don't get > handled > explicitly in business facade code, exception mappings in error views are > affected. > > At least callers don't need to declare the formerly checked > HibernateException anymore: If they don't care about exceptions thrown by > the DAO, they can simply let the now (since Hibernate3) unchecked > HibernateException through - without explicitly catching it or declaring > it > in the "throws" clause. > > An interesting question is whether we should always strictly recommend the > HibernateTemplate/DataAccessException approach, even in case of many > custom > HibernateCallbacks. In some applications, people might not care about > generic data access exceptions, so Hibernate3's unchecked exceptions might > be acceptable... > > The same applies to JDO: unchecked JDOExceptions might be acceptable as > DAO > exceptions in some scenarios. Like in the Hibernate3 case, this is only > really interesting if always executing within a transaction, though: else, > DAO implementations would get cluttered with try-finally blocks for > closing > resources. > > Thoughts? Opinions? > > Juergen > > > > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from real users. > Discover which products truly live up to the hype. Start reading now. > http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click > _______________________________________________ > Springframework-developer mailing list > Spr...@li... > https://lists.sourceforge.net/lists/listinfo/springframework-developer ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click _______________________________________________ Springframework-developer mailing list Spr...@li... https://lists.sourceforge.net/lists/listinfo/springframework-developer |
|
From: Rod J. <ro...@in...> - 2005-03-14 18:26:03
|
Cameron Braid wrote: > How is this for an idea : > > Have a configuration option that allows spring to wrap the hibernate session > in a class that automatically converts the exceptions into spring > exceptions. I'm afraid I don't like this. It could be done with an aspect with AspectJ or the like, but trying to do it otherwise would be messy. Also the semantics are arguably messy. |
|
From: Colin S. <col...@ex...> - 2005-03-14 04:17:52
|
Juergen Hoeller wrote: >We've just received a request for further overloaded methods on >HibernateTemplate: taking Hibernate3 entity names as alternative to Class >arguments, analogous to the Hibernate3 Session. > >http://opensource.atlassian.com/projects/spring/browse/SPR-777 > >As I've replied there, I'm somewhat reluctant to add the overloaded >operations for entity names to HibernateTemplate itself. HibernateTemplate >already has quite a lot of operations on it; I'd like to avoid cluttering it >with even more overloaded methods. > >Of course, like for all needs that HibernateTemplate's convenience methods >do not cover, implement a custom HibernateCallback and use the Hibernate >Session API natively. With an anonymous inner class, such a callback is >straightforward to implement, but of course more verbose than a one-line >convenience operation call. > >It's worth pointing out that there is an alternative now, enabled by >Hibernate3's use of unchecked exceptions: direct Hibernate Session access >via SessionFactoryUtils.getSession - if you can live with Hibernate3's >unchecked HibernateException's getting thrown to callers. > >If you can guarantee to always execute within a transaction in such a >scenario, you don't even need to call >SessionFactoryUtils.closeSessionIfNecessary, which allows you to write >straightforward direct Session access code. > >public class MyDao extends HibernateDaoSupport { > > public MyObject loadMyObject(String id) throws HibernateException { > return (MyObject) getSession(false).load("myEntityName", id); > } >} > >The advantage of this style is that there is no wrapping of the Hibernate >Session: It's plain Hibernate Session access; the only thing Spring provides >is a lookup method that returns the Spring-managed transactional Hibernate >Session. > >The disadvantage is, of course, that HibernateExceptions will get thrown >as-is, which means that callers have to be coded against Hibernate-specific >exceptions when trying to handle them. If those exceptions don't get handled >explicitly in business facade code, exception mappings in error views are >affected. > >At least callers don't need to declare the formerly checked >HibernateException anymore: If they don't care about exceptions thrown by >the DAO, they can simply let the now (since Hibernate3) unchecked >HibernateException through - without explicitly catching it or declaring it >in the "throws" clause. > >An interesting question is whether we should always strictly recommend the >HibernateTemplate/DataAccessException approach, even in case of many custom >HibernateCallbacks. In some applications, people might not care about >generic data access exceptions, so Hibernate3's unchecked exceptions might >be acceptable... > >The same applies to JDO: unchecked JDOExceptions might be acceptable as DAO >exceptions in some scenarios. Like in the Hibernate3 case, this is only >really interesting if always executing within a transaction, though: else, >DAO implementations would get cluttered with try-finally blocks for closing >resources. > >Thoughts? Opinions? > >Juergen > > I think the decision on whether to add the new methods should not be based oin whether there are already too many methods, but rather on how often the new ones would be used. If the new Hibernate3 Session interface methods are going to be used a lot by the average yuser (as much as the class based ones) then they probably belong. On the other hand, if they are going to be used a lot less, then the callback is ok (IMHO). But just because they're new shouldn't be the real criteria... |
|
From: Juergen H. <ju...@in...> - 2005-03-14 08:50:10
|
Good point, the criteria should be how often they are likely to be used, not that they are new. Agreed on that. So, how often are they likely to be used? :-) What's your opinion on direct Session access, Colin? The same option exists not only with JDO, but also with the forthcoming EJB3 EntityManager: All those could be accessed directly if one accepts the non-generic (but at least unchecked) data access exceptions getting thrown. Spring would essentially just provide DAO wiring and transaction management here, which some people might like: no wrapping of the perfectly good Hibernate (or JDO or EntityManager) API going on there ;-) Juergen -----Original Message----- From: spr...@li... [mailto:spr...@li...]On Behalf Of Colin Sampaleanu Sent: Monday, March 14, 2005 5:17 AM To: spr...@li... Subject: Re: [Springframework-developer] Hibernate3 entity names, direct Session access Juergen Hoeller wrote: >We've just received a request for further overloaded methods on >HibernateTemplate: taking Hibernate3 entity names as alternative to Class >arguments, analogous to the Hibernate3 Session. > >http://opensource.atlassian.com/projects/spring/browse/SPR-777 > >As I've replied there, I'm somewhat reluctant to add the overloaded >operations for entity names to HibernateTemplate itself. HibernateTemplate >already has quite a lot of operations on it; I'd like to avoid cluttering it >with even more overloaded methods. > >Of course, like for all needs that HibernateTemplate's convenience methods >do not cover, implement a custom HibernateCallback and use the Hibernate >Session API natively. With an anonymous inner class, such a callback is >straightforward to implement, but of course more verbose than a one-line >convenience operation call. > >It's worth pointing out that there is an alternative now, enabled by >Hibernate3's use of unchecked exceptions: direct Hibernate Session access >via SessionFactoryUtils.getSession - if you can live with Hibernate3's >unchecked HibernateException's getting thrown to callers. > >If you can guarantee to always execute within a transaction in such a >scenario, you don't even need to call >SessionFactoryUtils.closeSessionIfNecessary, which allows you to write >straightforward direct Session access code. > >public class MyDao extends HibernateDaoSupport { > > public MyObject loadMyObject(String id) throws HibernateException { > return (MyObject) getSession(false).load("myEntityName", id); > } >} > >The advantage of this style is that there is no wrapping of the Hibernate >Session: It's plain Hibernate Session access; the only thing Spring provides >is a lookup method that returns the Spring-managed transactional Hibernate >Session. > >The disadvantage is, of course, that HibernateExceptions will get thrown >as-is, which means that callers have to be coded against Hibernate-specific >exceptions when trying to handle them. If those exceptions don't get handled >explicitly in business facade code, exception mappings in error views are >affected. > >At least callers don't need to declare the formerly checked >HibernateException anymore: If they don't care about exceptions thrown by >the DAO, they can simply let the now (since Hibernate3) unchecked >HibernateException through - without explicitly catching it or declaring it >in the "throws" clause. > >An interesting question is whether we should always strictly recommend the >HibernateTemplate/DataAccessException approach, even in case of many custom >HibernateCallbacks. In some applications, people might not care about >generic data access exceptions, so Hibernate3's unchecked exceptions might >be acceptable... > >The same applies to JDO: unchecked JDOExceptions might be acceptable as DAO >exceptions in some scenarios. Like in the Hibernate3 case, this is only >really interesting if always executing within a transaction, though: else, >DAO implementations would get cluttered with try-finally blocks for closing >resources. > >Thoughts? Opinions? > >Juergen > > I think the decision on whether to add the new methods should not be based oin whether there are already too many methods, but rather on how often the new ones would be used. If the new Hibernate3 Session interface methods are going to be used a lot by the average yuser (as much as the class based ones) then they probably belong. On the other hand, if they are going to be used a lot less, then the callback is ok (IMHO). But just because they're new shouldn't be the real criteria... ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click _______________________________________________ Springframework-developer mailing list Spr...@li... https://lists.sourceforge.net/lists/listinfo/springframework-developer |
|
From: Rod J. <ro...@in...> - 2005-03-14 18:27:44
|
> Good point, the criteria should be how often they are likely to be used, not > that they are new. Agreed on that. So, how often are they likely to be used? > :-) I think these new methods are potentially useful, and an important enhancement in Hibernate 3 which would allow advising Hibernate persistent objects etc. So I would favour adding them. > What's your opinion on direct Session access, Colin? The same option exists > not only with JDO, but also with the forthcoming EJB3 EntityManager: All > those could be accessed directly if one accepts the non-generic (but at > least unchecked) data access exceptions getting thrown. Spring would > essentially just provide DAO wiring and transaction management here, which > some people might like: no wrapping of the perfectly good Hibernate (or JDO > or EntityManager) API going on there ;-) I think exception translation is very important to the value proposition. I don't think we should encourage raw session access. Exception translation is a very important value add. R |
|
From: Artur K. <kar...@as...> - 2005-03-14 18:53:33
|
Colin Sampaleanu wrote: > I think the decision on whether to add the new methods should not be > based oin whether there are already too many methods, but rather on how > often the new ones would be used. If the new Hibernate3 Session > interface methods are going to be used a lot by the average yuser (as > much as the class based ones) then they probably belong. On the other > hand, if they are going to be used a lot less, then the callback is ok > (IMHO). But just because they're new shouldn't be the real criteria... Could not agree more. And, from my experience, HB team doesn't add features when they don't have quite *huge* user demand - frankly speaking :))). Is just me who want generic class like Address map in few different scenarios :)))? Hope not :). Artur -- ,,Wine, jako emulator Windows...'' -- WO, pcoa ,,As Wine's name says: "Wine Is Not an Emulator"'' -- WineHQ, FAQ |
|
From: Artur K. <kar...@as...> - 2005-03-14 18:46:49
|
Juergen Hoeller wrote: > We've just received a request for further overloaded methods on > HibernateTemplate: taking Hibernate3 entity names as alternative to Class > arguments, analogous to the Hibernate3 Session. > > http://opensource.atlassian.com/projects/spring/browse/SPR-777 > > As I've replied there, I'm somewhat reluctant to add the overloaded > operations for entity names to HibernateTemplate itself. HibernateTemplate > already has quite a lot of operations on it; I'd like to avoid cluttering > it with even more overloaded methods. It's 15 new methods - that's right. But these are very usefull methods. Please add these methods just for sake of consistency. I think, in two years, when HB3+ becomes mainstream and people will be using new features, there will be a lot of questions about that. Don't You think? > Of course, like for all needs that HibernateTemplate's convenience methods > do not cover, implement a custom HibernateCallback and use the Hibernate > Session API natively. With an anonymous inner class, such a callback is > straightforward to implement, but of course more verbose than a one-line > convenience operation call. That's exactly my point. It's relativelly inconvinient and verbose. Don't get me wrong, but - 'there is a lot of methods in HibernateTemplate already' - is not good excuse ;). > It's worth pointing out that there is an alternative now, enabled by > Hibernate3's use of unchecked exceptions: direct Hibernate Session access > via SessionFactoryUtils.getSession - if you can live with Hibernate3's > unchecked HibernateException's getting thrown to callers. But as You say - You have to deal with hibernate exceptions. It's not easy nor elegant. Users love Spring because it wraps these exceptions ang giving them better, meaningfull versions. And You can easly switch from HB to JDO without rewriting Your exception handling code. > If you can guarantee to always execute within a transaction in such a > scenario, you don't even need to call > SessionFactoryUtils.closeSessionIfNecessary, which allows you to write > straightforward direct Session access code. > > public class MyDao extends HibernateDaoSupport { > > public MyObject loadMyObject(String id) throws HibernateException { > return (MyObject) getSession(false).load("myEntityName", id); > } > } > > The advantage of this style is that there is no wrapping of the Hibernate > Session: It's plain Hibernate Session access; the only thing Spring > provides is a lookup method that returns the Spring-managed transactional > Hibernate Session. > > The disadvantage is, of course, that HibernateExceptions will get thrown > as-is, which means that callers have to be coded against > Hibernate-specific exceptions when trying to handle them. If those > exceptions don't get handled explicitly in business facade code, exception > mappings in error views are affected. And that's really big disadvantage. > The same applies to JDO: unchecked JDOExceptions might be acceptable as > DAO exceptions in some scenarios. Like in the Hibernate3 case, this is > only really interesting if always executing within a transaction, though: > else, DAO implementations would get cluttered with try-finally blocks for > closing resources. > > Thoughts? Opinions? IMHO You should add these methods just for sake of consistency. Artur -- ,,Wine, jako emulator Windows...'' -- WO, pcoa ,,As Wine's name says: "Wine Is Not an Emulator"'' -- WineHQ, FAQ |
|
From: Rod J. <ro...@in...> - 2005-03-14 18:57:56
|
> It's 15 new methods - that's right. But these are very usefull methods. > Please add these methods just for sake of consistency. I think, in two > years, when HB3+ becomes mainstream and people will be using new features, > there will be a lot of questions about that. Don't You think? I agree with Artur. |
|
From: Juergen H. <ju...@in...> - 2005-03-14 22:47:26
|
OK, I've added all overloaded methods for entity names to HibernateTemplate. I agree that it makes sense for the long term. I've also added them to the HibernateOperations interface, as there is no way to achieve the same call through the already existing interface methods. As a counterbalance, I've removed all methods that take Hibernate Type arguments from HibernateOperations and HibernateTemplate (only for the hibernate3 package). In my experience and from what I've seen in customer code, those are hardly ever used, or at least hardly ever necessary: Hibernate is usually able to guess the type correctly. We mainly had those methods with Type arguments in HibernateTemplate because the Hibernate 2.1 Session interface had them too. In Hibernate 3.0, those have been removed (that is, moved to the deprecated org.hibernate.classic.Session interface). I consider this a good occasion to not offer those methods in our hibernate3 package in the first place. If you need to set a specific Hibernate Type for a parameter, simply use a HibernateCallback and work with the Hibernate Query API. This is much nicer than those endless array arguments for one-line find methods. After all, one-line convenience methods are just nice for a limited number of arguments. Juergen -----Original Message----- From: spr...@li... [mailto:spr...@li...]On Behalf Of Rod Johnson Sent: Monday, March 14, 2005 7:58 PM To: spr...@li... Subject: Re: [Springframework-developer] Re: Hibernate3 entity names, direct Session access > It's 15 new methods - that's right. But these are very usefull methods. > Please add these methods just for sake of consistency. I think, in two > years, when HB3+ becomes mainstream and people will be using new features, > there will be a lot of questions about that. Don't You think? I agree with Artur. ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click _______________________________________________ Springframework-developer mailing list Spr...@li... https://lists.sourceforge.net/lists/listinfo/springframework-developer |
|
From: Keith D. <ke...@in...> - 2005-03-14 19:02:10
|
Just a general my 2c comment on this: In general, I think adding convenience methods to _implementations_ won't introduce confusion so long as those methods add value in the typical cases and are clearly documented. I think if anything they'll actually help promote usage consistency, as mentioned. On the other hand, I think adding a bunch of convenience methods to an interface is typically not a good thing (which would be HibernateOperations in this case?), as it places unnecessary-brittle burden on implementers. We've seen this issue addressed elegantly in other parts of Spring. Take the MessageSource interface, for example. MessageSource is base interface that may be easily wrapped in a MessageSourceAccessor convenience accessor implementation. From there usage is a snap, and MessageSource implementations don't have to change. I recently had to deal with this same issue in web.flow and adopted a similar strategy (simple interface, rich convenience wrapper), which I thought worked well. Keith -----Original Message----- From: spr...@li... [mailto:spr...@li...] On Behalf Of Artur Karazniewicz Sent: Monday, March 14, 2005 1:08 PM To: spr...@li... Subject: [Springframework-developer] Re: Hibernate3 entity names, direct Session access Juergen Hoeller wrote: > We've just received a request for further overloaded methods on > HibernateTemplate: taking Hibernate3 entity names as alternative to Class > arguments, analogous to the Hibernate3 Session. > > http://opensource.atlassian.com/projects/spring/browse/SPR-777 > > As I've replied there, I'm somewhat reluctant to add the overloaded > operations for entity names to HibernateTemplate itself. HibernateTemplate > already has quite a lot of operations on it; I'd like to avoid cluttering > it with even more overloaded methods. It's 15 new methods - that's right. But these are very usefull methods. Please add these methods just for sake of consistency. I think, in two years, when HB3+ becomes mainstream and people will be using new features, there will be a lot of questions about that. Don't You think? > Of course, like for all needs that HibernateTemplate's convenience methods > do not cover, implement a custom HibernateCallback and use the Hibernate > Session API natively. With an anonymous inner class, such a callback is > straightforward to implement, but of course more verbose than a one-line > convenience operation call. That's exactly my point. It's relativelly inconvinient and verbose. Don't get me wrong, but - 'there is a lot of methods in HibernateTemplate already' - is not good excuse ;). > It's worth pointing out that there is an alternative now, enabled by > Hibernate3's use of unchecked exceptions: direct Hibernate Session access > via SessionFactoryUtils.getSession - if you can live with Hibernate3's > unchecked HibernateException's getting thrown to callers. But as You say - You have to deal with hibernate exceptions. It's not easy nor elegant. Users love Spring because it wraps these exceptions ang giving them better, meaningfull versions. And You can easly switch from HB to JDO without rewriting Your exception handling code. > If you can guarantee to always execute within a transaction in such a > scenario, you don't even need to call > SessionFactoryUtils.closeSessionIfNecessary, which allows you to write > straightforward direct Session access code. > > public class MyDao extends HibernateDaoSupport { > > public MyObject loadMyObject(String id) throws HibernateException { > return (MyObject) getSession(false).load("myEntityName", id); > } > } > > The advantage of this style is that there is no wrapping of the Hibernate > Session: It's plain Hibernate Session access; the only thing Spring > provides is a lookup method that returns the Spring-managed transactional > Hibernate Session. > > The disadvantage is, of course, that HibernateExceptions will get thrown > as-is, which means that callers have to be coded against > Hibernate-specific exceptions when trying to handle them. If those > exceptions don't get handled explicitly in business facade code, exception > mappings in error views are affected. And that's really big disadvantage. > The same applies to JDO: unchecked JDOExceptions might be acceptable as > DAO exceptions in some scenarios. Like in the Hibernate3 case, this is > only really interesting if always executing within a transaction, though: > else, DAO implementations would get cluttered with try-finally blocks for > closing resources. > > Thoughts? Opinions? IMHO You should add these methods just for sake of consistency. Artur -- ,,Wine, jako emulator Windows...'' -- WO, pcoa ,,As Wine's name says: "Wine Is Not an Emulator"'' -- WineHQ, FAQ ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click _______________________________________________ Springframework-developer mailing list Spr...@li... https://lists.sourceforge.net/lists/listinfo/springframework-developer |