2016-06-07 88 views
0

的特定列的实体,我有两个实体:休眠得到嵌套实体

@Entity 
@Table(name = "animals") 
public class Animal extends BaseEntity { 
    private String nickname; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "species_id", nullable = false) 
    private Species species; 
} 

@Entity 
@Table(name = "species") 
public class Species extends BaseEntity { 
    private String scientificName; 
    private Integer animalsPerHouse; 
} 

如何获得Animal具体Species场,scientificName例如?如何告诉Hibernate我只需要嵌套实体的特定字段?

期望中的动物:

{ 
    "id": 1, 
    "nickname": "Locuroumee", 
    "species": { 
     "id": 161130, 
     "scientificName": "Anguilla bicolor", 
    } 

实际的动物:

{ 
    "id": 1, 
    "nickname": "Locuroumee", 
    "species": { 
     "id": 161130, 
     "scientificName": "Anguilla bicolor", 
     "animalsPerHouse": 4 
    } 

我已经花了很多时间与预测,别名,但它并不能帮助

+0

我也有类似的问题[这](http://stackoverflow.com/questions/12105757/complex-hibernate-projections) – hardcoder

回答

0

有这样的实体图任务。

Dynamic fetching via JPA entity graph

JPA 2.1 Entity Graph – Part 1: Named entity graphs

JPA 2.1 Entity Graph – Part 2: Define lazy/eager loading at runtime

但是,看起来像,Hibernate不支持部分领域加载,对面的EclipseLink。

你可以尝试使用了定制结果变压器: How to transform a flat result set using Hibernate

+0

投影用于部分字段加载。它的工作原理)它可能与嵌套的实体字段一起工作......但我不知道如何 – hardcoder

+0

@hardcoder请看这里的一个例子:http://stackoverflow.com/a/36361630/3405171 –

+0

已经看到)他们使用定制变压器。但显然有没有它的解决方案。 – hardcoder