Using Java beans as Grails Domain Classes

There are many similar questions, but I have not been able to determine exactly how to use the POJO model in the 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 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 if this should work, but I have not seen anything that would indicate 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?

+4
source share
2 answers

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

If your Java domain classes are hibernated, you do not extend them in grails. All you have to do is define the restrictions in a separate file called UserBeanConstraints.groovy:

 constraints = { name(blank:false) surname() age() } 
+2
source

What does your hibernate.cfg.xml look like?

Make sure your com.example.grails.User class is displayed:

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

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

+3
source

Source: https://habr.com/ru/post/1347521/


All Articles