我有兩個Java
Hibernate
與oneToMany
關係的實體。另外,我在前端有Spring
控制器和AngularJS
。當我在Chrome
瀏覽器上嘗試/cars/getCars
時,我的網絡狀態失敗。 Eclipse停止工作(凍結)。Java休眠實體未正確連接(雙向)
@Entity
@Table(name="auto")
public class Make {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(length = 100)
private String make;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "make")
private List<Model> models = new ArrayList<Model>(0);
public String getMake() {
return make;
}
...
}
和
@Entity
@Table(name="models")
public class Model {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(length = 100)
private String model;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_maker")
private Make make;
public Model() {}
public void setModel(String model){
this.model = model;
}
...
DAO
public List<Make> getCars(){
Criteria criteria = session.createCriteria(Make.class);
return criteria.list();
}
和Spring
控制器:
@RequestMapping("/cars/getCars")
public @ResponseBody List<Make> getCars(){
DAO d = new DAO();
return d.getCars();
}
對於我看起來像N + 1個查詢問題。從異常
和片斷:
..at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:111)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:24)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:656)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:675)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:656)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:675)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:111)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:24)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:656)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:675)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:656)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:675)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157) ...
如果變化:@OneToMany(fetch = FetchType.LAZY, mappedBy = "make") to @OneToMany
一個從Model
類比上班正常的,但現在我不能用model
得到make
make
字段中刪除。
您是如何嘗試調試到目前爲止?你怎麼知道這是一個Hibernate的問題? Eclipse意味着什麼停止工作?你爲什麼認爲這是一個n + 1查詢問題?當你明確想要返回完整的汽車結果時,你是否試圖停止懶惰的獲取? DAO代碼在哪裏? –