Hi,
I am trying to create onetoone mapping between two tables where the parent key primary key acts as the primary key for child as well. While trying to save parent I am getting the following error.
message": "attempted to assign id from null one-to-one property [com.newModel.Compensation.order]; nested exception is org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.newModel.Compensation.order]"
Please find the below console log, model classes and service class used for the same. Can someone pls help to resolve the error.
Basically want to transfer the order id from order class to order id under compensation using crud repo
Parent class:
package com.newModel;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="ORDERS")
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="ORDER_ID")
private String orderId;
@Column(name="ACCESS_ID")
private String accessId;
@OneToOne(cascade=CascadeType.ALL,mappedBy="order",fetch=FetchType.EAGER)
private Compensation compensation;
//getters & setters
}
Child class
package com.newModel;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapsId;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
/**
* The persistent class for the ORDER_COMPENSATION database table.
*
*/
@Entity
@Table(name="COMPENSATION")
@NamedQuery(name="Compensation.findAll", query="SELECT o FROM Compensation o")
public class Compensation implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="ORDER_ID",insertable = true, updatable = false)
private String orderId;
@Column(name="CHANNEL_DEALER_CODE")
private String channelDealerCode;
//bi-directional one-to-one association to Order
@MapsId
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="ORDER_ID")
private Order order;
public Compensation() {
}
//getters & setters
}
Service class
package com.sample.service;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.newModel.Order;
@Component
public class MobilityServiceImpl implements MobilityService {
@Autowired
private MobilityRepository mobilityRepo;
@Override
public Response getOrderDetails(String orderId) {
Order orderDetails=mobilityRepo.findByOrderId(orderId);
return Response.ok(orderDetails).build();
}
@Override
public Response saveOrderDetails(Order orderDetails) {
orderDetails.getCompensation().setOrder(orderDetails);
Order orderResponse =mobilityRepo.save(orderDetails);
String resp=orderResponse.getOrderId()+" is Success";
return Response.ok(resp).build();
}
}
Console log
Hibernate: select order0_.order_id as order_id1_1_1_, order0_.access_id as access_i2_1_1_, compensati1_.order_id as order_id1_0_0_, compensati1_.channel_dealer_code as channel_2_0_0_ from orders order0_ left outer join compensation compensati1_ on order0_.order_id=compensati1_.order_id where order0_.order_id=?
2018-11-23 14:14:23.268 ERROR 20112 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: attempted to assign id from null one-to-one property [com.newModel.Compensation.order]; nested exception is org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.newModel.Compensation.order]] with root cause
org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.newModel.Compensation.order]
JSON Request:
{
"orderId": "1006730",
"accessId": "1810_CRU",
"compensation": {
"channelDealerCode": "ABCD"
}
}