问题描述
有很多类似的问题,但我无法在grails应用程序中找到如何使用POJO模型。考虑下面的Java Bean:
There are many similar questions, but I couldn't find exactly how to use a POJO model in a grails application. Consider the following Java Bean:
package com.example.java;
public class UserBean {
String name;
String surname;
Integer age;
//--- assume setters and getters here
}
和一个grails域的类:
and a grails domain class:
package com.example.grails
class User extends com.example.java.UserBean {
static constraints = {
name(blank:false)
surname()
age()
}
}
和scaffold = true的伴随控制器。我不确定这是否应该起作用,但我没有看到任何其他建议。这编译和运行良好,直到我尝试从生成的视图添加一个新用户。然后我得到
and a companion controller with scaffold=true. I am not sure whether this supposed to work but I didn't see anything suggesting otherwise. This compiles and runs fine, until I try to add a new user from the generated view. Then I get
org.hibernate.MappingException: Unknown entity: com.example.grails.User
有什么想法?
Any ideas?
推荐答案
我在这个问题上找到了答案:
I found the answer to this problem on: this spring.io blog post
如果您的Java域类是hibernate映射的,那么您不要在grails中扩展它们。您只需要在名为UserBeanConstraints.groovy的单独文件中定义约束:
If your Java domain classes are hibernate mapped, you don't extend them in grails. All you have to do is to define the constraints in a separate file named UserBeanConstraints.groovy:
constraints = {
name(blank:false)
surname()
age()
}
这篇关于将Java bean用作Grails域类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!