Tuesday, August 31, 2010

More Typesafe JPA2 mappedBy

I've been eating my own dog food lately, reworking some of my client projects to use the annotation processor suggested by Dan Allen. It's been working out nicely, allowing me to refactor...

public class Person {

   public String getName() { ... }

   @UiComesAfter( "name" )
   public Set<Address> getAddresses() { ... }
}

...into...

public class Person {

   public String getName() { ... }

   @UiComesAfter( Person_.name )
   public Set<Address> getAddresses() { ... }
}

But today I realised it has another cool side effect. I can refactor...

public class Person {

   public String getName() { ... }

   @UiComesAfter( Person_.name )
   @OneToMany( mappedBy = "person" )
   public Set<Address> getAddresses() { ... }
}

...into...

public class Person {

   public String getName() { ... }

   @UiComesAfter( Person_.name )
   @OneToMany( mappedBy = Address_.person )
   public Set<Address> getAddresses() { ... }
}

...giving me more typesafe (well, typo-safe :) JPA2 annotations! The existing JPA2 metamodel, being designed mainly for Criteria queries, doesn't seem to support this yet. But I don't see why it couldn't be added for a future release?

Comments welcome!

0 comments: