如何在 Spring Roo DBRE 生成的实体中自定义设置器?
这个问题可能暴露了我对 AspectJ 知识的缺乏,但无论如何:)
我已经成功地使用 Spring Roo 进行数据库逆向工程,将一堆表放入一个新的 Roo 项目中。我希望能够保留 Roo 的往返工程方面,而不是将我的方面“推”到普通的旧 Java 类中。但是,我在向方面定义的成员字段分配值时遇到问题。
例如,我有一个 RooDbManaged 实体类,我们称之为 X,Roo 生成了一个切面:X_Roo_DbManaged。现在,我想为字段“updateDate”自定义一个设置器,以便我可以将日期指定为持久化时的当前日期。因此,我将自定义代码放置在类 X 中,如下所示:
public void setUpdateDate(Date updateDate) {
this.updateDate = new Date();
}
这会导致 Roo 从方面删除 setter,正如我所期望的那样,因为我现在已经定义了它在 X 中。
这给了我一个错误: 字段 X.updateDate 不可见
如果我将 X_Roo_DbManaged 方面中字段的可见性更改为“公共”(我不想这样做),这会解决问题,直到 Roo 自动重新生成导致错误再次发生的方面。
我的猜测是,我错过了一些如此明显的东西,以至于没有人认为值得发布,因为我通常成功的谷歌搜索未能找到这个问题的任何解决方案!
我应该补充一点,我的解决方法是通过将 X_Roo_Controller 中的相关方法移动到 XController 中并使用未修改的 setter 在那里分配日期来自定义这些方法。这似乎违反直觉,因为我真的希望 updateDate 始终是设置时的当前日期。有趣的是,Roo 生成的 updateDate 字段上的 @Temporal(TemporalType.TIMESTAMP) 注释不提供此功能。我真的很希望能够使用 Roo 命令告诉 Roo,某些 DBRE 字段应该具有这种行为,而不必担心本质上是什么“管道”。
This question may expose my lack of knowledge of AspectJ but here goes anyway :)
I have successfully used Spring Roo to Database Reverse Engineer a bunch of tables into a new Roo project. I'd like to be able to keep the round-trip-engineering aspect of Roo by not 'pushing' my aspects into plain old Java classes. However, I am experiencing an issue with assigning values to the member fields that are defined in the Aspect.
For example, I have a RooDbManaged entity class, let's call it X, and Roo has generated an aspect: X_Roo_DbManaged. Now, I want to customize a setter for the field 'updateDate' so that I can assign the date to be the current at the time of persistence. So, I have placed my custom code in the class X as follows:
public void setUpdateDate(Date updateDate) {
this.updateDate = new Date();
}
This causes Roo to remove the setter from the aspect, as I would expect, because I have now defined it in X.
This gives me an error:
The field X.updateDate is not visible
If I change the visibility of the field in the X_Roo_DbManaged aspect to 'public' (something that I'd rather not do), this resolves the issue until Roo automatically regenerates the aspect causing the error to recur.
My guess is that I'm missing something so obvious that no one has thought it worthwhile posting, as my usually successful Googling failed to find any solution for this one!
I should add that my workaround is to customize the relevant methods from the X_Roo_Controller by moving them into the XController and assigning the date there using the unmodified setter. This seems to be counter intuitive as I would really like the updateDate always to be the current date when set. Interestingly the Roo-generated @Temporal(TemporalType.TIMESTAMP) annotation on the updateDate field does not provide this functionality. I'd realy like to be able to tell Roo with a Roo command that certain DBRE fields ought to have this behavior and not have to worry about what is essentially 'plumbing'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用
@PrePersist
和@PreUpdate
而不是破解设置器否则您仍然可以将字段本身从
X_Roo_DbManaged.aj
放入X.java
文件,但您将失去对该字段进行增量逆向工程的能力。I suggest you use
@PrePersist
and@PreUpdate
instead of hacking the settersOtherwise you can still put the field itself from
X_Roo_DbManaged.aj
intoX.java
file, but you will loose the ability to incrementally reverse engineer that field.您可以将字段 updateDate 移动到 X.java 中以使其可见。
You can move the field updateDate into X.java for it to be visible.