Ramblings Of a Madman

Yeah, like i would know a good description…

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 :)

A new kind of bandages?

[caption id="attachment_1007" align="alignleft" width="200" caption="A normal bandage"][/caption]

I hate bandages. I really do. Everyone knows the best way for a wound to heal is to leave it exposed to oxygen and, hence, a bandage is the worst thing you can do to it.

A couple of days ago i took a big crash while going downhill on my bike (for gore lovers, check out the picture of my arm) and ended up with my right shoulder and elbow completely scratched. The elbow is a no brainer in the summer; since i use a t-shirt all day, it can just go around with Betadine on it and heal normally. The problem is the shoulder. Since i don't usually like to use tank tops in my everyday life, it would be hard for my clothes not to get soaked in a mixture of blood and Bepanthene/Betadine.

Now, here's an idea: why not create a bandage that is open everywhere, except in its boundaries - so that the wound can normally "breathe" and at the same time not be in contact with the clothes?

[caption id="attachment_1008" align="alignright" width="400" caption="The bandage i needed"][/caption]

I kind had to pull a Mcggyver (check the last entry) and improvise one this morning. It works marvelously. I think i should start selling these things :)

Good blogging desktop client for mac (osx)

When i think about my mind's typical excuses not to write on this blog, the one that pops up the fastest is usually the lack of a good blogging desktop client for mac.

I know, i know, i should just shut the hell up, use Wordpress' web admin interface, and quit whining. But i have a big problem when it comes to writing: i'm lazy. Anything that takes more than a few clicks in order to start writing will make me want to fire up my desktop PC and play something. I once bought a desktop blogging client called Ecto (which i still use, from times to times), but i think it's no longer maintained properly since illumineX acquired it from it's maker - Adriaaan Tijsseling - in the spring of 2008. And to be honest it never felt quite awesome, complaining all the time about the malformed-ness of my youtube embedded code.

So i was wondering if any of you - the 2 people that actually read my blog - know about a nicer (WP) desktop client for the mac.

In other - completely unrelated - news: I have my own parachute rig now. Here are some pics:

DSCF0866 by nocivus

DSCF0867 by nocivus

Irony of searches and one iPad visit to my blog

Just a couple of pictures this time.

The first one is about something i noticed i was doing, while searching for a code fragment. One could call it ironic, trying to find "find" :)

The other is proof of my amazement by the fact that there was one person who visited my blog using an iPad :D

UPDATE: Turns out i was looking at a 2 day period only. This month there have been actually 4 iPad visits, hehe :)