我有兩個表命令和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`)
);
請幫我解決這個問題。
您正在錯誤地創建關係。看看我的教程[這裏](http://stackoverflow.com/questions/24257449/how-do-i-use-annotations-to-define-x-relationship-in-hibernate-4-and-spring) 。這應該能夠讓你開始。 – JamesENL
感謝您的指導。我會現在檢查。你能指出我犯錯的地方嗎? – user3785322
我正在寫一個答案。你真的不需要一個連接表,因爲你正在做一對多非多對多 – JamesENL