使用Maven增强模型播放2.1.3应用程序不会加载惰性对象 [英] Play 2.1.3 application with Maven enhanced models not loading lazy objects

查看:125
本文介绍了使用Maven增强模型播放2.1.3应用程序不会加载惰性对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个重构的Play应用程序.播放模型已经重构为自己的Maven项目,因此可以与其他项目共享,并且使用ant-run插件来增强模型类.

I have a Play application that is been refactored. The play models have been refactored out to its own Maven project so they can be shared with other projects, and the ant-run plugin is being used to enhance the model classes.

这些模型可以正常工作,直到涉及到加载惰性引用(我正在对公共字段使用字段增强).根本不加载引用,并且返回空对象.示例代码为:

The models work ok up to the point that it involves loading the lazy references (I'm using field enhancement with public fields). The references are simply not loaded and an empty object is beeing returned. The example code is:

@Entity
class A extends Model {

   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   public int id;

   @ManyToOne
   public B b;

   public static Finder<Integer,A> finder = new Finder<Integer,A>(Integer.class, A.class);
}

@Entity
class B extends Model {

   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   public int id;

   public String someField;  
}
....
....
A a = A.finder.by(someId); // returns the correct object
B b = a.b; // Returns a not null but empty object
System.out.println(b.someField); // someField is always null 

当我尝试使用引用的对象时,该对象不是null,但似乎尚未使用数据库中的数据对其进行初始化.

When I try to use the referenced object the object is not null, but it seems it hasn't been initialized with data from the DB.

即使B上的id字段已正确填充,但其余字段也未正确填充.

Even the id field on the B is populated correctly, but the rest of the fields are not.

Maven似乎可以正确地增强对象(蚂蚁运行),因为我可以通过查询等方式获取对象.

Maven seems to be enhancing the objects correctly (ant-run) since I can get the objects with queries, etc.

如果我在Java端指定了.fetch("b"),则可以正确返回该对象,但是以前没有必要这样做,有时也无法这样做(在Scala模板中).

If I specify .fetch("b") on the Java side the object is returned correctly, but it wasn't necessary before, and sometimes is not possible to do so (in the Scala templates).

我已经检查了数据库以及相关的对象.同样,代码在Play项目中时仍在工作,但是自从我将模型移到Play之外后,很多代码(不是全部)都停止了.

I've checked the DB and the related objects are there. Also the code was working while it was inside the Play project, but a lot of code (not all) has stopped doing so since I moved the models outside Play.

是否有用于application.conf的特定配置以指定延迟加载,或者我缺少某些内容?

Are there any specific configs for application.conf to specify lazy loading or am I missing something?

我已经向Maven项目添加了一个测试,并直接使用EBean测试了这些类,而不是使用Play中的Model类,它也不起作用,因此似乎指向了Avaje Enorm:

I have added a tests to the Maven project and tested directly the classes with EBean instead of using the Model class from Play and it doesn't work either, so it seems to point Avaje Enorm:

List<A> aS = server.find(A.class).findList();
for (A a : aS) {
   B b = a.b;
   System.out.println("Prop: " + a.b.someProperty); // prints Prop: null
   b.refresh();
   System.out.println("Prop: " + a.b.someProperty); // prints correctly the value
}

Play编译器在增强类方面是否做一些特殊的事情?我使用ant-run-plugin来增强类,因为我找不到使maven-enhance-plugin起作用的方法(在antrun时找不到Play的Model类来检查增强功能)

Does the Play compiler do something special to enhance the classes? I use the ant-run-plugin to enhance the classes because I can't find a way to make the maven-enhance-plugin work (it can't find Play's Model class to check for enhancement while the antrun does)

编辑2 我给了#user1664823正确的答案,因为它可以正常工作.但是在我的情况下它不起作用的原因是因为我淘汰了这些类以及增强的工作方式.首先,我是通过直接进入现场的方式来射击自己的.

Edit 2 I've given #user1664823 answer as right, because it works. But the reason why it is not working in my case is because I extracted the classes out of play and the way the enhancement works. I shot myself on the foot by using direct field access in the first place.

Ebean通过修改访问器或修改对字段的访问代码,添加额外的代码来检测访问并执行延迟加载机制(尝试查看增强类的反编译版本)来增强类.可以在 http://www.avaje.org/topic-159上看到有关此主题的一些好的评论. .html

Ebean enhances the classes by modifying the accessors or modifying the access code to the fields, adding extra code to detect the access and execute the lazy loading mechanism (try to see the decompiled version of your enhanced class). Some good comments on the subject can be seen at http://www.avaje.org/topic-159.html

由于我的班级从主要的Play项目移入了外部Maven项目,并且增强功能是通过ant完成的,因此仅Maven项目中的那些类得到了增强.播放代码无法使用直接字段访问来触发延迟加载机制.

Since my classes got out of the main Play project into an external Maven project, and the enhancement was done with ant, only those classes in the Maven project were enhanced. Play code was not able to trigger the lazy loading mechanism using direct field access.

我没有像user1664823所说的那样使用获取机制(它可以那样工作),而是将代码更改为使用getter和setter,现在懒加载可以了.

Instead of using the fetch mechanism as user1664823 said (it works that way), I have changed the code to use getters and setters and now the lazy loading works ok.

推荐答案

从2.0.*升级到2.1.*时,我遇到了同样的问题. 我发现的唯一解决方案是在所有finder调用中添加fetch(..)规范.

I had the same problem while upgrading from 2.0.* to 2.1.*. The only solution I found was adding the fetch(..) spec to all of my finder calls.

这篇关于使用Maven增强模型播放2.1.3应用程序不会加载惰性对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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