简体   繁体   English

Grails和JPA注释+ hibernate映射文件。 如何摆脱这个错误?

[英]Grails & JPA annotations + hibernate mapping files. How to get rid of this error?

I'm trying to reuse some of the entity classes with JPA-annotations in my project. 我正在尝试在我的项目中重用一些带有JPA注释的实体类。 But unfortunately I'm newbie in Hibernate & JPA too. 但不幸的是,我也是Hibernate和JPA的新手。 When I try to generate controllers with command "grails generate-all" I get error: 当我尝试使用命令“grails generate-all”生成控制器时,我收到错误:

Error running generate-all: Error creating bean with name 'messageSource': Initialization of bean failed; 运行generate-all时出错:创建名为'messageSource'的bean时出错:bean的初始化失败; 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'; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'transactionManager'的bean时出错:在设置bean属性'sessionFactory'时无法解析对bean'sessionFactory'的引用; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'sessionFactory'的bean时出错:init方法的调用失败; nested exception is org.hibernate.PropertyNotFoundException: Could not find a setter for property version in class example.MyBean 嵌套异常是org.hibernate.PropertyNotFoundException:在类example.MyBean中找不到属性版本的setter

There is very simple solution: add field 'version' with getter/setter pair. 有一个非常简单的解决方案:使用getter / setter对添加字段'version'。 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. 但我不想改变我的实体类,因为我可能想在其他一些非grails项目中重用它们。 Is there a way to use together JPA annotations with hibernate mapping files, without duplication already used annotations in entity classes? 有没有办法将JPA注释与hibernate映射文件一起使用,而没有重复已经在实体类中使用注释?

Yes, and also, is this a good idea or there is some better approach? 是的,而且,这是一个好主意还是有更好的方法?

In your xml mapping files have: 在你的xml映射文件中有:

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

default-access (optional - defaults to property): the strategy Hibernate should use for accessing all properties. default-access(可选 - 默认为property):Hibernate用于访问所有属性的策略。

If it is set to field , hibernate reads the fields directly, without the need of getters. 如果将其设置为field ,则hibernate直接读取字段,而不需要getter。

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) (好吧,也许你可以用一些'魔术',但我会反对它)

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. 您应该通过hibernate配置,全局或每个类使用class / optimistic-lock属性来执行此操作,或者您可以将hibernate注释添加到类中,但这显然意味着更改类并假设您正在使用hibernate。

You could also possibly use an aop compiler to weave in a version filed/getter/setter or annotation into build domain model 您还可以使用aop编译器将版本字段/ getter / setter或注释编织到构建域模型中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM