Oct 29 2007

Quick Tip: Using Spring and Hibernate Annotations

Tag: hibernate, java, jpa, springpmularien @ 10:01 am

Hibernate Annotations provides a complete implementation of EJB 3 / JPA-compatible annotations. It is possible to use Hibernate annotations with the native Hibernate API. You use Hibernate annotated classes in the same way that you would use XML mapping files. This is a great strategy if you’d like to prepare for a transition to JPA, but aren’t quite ready due to some of the known deficiencies in the API.

It is also possible to tie the annotated classes (and Hibernate) together with Spring’s transaction management. Unfortunately, the Spring documentation on ORM using Hibernate doesn’t cover how to do this. I’ll show you how, below.

To use annotated classes instead of XML mapping files, simply reference Hibernate like so in your Spring XML configuration file (note this requires Spring 2.0+) (line breaks denoted with »):

&lt;bean id="sessionFactory" class="<strong>org.springframework.orm.hibernate3. » annotation.AnnotationSessionFactoryBean</strong>"&gt; &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt; &lt;property name="annotatedClasses"&gt; &lt;list&gt; &lt;value&gt;com.foo.Employee&lt;/value&gt; ...

In describing this, I assume that you’ve already got your basic Spring and Hibernate configuration working (there are plenty of great examples available if you don’t).

And that’s basically it - all the classes listed in “annotatedClasses” need to have JPA-compatible annotations (@Entity or @MappedSuperclass). This allows you to avoid heavy XML mappings and take one step closer to JPA / Java EE 5.


Oct 26 2007

Eclipse 3.3 Quick Fix / Quick Assist Cheat Sheets

Tag: eclipse, javapmularien @ 2:37 pm

I used to be an IntelliJ IDEA user, but have migrated over to Eclipse. One of the things that I initially missed in Eclipse was the availability of the “light bulb” tips to do quick refactors and code optimizations. I have recently discovered that Eclipse has many of the same fixes, but they aren’t advertised as well.

Eclipse differentiates between two types of “quick” tips.

“Quick Fixes” are used when something is actually broken, and Eclipse can provide one (or more) solutions for you, which it can apply to your code. I was able to find a handy list of all the available fixes on the Eclipse site:

http://help.eclipse.org/help33/topic/org.eclipse.jdt.doc.user/concepts/cquickfix.htm

These are great, and I have found that with most basic Java development issues, there’s an appropriate quick fix to solve the issue.

“Quick Assists” are minor refactorings that Eclipse can perform on your behalf. This was the IDEA feature that I missed most when coming over to Eclipse. Here’s the reference list:

http://help.eclipse.org/help33/topic/org.eclipse.jdt.doc.user/concepts/cquickassists.htm

One difference (IIRC) is that Eclipse doesn’t determine which quick assists may be appropriate given your current cursor position - it requires that you select an expression or block in order to figure out which quick assists are relevant.

For those keyboard junkies like myself, a couple notes may be helpful. I remapped (IMO horrible) default key shortcut for Quick Fix and Quick Assist (Ctrl+1) to Ctrl+Enter. This makes it much easier to activate these quick changes without hand gyrations (especially on a laptop keyboard).

Second little helpful tidbit is Alt+Shift+Up (try pressing this repeatedly) to select increasingly higher-scoped compilation units (you kind of have to see this to know how it works).

Hopefully this helps you get something more out of Eclipse, especially if you use it every day!