I have 3 model classes (namely A, B & C). A is the parent having onetoMany bidirectional relationship with B. B is parent to C and having onetoMany bidirectional. B & C have an embedded pKey.
From A --> one primary key is being set to B. From B --> 2 pKeys is being set to C.
Inside service I am setting A inside B, and B inside C.
Get method works fine. while Post I am getting null pointer in C ( as the Pkey is not being transferred from B).
Can someone help how to transfer pKey from A to C ( as one of the Pkey inside B is derived from A).
Model classes:
Class A:
package com.newmodel;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
@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;
//bi-directional many-to-one association to LineItem
@OneToMany(mappedBy="order",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<ProductItem> productItems;
getters & setters
}
Class B:
package com.newmodel;
import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;
@Entity
@Table(name="PRODUCT_ITEMS")
public class ProductItem implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private ProductItemPK id;
private String asku;
//bi-directional many-to-one association to ItemTax
@OneToMany(mappedBy="productItem",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@Fetch(value=FetchMode.SUBSELECT)
private List<ProductItemTax> productItemTaxs;
//bi-directional many-to-one association to Order
@ManyToOne
@JoinColumn(name="ORDER_ID",referencedColumnName="ORDER_ID")
@MapsId("orderId")
@JsonIgnore
private Order order;
getters & setters
}
Class B PK:
package com.newmodel;
import java.io.Serializable;
import javax.persistence.*;
@Embeddable
public class ProductItemPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="ORDER_ID")
private String orderId;
@Column(name="PRODUCT_NUMBER")
private long productNumber;
getters & setters
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ProductItemPK)) {
return false;
}
ProductItemPK castOther = (ProductItemPK)other;
return
this.orderId.equals(castOther.orderId)
&& (this.productNumber == castOther.productNumber);
}
public int hashCode() {
final int prime = 31;
int hash = 17;
hash = hash * prime + this.orderId.hashCode();
hash = hash * prime + ((int) (this.productNumber ^ (this.productNumber >>> 32)));
return hash;
}
}
class c:
package com.newmodel;
import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
@Entity
@Table(name="PRODUCT_TAX")
public class ProductItemTax implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private ProductItemTaxPK id;
@Column(name="CREATE_DATE")
private java.sql.Date createDate;
//bi-directional many-to-one association to LineItem
@ManyToOne
@MapsId("id")
@JoinColumns({
@JoinColumn(name="ORDER_ID",referencedColumnName="ORDER_ID"),
@JoinColumn(name="PRODUCT_NUMBER",referencedColumnName="PRODUCT_NUMBER")})
@JsonIgnore
private ProductItem productItem;
getters & setters
}
class c PK:
package com.newmodel;
import java.io.Serializable;
import javax.persistence.*;
@Embeddable
public class ProductItemTaxPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="ORDER_ID")
private String orderId;
@Column(name="PRODUCT_NUMBER")
private long productNumber;
@Column(name="TAX_SEQUENCE")
private long taxSequence;
getters & setters
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ProductItemTaxPK)) {
return false;
}
ProductItemTaxPK castOther = (ProductItemTaxPK)other;
return
this.orderId.equals(castOther.orderId)
&& (this.productNumber == castOther.productNumber)
&& (this.taxSequence == castOther.taxSequence);
}
public int hashCode() {
final int prime = 31;
int hash = 17;
hash = hash * prime + this.orderId.hashCode();
hash = hash * prime + ((int) (this.productNumber ^ (this.productNumber >>> 32)));
hash = hash * prime + ((int) (this.taxSequence ^ (this.taxSequence >>> 32)));
return hash;
}
}