2016-01-27 65 views
-1

我有兩個JavaHibernateoneToMany關係的實體。另外,我在前端有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得到makemake字段中刪除。

+0

您是如何嘗試調試到目前爲止?你怎麼知道這是一個Hibernate的問題? Eclipse意味着什麼停止工作?你爲什麼認爲這是一個n + 1查詢問題?當你明確想要返回完整的汽車結果時,你是否試圖停止懶惰的獲取? DAO代碼在哪裏? –

回答

4

這個問題似乎與json解析hibernate雙向關係有關。

您需要在@ManyToOne之後添加@JsonIgnoreProperties,此註釋來自jackson 2. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity 
@Table(name="auto") 
public class Make { 
    ... 

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "make") 
    @JsonIgnoreProperties(value = "make") // to ignore the make from Model class while parsing to your json/xml 
    private List<Model> models = new ArrayList<Model>(0); 

    ... 
} 

同樣,

@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "fk_maker") 
@JsonIgnoreProperties(value = "models") // to ignore the models from Make class while parsing to your json/xml 
private Make make; 

這種類型的方法在@ManyToMany爲我工作,希望會幫助你。

+0

工作就像一個魅力 - 我有一個雙向OneToMany關係相同的問題;這是我第一次發現ignoreProperties可以在屬性級應用:)。非常感謝!!有沒有一個體面的文檔鏈接爲這種更快的xml的深層功能? – user1523177

+0

我也在尋找相同的文檔。 但是你可以在mkyong.com上找到一些很好的例子 –