0
votes

I'm trying to reuse some of the entity classes with JPA-annotations in my project. But unfortunately I'm newbie in Hibernate & JPA too. When I try to generate controllers with command "grails generate-all" I get error:

Error running generate-all: Error creating bean with name 'messageSource': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is 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 version in class example.MyBean

There is very simple solution: add field 'version' with getter/setter pair. But I don't want to change my entity classes, because may be I'll want to reuse them in some other non-grails projects. Is there a way to use together JPA annotations with hibernate mapping files, without duplication already used annotations in entity classes?

Yes, and also, is this a good idea or there is some better approach?

2

2 Answers

1
votes

Do you require optimistic locking? I believe turning this off will cure this.

You should be to do it via the hibernate config, globally or per class basis using the class/optimistic-lock attribute, or you could add the hibernate annotation to the class, but this obviously means changing the class and assumes you are using hibernate.

You could also possibly use an aop compiler to weave in a version filed/getter/setter or annotation into build domain model

1
votes

In your xml mapping files have:

<hibernate-mapping default-access="field">

default-access (optional - defaults to property): the strategy Hibernate should use for accessing all properties.

If it is set to field, hibernate reads the fields directly, without the need of getters.

Of course, you need the have at least a field. You can't just map arbitrary properties that do not exist. (well, perhaps you can with some 'magic', but I would advise against it)