Adding business logic to a domain class using a getter style method name

Posted by Richard Paul on Stack Overflow See other posts from Stack Overflow or by Richard Paul
Published on 2010-05-27T10:22:16Z Indexed on 2010/05/28 10:42 UTC
Read the original article Hit count: 259

Filed under:
|

I'm attempting to add a method to a grails domain class, e.g.

class Item {

  String name

  String getReversedName() {
    name.reverse()
  }

}

When I attempt to load the application using grails console I get the following error:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory':Invocation of init method failed; nested exception is org.hibernate.PropertyNotFoundException: Could not find a setter for property reversedName in class Item ... 18 more

It looks like Hibernate is interpreting getReversedName() as a getter for a property, however in this case it is a derived field and hence should not be persisted. Obviously in my actual code the business logic I'm exposing is more complex but it isn't relevant to this question. I want to be able to call item.reversedName in my code/gsps.

How can I provide property (getter) access to a method in a Grails domain class without Grails attempting to map it with Hibernate?

© Stack Overflow or respective owner

Related posts about grails

Related posts about groovy