views:

84

answers:

1

I'm looking for a way to use aspects to inject parameters in hibernate annotated classes.

Here is a user :

 @Entity
 public class User implements IHasCity {

     @Id
     private int id;

     private String name;

}

public interface IHasCity {

}

Here is an aspect contributing to that User

public aspect ACity {

@Column
private String IHasCity.cityName;

private String IHasCity.getCityName(){
 return this.cityName;
}

}

Now i'd like to make an request :

If i'm doing like :

"from User" i have no problems to do like result.getCityName().

If i do "from User where cityName=?" it doesn't work ...

It is due to aspect waving. instead of associating my private String cityName to "cityName" in hibernate, it associates it to the unusable intertype name : something like class$ajc$intertype$interface$cityName.

Do you have a way around this ? changing hql attributes names associations ?

Thank you very much !

A: 

The name IHasCity.cityName is gonna be mangled by aspectj bytecode weaver into something like : ajc$interField$interface$cityName.

When you request hibernate you need to take take into account. I guess you could create a helper to do so :

session.createQuery("from User u where u." + AspectHelper.mangle(ACity.class,IHasCity.class,"cityName") + "=:cityName").setParameter(":cityName",...etc).

They are looking into name mangling strategies at AspectJ in order to give an option to avoid that trick. See that thread

christian