使用Hibernate/JPA和play framework在@ManyToMany关系中获取重复的结果对象

使用Hibernate/JPA和play framework在@ManyToMany关系中获取重复的结果对象,hibernate,jpa,playframework,Hibernate,Jpa,Playframework,我正在使用PlayFrameworkV1.2.5、JPA和Hibernate 我有两种型号: @Entity public class MapTile extends Model { // ... @Required public Integer tileOrder; @Required @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) public List<

我正在使用PlayFrameworkV1.2.5、JPA和Hibernate

我有两种型号:

@Entity
public class MapTile extends Model {

    // ...

    @Required
    public Integer tileOrder;

    @Required
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    public List<MapBuildingInfo> buildingsInfo;

    // ...
}

@Entity
public class MapBuildingInfo extends Model {

    // ...

    @Required
    public Integer buildingsCount;

    // ...
}
我正在尝试检索(仅使用一个查询和联接)所有
MapTiles
及其对应的
MapBuildingFos

以下是我使用的查询:

List<MapTile> list = MapTile.find("from MapTile tile left join fetch tile.buildingsInfo building").fetch();
而不仅仅是:

list[0].tileOrder => 1
list[0].buildingsInfo[0].buildingsCount => 1
list[0].buildingsInfo[0].buildingsCount => 2

list[1].tileOrder => 2
list[1].buildingsInfo[0].buildingsCount => 3
list[1].buildingsInfo[0].buildingsCount => 4
Hibernate/JPA似乎将每个结果放在一个新的MapTile对象中,因此它创建了一个复制。但是,他正确填写了
mapile
buildingsInfo

我不明白为什么我会得到这个结果。你知道我会做错什么吗?你知道这是不是来自Hibernate/JPA的bug吗?还是我的错误?我怎样才能得到正确的结果


谢谢你的帮助

我认为您必须将查询转换为:

从MapTile中选择不同的tile left join fetch tile.buildingsInfo building


由于您的fetch,您创建了多行,需要告诉hibernate将其映射到同一个实例。

我认为您必须将查询转换为:

从MapTile中选择不同的tile left join fetch tile.buildingsInfo building


由于您的抓取,您创建了多行,并且需要告诉hibernate将其映射到同一个实例。

感谢您的回答,它似乎可以工作!我不认为是那样的simple@FabienHenon谢谢,它总是有帮助的。谢谢你的回答,它似乎起作用了!我不认为是那样的simple@FabienHenon谢谢,这总是有帮助的。
list[0].tileOrder => 1
list[0].buildingsInfo[0].buildingsCount => 1
list[0].buildingsInfo[0].buildingsCount => 2

list[1].tileOrder => 1
list[1].buildingsInfo[0].buildingsCount => 1
list[1].buildingsInfo[0].buildingsCount => 2

list[2].tileOrder => 2
list[2].buildingsInfo[0].buildingsCount => 3
list[2].buildingsInfo[0].buildingsCount => 4

list[3].tileOrder => 2
list[3].buildingsInfo[0].buildingsCount => 3
list[3].buildingsInfo[0].buildingsCount => 4
list[0].tileOrder => 1
list[0].buildingsInfo[0].buildingsCount => 1
list[0].buildingsInfo[0].buildingsCount => 2

list[1].tileOrder => 2
list[1].buildingsInfo[0].buildingsCount => 3
list[1].buildingsInfo[0].buildingsCount => 4