java spring data jpa,hibernate,奇怪的映射

时间:2015-02-11 01:16:51

标签: java hibernate jpa spring-data-jpa one-to-many

我对下面的stange行为有点麻烦。 有3个实体:`

@Entity
@Table(name = "project")
public class Project {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "project", cascade = CascadeType.ALL)
private List<Building> buildings; 

@Entity
@Table(name = "building")
public class Building {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToMany(orphanRemoval = true, mappedBy = "building", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<GeoPoint> bound;

@ManyToOne
@JoinColumn(name = "project_id")
private Project project;
}

@Entity
@Table(name = "geo_point")
public class GeoPoint {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "building_id")
private Building building;
}

我偷偷地隐藏了不必要的领域和方法。我在数据库存储中有一些数据,当我使用存储库方法findAll()获取数据时,我收到了正确的结果。

但是,如果我使用存储库的findOne()方法,那么我会感到很奇怪&#39;结果, 在这种情况下,建筑物数量增加并等于GeoPoint数量。即我有很多重复的建筑。

你能解释一下这种行为吗?可能有人有类似的问题。

编辑:

通过调用repository.findAll()方法

收到下一个结果

&#13;
&#13;
buildings: [{id: 1, osmId: 112896787, city: null, country: null, houseNumber: "5",…}]
0: {id: 1, osmId: 112896787, city: null, country: null, houseNumber: "5",…}
bound: [{id: 1, longitude: 47.827211, latitude: 52.0166565},…]
0: {id: 1, longitude: 47.827211, latitude: 52.0166565}
1: {id: 2, longitude: 47.8270916, latitude: 52.0167423}
2: {id: 3, longitude: 47.8272374, latitude: 52.0168192}
3: {id: 4, longitude: 47.827502, latitude: 52.0169586}
4: {id: 5, longitude: 47.8277507, latitude: 52.0170896}
5: {id: 6, longitude: 47.8280161, latitude: 52.0172294}
6: {id: 7, longitude: 47.8282689, latitude: 52.0173626}
7: {id: 8, longitude: 47.8284111, latitude: 52.0174375}
8: {id: 9, longitude: 47.8285305, latitude: 52.0173517}
9: {id: 10, longitude: 47.827211, latitude: 52.0166565}
city: null
country: null
houseNumber: "5"
id: 1
osmId: 112896787
street: "Степная улица"
description: "description"
id: 1
latitude: 52.02038830745109
longitude: 47.826576232910156
name: "name"
&#13;
&#13;
&#13;

并调用repository.findOne(id)方法:

&#13;
&#13;
buildings: [{id: 1, osmId: 112896787, city: null, country: null, houseNumber: "5",…},…]
0: {id: 1, osmId: 112896787, city: null, country: null, houseNumber: "5",…}
bound: [{id: 1, longitude: 47.827211, latitude: 52.0166565},…]
city: null
country: null
houseNumber: "5"
id: 1
osmId: 112896787
street: "Степная улица"
1: {id: 1, osmId: 112896787, city: null, country: null, houseNumber: "5",…}
bound: [{id: 1, longitude: 47.827211, latitude: 52.0166565},…]
city: null
country: null
houseNumber: "5"
id: 1
osmId: 112896787
street: "Степная улица"
2: {id: 1, osmId: 112896787, city: null, country: null, houseNumber: "5",…}
bound: [{id: 1, longitude: 47.827211, latitude: 52.0166565},…]
city: null
country: null
houseNumber: "5"
id: 1
osmId: 112896787
street: "Степная улица"
3: {id: 1, osmId: 112896787, city: null, country: null, houseNumber: "5",…}
4: {id: 1, osmId: 112896787, city: null, country: null, houseNumber: "5",…}
5: {id: 1, osmId: 112896787, city: null, country: null, houseNumber: "5",…}
6: {id: 1, osmId: 112896787, city: null, country: null, houseNumber: "5",…}
7: {id: 1, osmId: 112896787, city: null, country: null, houseNumber: "5",…}
8: {id: 1, osmId: 112896787, city: null, country: null, houseNumber: "5",…}
9: {id: 1, osmId: 112896787, city: null, country: null, houseNumber: "5",…}
description: "description"
id: 1
latitude: 52.02038830745109
longitude: 47.826576232910156
name: "name"
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

正如我发现的那样,这种行为是由fetch annotation问题引起的。 如果要添加注释@Fetch(value = FetchMode.SUBSELECT),它将运行良好。

Discussion关于它