Search code examples
javaspringjpaspring-dataone-to-many

Java Spring Data OneToMany relationschip Error


Could someone please help with one question. I have made objects structure as in documentation, but relationschip OneToMany doesn't work.

    Order:
    - Positions
    - Partners

    I receiving error:

    SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order (order_id)' at line 1

Order:

@Getter
@Setter
@Entity
@Table(name = "e_orders")
public class Order {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "order_id")
    private int orderId;

    @Column(unique = true)
    private String orderNumber;
    private int count;

    @OneToMany(mappedBy = "e_orders", fetch = FetchType.LAZY)
    private List<Partner> partners;

    @OneToMany(mappedBy = "e_orders", fetch = FetchType.LAZY)
    private List<Position> positions;
}

Partner:

@Getter
@Setter
@Entity
@Table(name = "e_partner")
public class Partner {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "partner_id")
    private int id;

    private String name;
    private String street;
    private String city;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @JoinColumn(name = "order_e_id")
    private Order attachedOrder;
}

Position:

@Getter
@Setter
@Entity
@Table(name = "e_position")
public class Position {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "position_id")
    private int id;

    private String guid;
    private String posnr;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @JoinColumn(name = "order_e_id")
    private Order attachedOrder;
}

Solution

  • I belive that the problem is in your mappedBy values, coz the value for the mappedBy should be the same as a property name in corresponding classes, not as in a @Table annotation. So to fix the issue just change values in mappedBy form e_orders to attachedOrder