0

I am facing some weird issue when using Hibernate one to many and many to one mapping and returning data using JSON to my swing client from rest web service.

When my web service is returning salesOrder object. i have checked that it does contains orderlines objects set. But, if i open one of the orderLine object, it again has sales order object.

This chaining is causing issue as client side that infinite string of json is being returned.

Like below...

[
{
"salesOrderNumber":"1",
"customerCode":"1",
"totalPrice":50.0,
orderLines":     
[
{
"salesOrderNumber":"1",
"productCode":"2",
"quantity":1,
"salesOrder":{"salesOrderNumber":"1","customerCode":"1","totalPrice":50.0,"orderLines":[{"salesOrderNumber":"1","productCode":"2","quantity":1,"salesOrder":{"salesOrderNumber":"1","customerCode":"1","totalPrice":50.0,"orderLines":
.............................................
...............................

I have tried to set @JSONIgnore as i don't want those to be sent to client, but, it didn't help.

My Two entities are like below:

@Entity
@Table(name = "salesorder")
//@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class SalesOrder implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "SalesOrderNumber", unique = true, nullable = false)
private String salesOrderNumber;

@Column(name = "CustomerCode")
private String customerCode;

@Column(name = "TotalPrice")
private double totalPrice;

@JsonIgnore
@OneToMany(fetch = FetchType.EAGER, mappedBy="salesOrder",cascade=CascadeType.ALL)
private Set<OrderLines> orderLines = new HashSet<OrderLines>();

public Set<OrderLines> getOrderLines() {
    return orderLines;
}
public void setOrderLines(Set<OrderLines> orderLines) {
    this.orderLines = orderLines;
}

public String getSalesOrderNumber() {
    return salesOrderNumber;
}

public void setSalesOrderNumber(String salesOrderNumber) {
    this.salesOrderNumber = salesOrderNumber;
}

public String getCustomerCode() {
    return customerCode;
}

public void setCustomerCode(String customerCode) {
    this.customerCode = customerCode;
}

public double getTotalPrice() {
    return totalPrice;
}

public void setTotalPrice(double totalPrice) {
    this.totalPrice = totalPrice;
}

}

@Entity
@Table(name = "orderlines")
//@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class OrderLines implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "SalesOrderNumber", unique = true, nullable = false)
private String salesOrderNumber;

@Id
@Column(name = "ProductCode")
private String productCode;

@Column(name = "Quantity")
private int quantity;

@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.ALL)
@JoinColumn(name="SalesOrderNumber")
private SalesOrder salesOrder;

public SalesOrder getSalesOrder() {
    return salesOrder;
}

public void setSalesOrder(SalesOrder salesOrder) {
    this.salesOrder = salesOrder;
}

public String getSalesOrderNumber() {
    return salesOrderNumber;
}

public void setSalesOrderNumber(String salesOrderNumber) {
    this.salesOrderNumber = salesOrderNumber;
}

public String getProductCode() {
    return productCode;
}

public void setProductCode(String productCode) {
    this.productCode = productCode;
}

public int getQuantity() {
    return quantity;
}

public void setQuantity(int quantity) {
    this.quantity = quantity;
}

}
bytecode77
  • 14,163
  • 30
  • 110
  • 141
  • It's a **really** bad idea to send your entities as json. Read more here: http://stackoverflow.com/a/30519514/608820 - The answer is that hibernate is rewriting the class and probably the `@JsonIgnore` annotation is lost. – Augusto Nov 01 '15 at 19:48

1 Answers1

0

It's a really bad idea to send your entities as json, as it will couple your clients to the internal representation of your system... and bad hacks come from this. If you do want to do this regardless and suffer later (or let one of your future colleagues suffer and curse you), keep reading.

The reason why it doesn't work is because Hibernate creates proxies from the objects that it gets from the DB, and the annotations are lost. There is a Jackson extension that will take care of this jackson-datatype-hibernate, but please don't do it (unless your app is trivial and will never change)

Augusto
  • 28,839
  • 5
  • 58
  • 88