Created
August 25, 2011 18:26
-
-
Save tomwhoiscontrary/1171380 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 java.util.HashSet; | |
import java.util.Set; | |
import javax.persistence.CascadeType; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.OneToMany; | |
import javax.persistence.Table; | |
@Entity | |
@Table(name="Order_") | |
public class Order { | |
@Id | |
private String id; | |
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL) | |
private Set<OrderLine> lines = new HashSet<OrderLine>(); | |
public Order(String id) { | |
this.id = id; | |
} | |
protected Order() {} | |
public String getId() { | |
return id; | |
} | |
public void setLines(Set<OrderLine> lines) { | |
this.lines = lines; | |
} | |
public Set<OrderLine> getLines() { | |
return lines; | |
} | |
@Override | |
public int hashCode() { | |
return id.hashCode(); | |
} | |
@Override | |
public boolean equals(Object that) { | |
return (this == that) || ((that instanceof Order) && this.id.equals(((Order) that).id)); | |
} | |
} |
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.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.IdClass; | |
import javax.persistence.ManyToOne; | |
@Entity | |
@IdClass(OrderLine.Key.class) | |
public class OrderLine { | |
public static class Key { | |
private String order; | |
private String productId; | |
public Key(String order, String productId) { | |
this.order = order; | |
this.productId = productId; | |
} | |
@Override | |
public int hashCode() { | |
return order.hashCode() + (productId.hashCode() << 1); | |
} | |
@Override | |
public boolean equals(Object that) { | |
return (this == that) || ((that instanceof Key) && this.order.equals(((Key) that).order) && this.productId.equals(((Key) that).productId)); | |
} | |
} | |
@Id | |
@ManyToOne | |
private Order order; | |
@Id | |
private String productId; | |
private int quantity; | |
public OrderLine(Order order, String productId, int quantity) { | |
this.order = order; | |
this.productId = productId; | |
this.quantity = quantity; | |
} | |
protected OrderLine() {} | |
public Key getId() { | |
return new Key(order.getId(), productId); | |
} | |
public Order getOrder() { | |
return order; | |
} | |
public String getProductId() { | |
return 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