我已经开始将手写JDBC ORM代码转换为Ebeans的项目。到目前为止,这很棒; Ebeans轻巧易用。JPA和Ebean ORM:空集合不为空
但是,我遇到了一个严重的问题:当检索一个应该是空的一对多列表时,实际上有一个元素。这个元素看起来是一种具有所有空字段的代理对象,所以它破坏了通过集合循环的代码。
我已经包括缩的定义在这里:
@Entity
class Store {
...
@OneToMany(mappedBy="store",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
List<StoreAlbum> storeAlbums = new LinkedList<StoreAlbum>();
}
@Entity
class StoreAlbum {
...
@ManyToOne(optional=false,fetch=FetchType.EAGER)
@JoinColumn(name="store_id",nullable=false)
Store store;
}
的......是所有标准的getter和setter方法。检索代码如下所示:
Store s = server.find(Store.class)
.where()
.eq("store_id",4)
.findUnique();
Assert.assertEquals("Sprint",s.getStoreName());
Assert.assertEquals(0, s.getStoreAlbums().size());
数据库已知含有“存储”行了“冲刺”,和“store_album”表不包含对于存储任何行。
JUnit测试在第二个断言上失败。它找到一个包含1个元素的列表,这是某种破碎的StoreAlbum对象。调试器显示该对象的类型为“[email protected]”,对于所有声明为nullable = false(和optional = false)的字段,该值为空值。
我在这里错过了什么吗?
我已经打开了SQL调试,并注意到查询正在执行一个“left outer join”store_album表: select t0.store_id c0,t0.store_name c1,t0.platform_name c2,t0.dsp_id C3 ,t1.store_id C4,C5 t1.album_id,t1.dsp_id C6,从商店T0 t1.pricecode_id C7 左外连接上t1.store_id = t0.store_id 其中t0.store_id = store_album T1? order by t0.store_id 必须有方法来改变这种行为 – homebrew 2011-03-25 17:23:21