Skip to content

Instantly share code, notes, and snippets.

@tomwhoiscontrary
Created August 25, 2011 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomwhoiscontrary/1171629 to your computer and use it in GitHub Desktop.
Save tomwhoiscontrary/1171629 to your computer and use it in GitHub Desktop.
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