Ebean:@OneToMany到同一个实体

时间:2013-06-01 11:18:44

标签: java sql jpa playframework one-to-many

我有以下情况,用户可以在广告系列上进行预订。

EG。用户希望保留一个活动(例如,一本书可享八折优惠)。我希望预订表看起来像这样:| id | user_id | campaign_id |

用户可以进行多次预约。 广告系列可以由许多用户保留。 预订将包含用户保留的广告系列。

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

Reservations.java

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

为什么这不起作用?

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

1 个答案:

答案 0 :(得分:0)

您可以考虑这样做,

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

Reservation.java

@Entity
public class Reservation extends Model {
    @Id
    public Long id;
    @ManyToOne
    public User user;
    @ManyToOne
    public Campaign campaign;
}