Created
August 25, 2011 19:40
-
-
Save tomwhoiscontrary/1171629 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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