Grails - 自动将自定义字段(列)添加到所有域对象 [英] Grails - adding custom fields (columns) to all domain objects - automatically

查看:175
本文介绍了Grails - 自动将自定义字段(列)添加到所有域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,当您创建域类时,它会自动为所有域类(表)添加id和version列。如果我想添加一个专栏,例如isChecked,并且这应该被自动添加到所有的域类(即表),类似id和version列的添加方式。我怎样才能做到这一点,如果我不想为某个特定领域类使用isChecked,我也应该可以做到这一点。

By default when you create a domain class, it will automatically add "id" and "version" column for all the domain classes (tables). What if I want to add a column say for e.g. "isChecked" and this should be added automatically to all the domain classes (i.e tables) similar way "id" and "version" columns are added. How can I achieve this and also if I don't want to have "isChecked" for a specific domain class, I should also be able to do that.

我在Grail 1.3.7中这样做?

How can I do this in Grail 1.3.7?

谢谢。
Jay Chandran

Thank You. Jay Chandran

编辑:我可以获得更多输入吗?建议的解答不起作用!

Can I get more inputs? Suggested answers did not work!

推荐答案

您可以使用Groovy为这类事物提供的元编程魔术,然而,我可能只是走一条更典型的路线并使用继承。创建一个包含isChecked(以及其他任何您需要的)的父域,并让您的域名需要它们扩展该类。

You could use the meta programming magic that Groovy provides for this sort of thing, however, I would probably just go a more typical route and use inheritance. Create a parent domain that contains isChecked (and anything else you need) and have your domains that require them extend that class.

这篇关于Grails - 自动将自定义字段(列)添加到所有域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆