How to access Grails domain classes in Java service layer?

后端 未结 3 1332
难免孤独
难免孤独 2021-01-07 00:46

How can I use grails domain classes (which is in groovy) in service layer which is in Java/Spring.

When using the grails MVC, everything is fine as I can use contro

相关标签:
3条回答
  • 2021-01-07 00:55

    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.

    0 讨论(0)
  • 2021-01-07 01:03

    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); 
    
    0 讨论(0)
  • 2021-01-07 01:08

    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

    0 讨论(0)
提交回复
热议问题