Friday, January 25, 2008

Hibernate Collections Lazy Initialization Gotcha

I have some code that looks like this:

AdContent.java (EJB3):
@CollectionOfElements
@JoinTable(name="AdContentSites",
joinColumns={@JoinColumn(name="adContentId")})
@Enumerated(EnumType.STRING)
@Column(name="siteName", nullable=false)
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
Set siteNames;
public Set getSiteNames() { return this.siteNames; }

This creates a join table called AdContentSites which maps an AdContent object to one or more SiteName's. SiteName is an enum. I then transform AdContent into a POJO called AdContentData and make that available to the web tier.

This is the transform method:
public static AdContentData adContent(AdContent raw)
{
// Need to lazily initialize the collection
raw.getSiteNames().size();

AdContentData adc = new AdContentData(
raw.getId(),
raw.getName(),
raw.getContent(),
raw.getSiteName(),
raw.getStart(),
raw.getEnd(),
raw.getCreatedBy(),
raw.getCreated());
return adc;
}

The part that bit me in the ass for a while was the fact that Hibernate lazily populates Collections. I kept getting the less than helpful error: "failed to lazily initialize a collection of role: com.kink.heart.entity.adn.AdContent.siteNames, no session or session was closed"

The solution (as you can see above) was to force Hibernate to initialize the Collection by calling AdContent.getSiteName().size(). Maybe it is just me, but I would have expected the call further down to raw.getSiteNames() to do that work for me, but I guess because of the way the object proxy works, that isn't what is going to happen.

Saturday, January 19, 2008

[JOB] Kink.com is hiring!

Dear Readers,

Kink.com is looking to expand our IT department with 7 new positions. 4 of those are software engineers (preferably Java). We are very profitable, growing the company like mad and need talented folks to work on really interesting and fun projects.

The company is great to work for, pays well, 401k, health benefits, 30" apple monitors, new fast computers, lots of free lunches, all the porn you want (not safe for work, NSFW, doesn't apply for us), etc. I'll even argue that we have the best reputation in the industry for how we treat everyone who works for us (check out the videos on our news site http://BehindKink.com/). We also just moved our offices into a massive historical building right in the heart of San Francisco 5 minutes from the 16th St. BART station called the Armory (http://sfarmory.com/).

A real benefit to working here is that you will be able to put on your resume that you have worked on some of the most cutting edge web technologies out there for very high traffic sites (CDN's, J2EE, Hibernate, EJB3, HD-video, etc). We have built and deployed a brand new Java based platform for all of our sites that has a well written build system, great overall design, unit tests (cruisecontrol), security, etc. It is a really fun and interesting engineering environment to work in with a lot of technical challenges.

I'll be honest, we are having a really hard time finding talented individuals because of the nature of the business. Sadly, some people feel they can't tell their family or they worry it will tarnish their resume. We are working on ways around that issue by forming sister a company that isn't related to porn that you can use on your resume and tell your family about.

If you know anyone who might be interested in working at Kink.com, please forward this message to them and send them my way as soon as possible. There are two job postings on the website right now for the Java engineers and more will be posted soon I'm sure. http://kink.com/jobs.php Sorry, no telecommuting.

Tuesday, January 15, 2008

Homeless in San Francisco go hi-tech

According to this article...

"Microsoft Corp. (MSFT) (MSFT) is bringing digital advertising to the grocery cart. The software maker spent four years working with Plano, Texas-based MediaCart Holdings Inc. on a grocery cart-mounted console that helps shoppers find products in the store, then scan and pay for their items without waiting in the checkout line."

Not only will your shopping cart reboot with blue screens, but the homeless will now have the worlds most expensive shopping carts to live out of. I heard once that a high percentage of the markup on grocery store goods is a result of all of the stolen grocery carts. Those things are expensive!

When I was in Holland, they had a better idea. It was a hand held unit. You picked it up on the way in from a wall tree of units and as you walked around the store you simply scanned each item as you put it in the cart. When you were done, you docked the unit again, put your groceries into the bags that you brought with you (another earth saving feature) and just walked out of the store. There wasn't even anyone checking to make sure you didn't 'forget' to scan something. The Dutch are so trusting.

Someone with their head on backwards also thinks this is a good idea that people will use:

"Customers with a ShopRite loyalty card will be able to log into a Web site at home and type in their grocery lists; when they get to the store and swipe their card on the MediaCart console, the list will appear."

What a joke, who the heck is going to build grocery lists at home via a web browser? Sure, I do it for my expensive Planet Organics orders, but that is because I don't have a choice (it is their only option and I think that most people just go with their default options). Doesn't anyone realize that 99% of the people out there buy their groceries based on all of that market research that grocery stores do with regards to product placement? The chips are next to the dip for a reason. Websites will never be able to simulate that correctly. I can't imagine grandma or even my father using this system at all.

Oh, I just found this great little article on Snopes.com. Apparently, people didn't like the shopping carts when they were first invented. "The answer was a resounding no — folks preferred to stick with what they knew rather than play around with an unusual-looking contraption..."

Sunday, January 13, 2008

Review: Eye-Fi Wireless 2 GB SD Memory Card

I recently purchased an Eye-Fi Wireless 2 GB SD Memory Card for my camera. I love it! Previously my workflow was that I would plug my camera into my laptop, download the images using iPhoto and the use the Flickr Export Plugin to upload them to my Flickr account. What a pain in the ass. Not only did it waste disk space on my laptop, but it was just too complicated to manage.

Now, I never have to plug my camera into my computer anymore. I just leave the camera on while at home and the images wirelessly transfer to my Flickr account. I can even use the Eye-Fi manager to watch the files being uploaded. The only thing that kind of bothers me is that the Eye-Fi Manager stores a small thumbnail of your images on the Eye-Fi servers and there is no way to clear your images off their servers. So, if you take some naked pictures of your girlfriend, you might want to switch back to a regular card.

Update: Eye-Fi fixes their system to allow you to go straight to iPhoto. Not really sure if this is a good thing for me or not. I'd rather skip iPhoto all together and just go straight to Flickr.

I bought mine from Amazon. You can click the link below to get yours!