加入三个表(Hibernate注释异常,OneToMany,mappedBy)

时间:2016-04-03 11:49:59

标签: hibernate exception annotations entity

enter image description here

我不能加入三个表。见上图。正如您所看到的,每家餐厅都有很多桌子,每张桌子都可以有很多预订。

餐厅实体:

@Entity
@Table(name = "restaurant")
public class Restaurant {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "name")
    private String name;
    @Column(name = "aadress")
    private String aadress;

    @OneToMany(mappedBy = "restaurant")
    private List<RestaurantTable> restaurantTables;
}

RestaurantTable实体:

@Entity
@Table(name = "restaurant_table")
public class RestaurantTable {

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

    @ManyToOne
    @JoinColumn(name = "restaurant_id")
    private Restaurant restaurant;

    @Column(name = "number")
    private int number;
    @Column(name = "count")
    private int count;

    @OneToMany(mappedBy = "restaurant_table")
    private List<Booking> bookings;
}

预订实体:

@Entity
@Table(name = "booking")
public class Booking {

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

    @ManyToOne
    @JoinColumn(name = "restaurant_table_id")
    private RestaurantTable restaurantTable;

    ...
}

Hibernate注释异常:

org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: Booking.restaurant in RestaurantTable.bookings

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

在RestaurantTable中

@OneToMany(mappedBy = "restaurant_table")
private List<Booking> bookings;

您指的是预订# restaurant_table ,但在预订实体类中,此属性名为 restaurantTable

@ManyToOne
@JoinColumn(name = "restaurant_table_id")
private RestaurantTable restaurantTable;

将实体RestaurantTable更改为

@OneToMany(mappedBy = "restaurantTable")