I am trying to understand why when I hit the controler for second time, My OnetoOne Mapping is getting lay initialized. Below is my code: Controller:
@RequestMapping(value="/updateOrderbyOrderid", method=RequestMethod.PUT,produces=MediaType.APPLICATION_JSON_VALUE)
public Order updateOrderbyOrderid (@RequestBody Order orderVO ) {
System.out.println(orderVO.getOrderId());
Order s1 = orderRepository.findByOrderId(orderVO.getOrderId());
if (orderVO.getCustomerId()!=null) {
orderVO.setCustomer(customerRepository.findByCustomerId(orderVO.getCustomerId()));
}
s1 = orderRepository.saveAndFlush(orderVO);
return s1;
}
Order Entity:
@Entity
@Table(name="Ordertable", schema="cf_2583f365_c3c6_499a_a60d_138e7e7023eb")
public class Order {
@Id
@Column(name = "ORDER_ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int orderId;
@OneToOne(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
@JoinColumn(name = "ORDER_CUSTOMER_ID", referencedColumnName = "CUSTOMER_ID")
private Customer customer;
private transient Long customerId;
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Long getCustomerId() {
return customerId;
}
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}
}
Customer Entity:
@Entity
@Table(name="Customer", schema="cf_2583f365_c3c6_499a_a60d_138e7e7023eb")
public class Customer {
@Id
@Column(name = "CUSTOMER_ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long customerId;
@Column(name = "CUSTOMER_NAME")
private String customer_name;
@Column(name = "CUSTOMER_address_id")
private int customer_address_id;
public Long getCustomerId() {
return customerId;
}
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}
public String getCustomer_name() {
return customer_name;
}
public void setCustomer_name(String customer_name) {
this.customer_name = customer_name;
}
public int getCustomer_address_id() {
return customer_address_id;
}
public void setCustomer_address_id(int customer_address_id) {
this.customer_address_id = customer_address_id;
}
public Customer() {
}
}
Json structure for Controller:
{
"orderId" :101,
"customerId" : 2
}
Order Table for First Hit :
OnetoOne annotated entity screenshot for first hit. No lazy initialised bean for customerRepository.findByCustomerId(orderVO.getCustomerId()):
Order Table for Second Hit :
When I hit for second time, I get a lazy loaded bean for customerRepository.findByCustomerId(orderVO.getCustomerId()). Why is it happening:
My expectation is hibernate should get fully loaded bean for next time as well when I am doing orderVO.setCustomer(customerRepository.findByCustomerId(orderVO.getCustomerId())); rather than assiging the lazy initialised bean which it got from Order s1 = orderRepository.findByOrderId(orderVO.getOrderId());
One important Note, If I comment below line in controller:
Order s1 = orderRepository.findByOrderId(orderVO.getOrderId()); :
and replace it with Order s1 = null, I am not getting lazy initialised bean anymore. Was hibernate caching the same lazy initialised bean internally earlier ??
Screenshot of s1 when I didn't commented it :
Code after commenting s1 & replacing it with null:
@RequestMapping(value="/updateOrderbyOrderid", method=RequestMethod.PUT,produces=MediaType.APPLICATION_JSON_VALUE)
public Order updateOrderbyOrderid (@RequestBody Order orderVO ) {
System.out.println(orderVO.getOrderId());
Order s1 = null;
//Order s1 = orderRepository.findByOrderId(orderVO.getOrderId());
if (orderVO.getCustomerId()!=null) {
orderVO.setCustomer(customerRepository.findByCustomerId(orderVO.getCustomerId()));
}
s1 = orderRepository.saveAndFlush(orderVO);
return s1;
}