Learning Hibernate & JPA

This is somewhat of a discovery project. JPA and Hibernate are a very different way of interfacing with data storage and I’m having to learn a lot and reconcile what I knew in order to do things in the “best way.” I am probably still not doing things in the best fashion, but I’m trying to.

An example would be what to do when, say, a league’s schedule changes drastically. A matchup is really given by the week, and the key of that week is really the start/end date. What if the league manager decides to suddenly have 2-week matchups? Those new weeks will be completely different, so we really need to purge the old and insert the new. Figuring out how to do this properly with JPA and Hibernate wasn’t trivial.

This commit is really where I figured it out… where I begin to stop worrying about the database and start worrying about my domain objects. I don’t have to delete manually via HQL; that would potentially leave the session in a pretty rough state. No, I should manipulate the objects and what they reference instead and let Hibernate sort out the details.

Another epiphany was when dealing with foreign keys. Inside the domain object, I was sticking the parent ID itself in there. To get Hibernate to do that automatically, I had to finagle it a bit with making the column nullable, adding a @JoinColumn annotation to the parent, etc. It just felt hackish to me. Then, after some Googling, I figured out that Hibernate will deal with it if you put the parent object inside the child. It will automatically insert into a foreign key column in the child table. Nice! Of course, you have to make sure Jackson doesn’t choke on the circular reference, but Jackson is easy to deal with.

It’s funny. The commit involved for that particular epiphant is pretty minimal, but took hours to figure out the right combination of annotations. Often with programming, volume is irrelevant. I recall once in an interview at a career fair many years ago, someone asked me how many lines of code I can write per day… as if it mattered. Looking back on that, I realize that person is probably a terrible project leader/manager and I’m glad they didn’t want to hire me. I believe they owned the company that owns the program FlashFXP… perhaps OpenSight Software? Well, it seems all they have is still FlashFXP. Unsurprising.

Anyway, I’m learning, slowly but surely. As I learn, this project continues to come together. I still can’t believe I have momentum on all of this; I usually get disinterested by now.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: