0

我有以下场景,用户可以对活动进行预订。

例如。用户想要预订一个活动(例如,一本书有 20% 的折扣)。我希望预订表如下所示:|id|user_id|campaign_id|

用户可以进行多次预订。活动可以被许多用户预订。Reservation 将包含哪些用户保留了哪些活动。

用户.java

@Entity
public class User extends Model {
    @Id
    public Long id;
    public String username;
    public String password;
    @OneToMany(cascade = CascadeType.PERSIST)
    public List<Reservation> reservations;
}

Campaign.java

@Entity
public class Campaign extends Model {
    @Id
    public Long id;
    public String name;
    @OneToMany(cascade = CascadeType.PERSIST)
    public List<Reservation> reservations;
}

预订.java

@Entity
public class Reservation extends Model {
    @Id
    public Long id;
}

这怎么行不通?

编辑:我得到一个有 id 和 user_id,但没有campaign_id 的预订表

4

1 回答 1

0

你可以考虑这样做,

用户.java

@Entity
public class User extends Model {
    @Id
    public Long id;
    public String username;
    public String password;
    @OneToMany(cascade = CascadeType.PERSIST,mappedBy="user")
    public List<Reservation> reservations;
}

Campaign.java

@Entity
public class Campaign extends Model {
    @Id
    public Long id;
    public String name;
    @OneToMany(cascade = CascadeType.PERSIST,mappedBy="campaign")
    public List<Reservation> reservations;
}

预订.java

@Entity
public class Reservation extends Model {
    @Id
    public Long id;
    @ManyToOne
    public User user;
    @ManyToOne
    public Campaign campaign;
}
于 2013-06-03T07:27:54.613 回答