0

I have got two tables orders and orderlines. When i try to join two columns,i am getting this exception.

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

My entity classes : 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",  
            joinColumns=@JoinColumn(name="product_id")  
                )  
    private Product product;

My Tables:

orders

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`)
);

Please help me to solve this.

user3785322
  • 153
  • 1
  • 6
  • 15
  • You are creating the relationship incorrectly. Have a look at my tutorial [here](http://stackoverflow.com/questions/24257449/how-do-i-use-annotations-to-define-x-relationship-in-hibernate-4-and-spring). That should be able to get you started. – JamesENL Jul 15 '14 at 05:05
  • Thanks for the tutorial. I will check now. Can you please point where i am making mistake. – user3785322 Jul 15 '14 at 05:12
  • I'm writing an answer now. You really don't need a join table because you are doing a one-to-many not many-to-many – JamesENL Jul 15 '14 at 05:13

1 Answers1

0

You appear to be creating your mapping incorrectly. You only need a bi-directional one-to-many with a foreign key mapping. Try this:

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;
}

You are really overcomplicating your design. You already have a foreign key on the OrderLine table, so why not use that instead of specifying 2 different join tables for this mapping. The above code should work out to be a bi-directional one to many mapping.

Also, as a side note, don't make the PK of OrderLines a composite of Order and Product Id's, just give it its own ID column. It'll make your life a ton easier.

JamesENL
  • 6,400
  • 6
  • 39
  • 64