开发者

Using Java beans as Grails domain classes

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
}

and a grails domain class:

package com.example.grails

class User extends com.example.java.UserBean {

    static constraints = {
        name(blank:false)
        surname()
        age()
    }
}

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.Us开发者_Python百科er

Any ideas?


What does your hibernate.cfg.xml look like?

Make sure that your com.example.grails.User class is mapped:

<hibernate-configuration>
  <session-factory>
   <mapping package="com.example.grails" />
   <mapping class="com.example.grails.User" />
  </session-factory>
</hibernate-configurations>

More details can be found: http://grails.org/doc/1.0.x/guide/15.%20Grails%20and%20Hibernate.html


I found the answer to this problem on: this spring.io blog post

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()
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜