获取Join to Lazily初始化集合和gson序列化

时间:2016-03-16 14:53:53

标签: hibernate rest gson

我在使用Gson API进行延迟初始化和序列化时遇到以下问题:这是我的实体:

这是第一个实体:

public class Cable implements Serializable {

/**
 * serialVersionUID used for the serialization
 */
private static final long serialVersionUID = 9144466650131822647L;

@Id
@Column(name = "reference_cab")
private String rexelReference;

@Column(name = "reference_fab")
private String providerReference;

@Column(name = "libelle")
private String productLibe;

@JsonIgnore
@ManyToMany(mappedBy = "cables", fetch = FetchType.LAZY)
private Collection<CrossSelling> complementaryProducts = new ArrayList<CrossSelling>();

这是第二个实体:

public class CrossSelling implements Serializable {

private static final long serialVersionUID = 1L;

@Id
private String reference;


@JsonIgnore
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "cable_complementaryproduct", joinColumns = { @JoinColumn(name = "reference", referencedColumnName = "reference") }, inverseJoinColumns = { @JoinColumn(name = "reference_cab", referencedColumnName = "reference_cab") })
private Collection<Cable> cables = new ArrayList<Cable>();


@Column
private String designation;

我尝试使用此查询使用获取连接来懒惰地获取互补产品数据:

select distinct cable from  Cable cable left join fetch cable.complementaryProducts

我有一个名为显示

的服务

在我的网络服务中,我执行以下操作:

    final Gson gson = new Gson();
    return new ResponseEntity<Object>(gson.toJson(this.shoppingCartService.display()), HttpStatus.OK);

但我有以下问题:

failed to lazily initialize a collection of role: com.sso.uuu.model.CrossSelling.cables, could not initialize proxy - no Session] with root cause

org.hibernate.LazyInitializationException:懒得初始化一个角色集合:com.sso.uuu.model.CrossSelling.cables,无法初始化代理 - 没有会话

它与gson.toJson()运行良好,但由于我们有编码问题,我们需要使用它感谢您的帮助。

0 个答案:

没有答案