Posts for Tag: All

Spring and hibernate- Lazy loading collections in desktop (swing) applications

So here's something you can't find on the internets. Or at least i couldn't, when i was trying to find a way to do this.

The problem:

Traditionally, spring and hibernate have been mostly used in web applications. In this case, you can easily define your domain objects to have lazy collections because spring/hibernate will keep an open session during the whole handling of each web request. This means that you can access lazy loading objects even in your views (provided you use the open session in view filter ;)).

Now, in desktop applications you usually don't go around having open sessions all the time which means that, unfortunately, when you try to get a lazy loading collection, hibernate will give you a big finger in the form of a LazyInitializationException due to the fact of no open sessions existing.

One solution:

My application's structure is such that no UI classes are being managed by spring (and by this i mean your mileage may vary with this solution), only my services and data access objects (DAOs) are. The UI accesses the services by use of a ServiceLocator pattern (which can be more or less complex, depending on your needs) and the DAOs in the services are autowired. The rest is just spring plumbing.

The way i found more elegant to deal with the lazy loading problem is the following. Take this bean (for the sake of simplicity some annotations or methods might be missing, beware): @Entity public class MyBean { @Column(name = "NAME") private String name;

@CollectionOfElements(fetch = FetchType.LAZY) private Set<AnotherBean> children;

public Set<AnotherBean> getChildren() { return children; } } If you try to access getChildren() hibernate will complain so i changed the method to this: public Set<AnotherBean> getChildren() {

children = DBUtils.initializeIfNeeded(this, children); return children; } And here's the definition of that static method: public static <T> Set<T> initializeIfNeeded(Object obj, Set<T> collection) {

if (!(collection instanceof PersistentCollection)) {

return Sets.newHashSet(); }

if (!((PersistentCollection) collection).wasInitialized()) {

ServiceLocator.get(CompanyDA.class).initializeLazyCollection(obj, collection); }

return collection != null ? collection : Sets.<T>newHashSet(); } What we are doing is checking whether this collection is a instance of hibernate's PersistentCollection and, if not, getting out. If it is, and hasn't been initialized, we call a special method in the super of all my DAO's that is responsible for asking hibernate to initialize the collection. Take into account that this method has to be inside a spring managed bean (i.e. non static) otherwise it won't be able to open a new session and you're back to square one.

Here's the definition of that method in the DAO: public void initializeLazyCollection(Object obj, Collection<? extends Object> collection) { sessionFactory.getCurrentSession().lock(obj, LockMode.NONE); new HibernateTemplate(sessionFactory).initialize(collection); } Hope this helps anyone and if you know of a better way to do the same thing, please share :)

Peace out.

Google instant is pretty awesome

[caption id="attachment_1122" align="alignright" width="300" caption="Google Instant results"][/caption]

In case you missed the Google event, they are rolling out a new feature called "Instant". It basically shows you search results as you type the query in the search box.

Currently i could only have it working on Chrome at www.google.com (can't be any other suffix, like co.uk, pt, etc.) but you definitely need to see it at work to appreciate it. It makes it much easier to find what you are looking for when you can change the results on the fly, if you are not happy with what you got :)

They still call it Blue Screen

After a long, long time without crashes on my windows box i finally got this one while watching a movie on vimeo.com. It seems they call the crash event "Blue Screen". Got love the nostalgia on the windows developers :D

Isn't music very similar?

One thing that both annoys and amazes me is the limited number of chord combinations in most songs. It's extremely frequent to hear a musical composition and get that feeling that you heard it before. And we are probably right most of the times.

In the last AC/DC album there is a song entitled "Big Jack". As soon as i heard it i immediately recalled another song by a very polemic Portuguese singer from the the 80s: António Variações (look up his songs, they were pretty innovative back then, actually).

I still trying to think why i am writing this but guess that i can't help wondering, first, why aren't there any more musical notes (i guess i should look that up) and, second, why most bands don't explore more and keep falling to the same chord combinations all the time. The easy explanation is the amount of money that you can make with little work on a catchy pop song, but i feel sad if that's the only one.

Feedsquares- A refreshing view for Google Reader

I don't know about you, but i have still to find the perfect RSS reader. I don't dislike Google Reader, but i always feel that things can be better (yeah, it's both a curse and a blessing) and, recently, i found a little thing - called Feedsquares - that puts a nice twist on its interface, making really non-distracting the act of reading each piece of news.

[caption id="attachment_1017" align="alignright" width="425" caption="Feedsquares looks nice"][/caption]

It is comprised of a bunch of squares (one per feed you have) and, when clicked, it will show yet more squares; this time the stories in that feed. Clicking those will open the story full page. Check it out, you might like it :)