0

E.g. I have:

  @Column(name = "username")
  private String m_username;

Note that the @Column annotation only affects the database column name.

Hibernate still thinks the name of the property is 'm_username'.

How can I tell Hibernate that the property name is just 'username'?

Please tell me there is a way to do this...

Edit: I removed the @AccessType annotation in my code example, as it is not relevant for this question.

Update: After switching everything to field access, this exception happens:

org.hibernate.QueryException: could not resolve property: username of: mypackage.model.User

It happens here:

Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.eq("username", username));
User result = (User) criteria.uniqueResult();

And the reason is most likely that hibernate only 'knows' of a property called 'm_username', while I think of it and program against a property named 'username'. Also note that my getters/setters are called: "getUsername()" and "setUsername(String value)" (automatically generated).

Reto Höhener
  • 5,419
  • 4
  • 39
  • 79

3 Answers3

0

why do you use AccessType.PROPERTY?

remove it and it is accessed by 'field'.

do not mix field and property annotations. stick to one or the other.

quartzde
  • 598
  • 2
  • 8
  • That's because I want access type property, but prefer to specify the annotations on the fields. The getters/setter are generated automatically (see my other question: http://stackoverflow.com/questions/14915606/how-can-i-place-the-hibernate-annotations-on-the-fields-but-always-have-propert). – Reto Höhener Feb 16 '13 at 23:17
  • @Zalumon why can't you use field access? It solves a couple of issues, eg. with getter/setter side-effects. It would solve your problem too. All in all persisting an object is about persisting its *internal* state. – Adam Dyga Feb 18 '13 at 10:27
  • @Adam: Actually I just switched everything to field access to see if I could get it to work that way, but I'm still having problems. I'll update the question with the exceptions thrown. – Reto Höhener Feb 18 '13 at 10:35
0

As per my knowledge we can't* .

There is still one issue with the way hibernate looks up the methods for a given property name.

Suppose you have a property with a name like "mProperty" (first lowercase, second uppercase, rest doesn't matter). Not the accessor methods in the source code will be getMProperty and setMProperty.

The way BasicPropertyAccessor.getterMethod is implemented in that way .

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Really no way? I can't be the only one with this requirement. And I don't understand what you're trying to say with the "mProperty" example. In my case it would be defined as m_mProperty, and the accessor methods would be named getMProperty/setMProperty, as you said (which seems correct to me). – Reto Höhener Feb 16 '13 at 23:12
  • I mentioned :As per my knowledge:: And in my mProperty example i am just telling how BasicPropertyAccessor will fetch for a geter method . – Suresh Atta Feb 16 '13 at 23:20
  • I think I understand now what you were trying to tell me. And I found some forum thread somewhere that suggests subclassing the BasicPropertyAccessor (see my own answer to this question). – Reto Höhener Feb 18 '13 at 10:45
  • check this also http://stackoverflow.com/questions/4313095/jpa-hibernate-and-custom-table-prefixes – Suresh Atta Feb 18 '13 at 10:50
  • Thanks, but that seems to be related to database table/column names, that problem is already solved by the @Column annotation. – Reto Höhener Feb 18 '13 at 10:54
0

I found one lead here: https://forum.hibernate.org/viewtopic.php?f=1&t=943110

But the thread is 7 years old and I don't know how to apply this to annotation based configuration (I don't have an xml configuration file).

Reto Höhener
  • 5,419
  • 4
  • 39
  • 79