How to access Grails domain classes in Java service layer?

懵懂的女人 提交于 2019-12-01 00:57:32

org.codehaus.groovy.runtime.InvokerHelper makes this pretty straightforward; see this mailing list thread: http://grails.1312388.n4.nabble.com/Calling-Dynamic-Finders-on-Domain-Class-via-the-MetaClass-td1596496.html

Sample Code Snippet :

import my.package.User
import org.codehaus.groovy.runtime.InvokerHelper;


List allInstances = (List)InvokerHelper.invokeMethod(User.class, "list", null)); 


User one=(User)InvokerHelper.invokeMethod(User.class, "get", id); 

Where as InvokerHelper works well, if you have access to the GORM class I would put wrapper functions around the GORM calls -- then the Java classes will see that.

static String List getAll() { return User.list() }
static String User getByID(long id) { return User.get(id) }

That seems cleaner and doesn't put a dependency on a Groovy class in your Java code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!