【问题标题】:Join Three tables (Hibernate annotation exception, OneToMany, mappedBy)Join 三表(Hibernate 注解异常、OneToMany、mappedBy)
【发布时间】:2016-07-22 22:53:50
【问题描述】:

我无法加入三个表。见上图。如您所见,每家餐厅都可以有很多张桌子,每张桌子都可以有很多预订。

餐厅实体:

@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

感谢您的帮助!

【问题讨论】:

    标签: hibernate exception annotations entity


    【解决方案1】:

    在餐厅餐桌上

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

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

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

    将实体 RestaurantTable 更改为

    @OneToMany(mappedBy = "restaurantTable")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 2020-04-01
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多