Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package uk.co.initech.sandbox;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
@Entity
public class OrderLine {
@Embeddable
public static class Key {
private String orderId;
private String productId;
public Key(String orderId, String productId) {
this.orderId = orderId;
this.productId = productId;
}
@Override
public int hashCode() {
return orderId.hashCode() + (productId.hashCode() << 1);
}
@Override
public boolean equals(Object that) {
return (this == that) || ((that instanceof Key) && this.orderId.equals(((Key) that).orderId) && this.productId.equals(((Key) that).productId));
}
}
@EmbeddedId
private Key id;
@ManyToOne
@MapsId("orderId")
private Order order;
private int quantity;
public OrderLine(Order order, String productId, int quantity) {
this.id = new Key(order.getId(), productId);
this.order = order;
this.quantity = quantity;
}
protected OrderLine() {}
public Key getId() {
return id;
}
public Order getOrder() {
return order;
}
public String getProductId() {
return id.productId;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public int hashCode() {
return getId().hashCode();
}
@Override
public boolean equals(Object that) {
return (this == that) || ((that instanceof OrderLine) && this.getId().equals(((OrderLine) that).getId()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.