Reputation: 2765
Hibernate lazy loading is not working in my code. It loads the entire data even it is specified as FetchType LAZY
@Transactional(propagation = Propagation.NEVER)
public OrderItem getItem(String itemId) throws Exception {
OrderItem item = itemDao.find(OrderItem.class, Integer.parseInt(itemId));
if (item == null) {
throw new Exception(502, "We are unable to load item for #" + itemId);
}
return item;
}
@NotFound(action = NotFoundAction.IGNORE)
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
@JoinColumn(name = "id_order_detail")
@Fetch(value= FetchMode.JOIN)
@JsonManagedReference
private Set<OrderItemStateChangeEntry> itemStateHistory;
I could not able to lazy load the contents. There is no foreign key constraint set in the db. And its not possible to set as the many parent data not present in the system.
Can somebody help me on this
Update Added my class and reference. But lazy load work
@Entity
@Table(name = "ps_orders")
@AttributeOverrides({
@AttributeOverride(name="id",column=@Column(name="id_order")),
@AttributeOverride(name="createTime",column=@Column(name="date_add")),
@AttributeOverride(name="updateTime",column=@Column(name="date_upd"))
})
public class Order extends BaseEntity{
@Column(name = "id_carrier")
private Integer carrier = 0;
@NotFound(action = NotFoundAction.IGNORE)
@OneToMany(fetch = FetchType.LAZY,cascade={CascadeType.PERSIST, CascadeType.MERGE}, mappedBy="order")
@Fetch(FetchMode.SELECT)
@JsonManagedReference
private Set<OrderStateChangeEntry> orderHistory;
//Getters and Setters
}
@Entity
@Table(name = "ps_order_history")
@EnableBouncerProfile
public class OrderStateChangeEntry implements java.io.Serializable{
public OrderStateChangeEntry(){}
public OrderStateChangeEntry(Order order){
this.order = order;
}
@Id
@Column(name = "id_order_history")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="id_order", nullable=false)
@JsonBackReference
private Order order;
//Getters and Setters
}
Upvotes: 0
Views: 5310
Reputation: 1098
It is because of your
@Fetch(value= FetchMode.JOIN)
it disables the lazy loading ...
As you specify the fetch mode in your @OnetoMany relationship, i would say that you can simply remove that line above.
Upvotes: 1