2015-02-11 47 views
0

我跟隨stange行爲有點麻煩。 有3個實體:`java spring數據jpa,休眠,奇怪的映射

@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; 
} 

我porposly藏不必要的字段和方法。我有一些數據在數據庫存儲和當我得到數據庫方法findAll()我收到正確的結果。

但是,如果我使用存儲庫的findOne()方法,那麼我會得到'怪異'結果, 在這種情況下,Building的數量增加並等於GeoPoint的數量。即我有很多重複的建築物。

你能解釋這種行爲嗎?可能是有人有類似的問題。

編輯:

下一結果被調用repository.findAll()方法

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"

和調用repository.findOne(ID)方法來接收:

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"

+1

,您可以包括實際調用(相關背景),以及預期和實際的結果? – adamdc78 2015-02-11 02:22:20

+0

使用Set而不是List,並儘可能避免提前抓取,尤其是對於許多關聯。 – 2015-02-11 11:27:28

+0

謝謝,我知道它的工作原理。然而,爲什麼findOne和findAll方法返回不同的結果呢? – 2015-02-11 11:32:47

回答

0

正如我發現的,這種行爲是由提取註釋問題引起的。 如果添加註釋@Fetch(value = FetchMode.SUBSELECT),它將工作正常。

Discussion