好吧,所以今天我花了一整天的時間試圖找出如何映射Orders,Products和OrderLine之間的關係與使用annotation所有java配置的hibernate之間的關係。訂單產品和Order_Line與複合鍵的休眠關係
我希望OrderLine成爲具有額外字段的聯結表,即加入產品和訂單。
訂單與OrderLine有一個OneToMany關係綁定 - 一個訂單有很多訂單行。
每個訂單有一個或多個訂單行,每個訂單行有一個訂單,每個訂單行有一個產品,每個產品可以有0個或多個訂單行。
從我一直遵循的教程,有兩種方法可以做到這一點。一個是@Embeddable,@EmbeddedId批註,另一個是@IdClass批註,我試過都沒有成功,我會在@IdClass方法中發佈自己的代碼。
我的訂單類
@Entity
@Table(name="orders")
public class Order {
@Id
@Column(name = "order_id")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "order")
private Set<OrderLine> orderLines = new HashSet<>();
...some extra properties relevant to all orders
public Order(){}
...setters/getters
}
我的產品類
@Entity
@Table(name="products")
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "product_id")
public Integer product_id; // primary key
@OneToMany(fetch = FetchType.EAGER, mappedBy = "product")
private Set<OrderLine> orderLines = new HashSet<>();
...other properties
public Product() {
}
...setters/getters
}
這裏是我的訂單行類
@Entity
@IdClass(OrderLineId.class)
@Table(name="order_line")
public class OrderLine implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "product_id", insertable= false, updatable= false)
public Integer product_id;
@Id
@Column(name = "order_id", insertable= false, updatable= false)
public Integer order_id;
@ManyToOne
@JoinColumn(name = "product_id", insertable = false, updatable = false)
private Product product;
@ManyToOne
@JoinColumn(name = "order_id", insertable = false, updatable = false)
private Order order;
@Column(name = "product_quantity", unique = false, nullable = false)
private int productQuantity;
...additional fields associated with each OrderLine
最後我OrderLineId實施
public class OrderLineId implements Serializable {
private static final long serialVersionUID = 1L;
private Integer order_id;
private Integer product_id;
public OrderLineId(){}
public OrderLineId(Integer order_id, Integer product_id) {
this.order_id = order_id;
this.product_id = product_id;
}
@Column(name = "order_id")
public Integer getOrder() {
return this.order_id;
}
public void setOrder(Integer order_id) {
this.order_id = order_id;
}
@Column(name = "product_id")
public Integer getProduct() {
return this.product_id;
}
public void setProduct(Integer product_id) {
this.product_id = product_id;
}
@Override
public int hashCode() {
return order_id + product_id;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof OrderLineId){
OrderLineId orderLineId = (OrderLineId) obj;
return orderLineId.order_id == order_id && orderLineId.product_id == product_id;
}
return false;
}
}
這裏是我得到
MySQLSyntaxErrorException: Unknown column 'orderlines0_.order' in 'field list'
例外,當我訪問以下
@RequestMapping(value = "/testorder", method = RequestMethod.GET)
public ModelAndView testOrder(){
Order order = orderService.findOrderById(23);
Product product = productService.findProduct(2);
OrderLine ol = new OrderLine(product, order);
ol.setSize("large");
ol.setProductQuantity(30);
orderService.saveOrderLine(ol);
return null;
}
終點可以請別人幫我,這是推動我瘋了....
謝謝
您好,感謝您的評論。那麼這就是我原來的代碼,但是我需要在函數表中有額外的字段,所以一個簡單的ManyToMany關聯將不起作用 - OrderLine將有大約6個與它相關的屬性,'product_size','product_color',等現在有意義嗎?還有,@Id的多次使用是特意完成的,我構建的教程是在構造從其他實體主鍵派生的組合鍵時執行的。謝謝 – 2014-11-25 18:10:37