2014-07-15 259 views
0

我有兩個表命令和orderlines。當我嘗試加入兩列時,我得到此異常。休眠映射異常

org.hibernate.MappingException: Unable to find column with logical name: order_id in org.hibernate.mapping.Table(orders) and its related supertables and secondary tables 

我的實體類: Order.java

@Entity 
@Table(name="orders") 
public class Order { 

    @Id 
    @GeneratedValue 
    @Column(name="id") 
    private int id; 

    @Column(name="firstname") 
    private String firstName; 

    @OneToMany(targetEntity=OrderLine.class, 
      cascade = CascadeType.ALL, 
      fetch = FetchType.LAZY) 
    @JoinTable( 
      name="orderlines", 
      joinColumns = {@JoinColumn(table = "orderlines", name="FK_orders_orders", 
      referencedColumnName = "order_id")}, 
      inverseJoinColumns={@JoinColumn(table = "orders", name="FK_orders_orders", 
      referencedColumnName = "id")} 
      ) 
    private Set<OrderLine> orderLines; 

OrderLine.java

@Entity 
@Table(name="orderlines") 
public class OrderLine { 
    @Id  
    @ManyToOne(targetEntity = Order.class, 
      cascade = CascadeType.ALL, 
      fetch = FetchType.LAZY) 
    @JoinTable( 
      name="orders", 
      joinColumns={@JoinColumn(table="orders", name="FK_orders_orders", 
             referencedColumnName="id")}, 
      inverseJoinColumns={@JoinColumn(table="orderlines", name="FK_orders_orders", 
               referencedColumnName="order_id")}) 
    private Order order; 

    @ManyToOne(targetEntity = Product.class, 
      cascade = CascadeType.ALL, 
      fetch = FetchType.LAZY) 
    @JoinTable( 
      name="products", 
      [email protected](name="product_id") 
       ) 
    private Product product; 

我的表:

個訂單

CREATE TABLE `orders` ( 
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
`firstname` VARCHAR(50) NOT NULL, 
); 

orderlines

CREATE TABLE `orderlines` (
orderLineId int(11) NOT NULL auto_increment, 
`orderId` INT NOT NULL, 
`product_id` INT NOT NULL, 
`quantity` INT NOT NULL, 
`product_price` DECIMAL(19,2) NOT NULL, 
`total_price` DECIMAL(19,2) NOT NULL, 
PRIMARY KEY(orderLineId), 
KEY `FK_orders_orders` (`orderId`), 
CONSTRAINT `FK_orders_orders` FOREIGN KEY(`orderId`) REFERENCES `orders`(`id`), 
KEY `FK_orders_products`(`product_id`), 
CONSTRAINT `FK_orders_products` FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) 
); 

請幫我解決這個問題。

+0

您正在錯誤地創建關係。看看我的教程[這裏](http://stackoverflow.com/questions/24257449/how-do-i-use-annotations-to-define-x-relationship-in-hibernate-4-and-spring) 。這應該能夠讓你開始。 – JamesENL

+0

感謝您的指導。我會現在檢查。你能指出我犯錯的地方嗎? – user3785322

+0

我正在寫一個答案。你真的不需要一個連接表,因爲你正在做一對多非多對多 – JamesENL

回答

0

您似乎錯誤地創建了您的映射。你只需要一個雙向一對多的外鍵映射。試試這個:

public class Order{ 
    @Id 
    @GeneratedValue 
    @Column(name="id") 
    private int id; 

    @OneToMany(mappedBy = "order") 
    private Set<OrderLine> orderLines; 
} 

public class OrderLine{ 
    @Id 
    @GeneratedValue 
    @Column(name="orderLineId") 
    private int orderLineId; 

    @ManyToOne 
    @JoinColumn(name = "orderId") 
    private Order order; 
} 

你實在太過於複雜的設計。你已經在OrderLine表上有一個外鍵,所以爲什麼不使用它,而不是爲這個映射指定2個不同的連接表。上面的代碼應該是一個雙向的一對多映射。另外,作爲一個附註,不要讓OrderLines的PK成爲訂單和產品ID的組合,只需給它自己的ID列即可。它會讓你的生活變得更輕鬆。

+0

你能不能幫我一起表。 – user3785322

+0

你的表不應該有任何錯誤,只需將'orderlines'上的PK更改爲一個自動遞增的新列int – JamesENL

+0

我收到此異常org.hibernate.exception.ConstraintViolationException:無法插入: – user3785322