简体   繁体   English

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

[英]java spring data jpa, hibernate, strange mapping

I have a little trouble with stange behavior of following. 我对下面的stange行为有点麻烦。 There are 3 entities: ` 有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;
}

I porposly hid unnecessary fields and methods. 我故意隐藏了不必要的字段和方法。 I have some data in db storage and when i get data with repository method findAll() I recieve proper result. 我在数据库存储中有一些数据,当我使用存储库方法findAll()获取数据时,我收到了正确的结果。

But if I use the findOne() method of repository then I get 'weird' result, In this case number of Building increases and equals to number of GeoPoint. 但是,如果我使用存储库的findOne()方法,那么我会得到“怪异”的结果,在这种情况下,建筑物的数量会增加,并且等于GeoPoint的数量。 ie I have many duplicates of building. 即我有很多重复的建筑。

Can you explain this behavior? 你能解释一下这种行为吗? Might be someone had similar issue. 可能有人遇到类似问题。

edited: 编辑:

The next result was received by invoking repository.findAll() method 通过调用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" 

and invoking repository.findOne(id) method: 并调用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" 

As I found out, this behavior is caused by issue of fetch annotation. 如我所知,此行为是由于获取注释问题引起的。 If to add annotation @Fetch(value = FetchMode.SUBSELECT) it will work well. 如果添加注释@Fetch(value = FetchMode.SUBSELECT),它将很好地工作。

Discussion about it 讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM