Thanks for the reply.
I made the change and I a sort of long generated SQL.
Here is the LineItem Key
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class LineItemId implements Serializable {
private Integer orderId;
private Integer productId;
public LineItemId() {
super();
// TODO Auto-generated constructor stub
}
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public boolean equals(Object arg0) {
if (arg0 == this) return true;
if (!(arg0 instanceof LineItemId)) return false;
LineItemId other = (LineItemId)arg0;
if(other.getOrderId().equals(orderId) && other.getProductId().equals(productId))
{
return true;
}
return false;
}
public int hashCode() {
return orderId + productId.hashCode();
}
}
here is the LineItem class
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="LINEITEM")
@IdClass(LineItemId.class)
public class LineItem implements Serializable {
private Product product;
private LineItemId lineItemId;
private Integer quantity;
private Integer total;
@Column(name="ORDER_ID")
public Integer getOrderId(){
return lineItemId.getOrderId();
}
public void setOrderId(Integer orderId) {
lineItemId.setOrderId(orderId);
}
@Column(name="QUANTITY")
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
@Column(name="AMOUNT")
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
@ManyToOne(fetch=FetchType.EAGER,optional=false)
@JoinColumn(name="PRODUCT_ID", referencedColumnName="SKU",insertable=false,updatable=false, nullable=false,unique=true)
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
@Column(name="PRODUCT_ID")
public Integer getProductId() {
return lineItemId.getProductId();
}
public void setProductId(Integer productId) {
lineItemId.setProductId(productId);
}
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "orderId",column=@Column(name="ORDER_ID")),
@AttributeOverride(name = "productId",column=@Column(name="PRODUCT_ID"))
})
public LineItemId getLineItemId() {
return lineItemId;
}
public void setLineItemId(LineItemId lineItemId) {
this.lineItemId = lineItemId;
}
It complained when I did not include setters and getters for orderId and productId. I treid mapping the fields and making them transient. Here is the SQL generated when I load the LineItems from the Order class.
select customer0_.CUST_ID as CUST1_3_, customer0_.NAME as NAME35_3_, customer0_.OPEN_ORDER_ID as OPEN3_35_3_, customeror1_.ORDER_ID as ORDER1_0_, customeror1_.TOTAL as TOTAL36_0_, customeror1_.STATUS as STATUS36_0_, customeror1_.CUSTOMER_ID as CUSTOMER4_36_0_, lineitems2_.ORDER_ID as ORDER3_5_, lineitems2_.orderId as orderId5_, lineitems2_.productId as productId5_, lineitems2_.orderId as orderId1_, lineitems2_.productId as productId1_, lineitems2_.orderId as orderId37_1_, lineitems2_.productId as productId37_1_, lineitems2_.ORDER_ID as ORDER3_37_1_, lineitems2_.PRODUCT_ID as PRODUCT4_37_1_, lineitems2_.AMOUNT as AMOUNT37_1_, lineitems2_.QUANTITY as QUANTITY37_1_, product3_.SKU as SKU2_, product3_.DESC as DESC39_2_, product3_.PRICE as PRICE39_2_ from CUSTOMER customer0_ left outer join ORDER customeror1_ on customer0_.OPEN_ORDER_ID=customeror1_.ORDER_ID left outer join LINEITEM lineitems2_ on customeror1_.ORDER_ID=lineitems2_.ORDER_ID left outer join PRODUCT product3_ on lineitems2_.PRODUCT_ID=product3_.SKU where customer0_.CUST_ID=?
Not sure why the orderId and productId props are becoming part of SQL.
Thanks for your help
|