Groovy 动态调用类和 find 方法不起作用?
我试图构建一个类似于以下内容的动态查询:
def domain = DomainName
def ids = 1
def domainClass = "$domain" as Class
domainClass.find("from ${domain} as m where m.job = ${ids} ").id
但它不起作用。
如果我尝试这样做,一切都很好:
def domain = DomainName
def ids = 1
DomainName.find("from ${domain} as m where m.job = ${ids} ").id
How can I usedynamic domain class name with find?
I trying to build a dynamic query similar to:
def domain = DomainName
def ids = 1
def domainClass = "$domain" as Class
domainClass.find("from ${domain} as m where m.job = ${ids} ").id
But it's not working.
If I'm trying this, all is fine:
def domain = DomainName
def ids = 1
DomainName.find("from ${domain} as m where m.job = ${ids} ").id
How can I use dynamic domain class name with find?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的方法是使用
getDomainClass
方法:请注意,如果您尝试通过 id 获取单个实例,请使用
get
:如果您想获取多个实例并且你有一个 id 列表,你可以使用 getAll
另外,使用嵌入属性值的 GStrings 是一个真正坏主意 - Google 'SQL Injection'
例如通过用户名查找一个人:
The simplest way is to use the
getDomainClass
method:Note that if you're trying to get a single instance by id, use
get
:and if you want to get multiple instances and you have a list of ids, you can use getAll
Also it's a REALLY bad idea to use GStrings with property values embedded - Google 'SQL Injection'
For example to find a person by username:
您应该能够通过显式使用 GroovyClassLoader 来做到这一点:
You should be able to do this by explicitly using the GroovyClassLoader:
动态获取 Domain 类的最佳方法是通过 GrailsApplication 对象。示例:
您还可以像在 Java 中一样使用
Class.forName()
。使用 3 参数版本并传入当前线程上下文类加载器:类加载器在 Java 和 JVM 框架中是一个丑陋的话题。在 Grails 中,您几乎总是希望使用线程上下文类加载器。但更好的是使用 GrailsApplication 接口并完全避免这个问题。
The best way to get a Domain class dynamically is through the GrailsApplication object. Example:
You can also use
Class.forName()
just as you would in Java. Use the 3 parameter version and pass in the current thread context class loader:Classloaders are an ugly topic in Java and JVM frameworks. In Grails, you almost always want to use the thread context classloader. But even better is to use the GrailsApplication interface and avoid the issue altogether.
使用 GrailsClassUtils
获取班级名称,所以这应该有效...如果我理解这个问题
use GrailsClassUtils
to get the name of the class, so this should work... if I understood the question