Java Hibernate实体未正确连接(双向)

时间:2016-01-27 06:54:44

标签: java hibernate

我有两个Java Hibernate个实体,oneToMany关系。我在前端有Spring控制器和AngularJS。当我在/cars/getCars浏览器上尝试Chrome时,我的网络状态失败了。 Eclipse停止工作(冻结)。

@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 @OneToManymake班级删除Model字段,而不是开始正常工作,但现在我无法通过make获得model

1 个答案:

答案 0 :(得分:5)

这个问题似乎与使用hibernate双向关系的json解析有关。

你需要在@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中对我有用,希望能帮到你。