Hibernate OneToMany双向无法识别外键

时间:2017-05-24 10:44:47

标签: java hibernate jpa

我正在研究一个教师项目,我们正在使用JPA来建立数据库。但是,我坚持这种一对多的关系。我试图让Waiter引用多个GuestOrders和GuestOrders并引用Waiter,但到目前为止还没有成功。

  • 服务员拥有许多GuestOrders
  • 许多GuestOrder属于一名服务员

服务员

@Entity
@Table(name = "waiter")
@PrimaryKeyJoinColumn(name = "USER_ID")
@DiscriminatorValue("W")
public class Waiter extends Worker {

    @OneToMany(mappedBy="waiter", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Collection<GuestOrder> activeGuestOrders;

    public Waiter() {
        this.activeGuestOrders = new ArrayList<>();
    }

    public Waiter(String email, String password, String name, String surname,
                  Date bithday, int shoeSize, int dressSize, Role role) {
        super(email, password, name, surname, bithday, shoeSize, dressSize, role);
        this.activeGuestOrders = new ArrayList<>();
    }


    public Collection<GuestOrder> getActiveGuestOrders() {
        return activeGuestOrders;
    }


    public void setActiveGuestOrders(Collection<GuestOrder> guestOrders) {
        this.activeGuestOrders = guestOrders;
    }

    public void addActiveOrder(String newOrder){
        GuestOrder newOne = new GuestOrder(newOrder);
        this.activeGuestOrders.add(newOne);
    }

    public void addActiveOrder(GuestOrder newGuestOrder){
        this.activeGuestOrders.add(newGuestOrder);
    }
}

GuestOrder

@Entity
public class GuestOrder {

    @GeneratedValue
    @Id
    private long order_id;

    @Column
    private String orders;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="user_id")
    private Waiter waiter;

    public Waiter getWaiter() {
        return waiter;
    }

    public void setWaiter(Waiter waiter) {
        waiter = waiter;
    }

    public GuestOrder() {}

    public GuestOrder(String name){
        this.orders = name;
    }
}

发生添加的部分代码:

    Waiter newWork = new Waiter(email, password, name, surname, bithday, shoeSize, dressSize,new Role(role));

    newWork.addActiveOrder("Order1");
    newWork.addActiveOrder("Order2");

    repository.flush();
    return repository.save(newWork);

我最终得到的是: GuestOrder Table Waiter Table

*抱歉小图片。

我已经尝试了谷歌设法找到的所有其他解决方案,但它没有帮助我。

3 个答案:

答案 0 :(得分:1)

如果你想在@OneToMany上使用级联来完成这项工作,那么你必须在关系的两边设置依赖关系:

Waiter newWork = new Waiter(email, password, name, surname, bithday, shoeSize, dressSize,new Role(role));

GuestOrder order1 = new GuestOrder();
GuestOrder order2 = new GuestOrder();

order1.setWaiter(newWork);
order2.setWaiter(newWork);

newWork.addActiveOrder(order1);
newWork.addActiveOrder(order2);

repository.flush();
return repository.save(newWork);

否则,持久性提供程序不会将此关系视为完全配置。

答案 1 :(得分:1)

GuestOrder未附加到waiter.Modify,如下所示,请尝试。

   public void addActiveOrder(String newOrder){
        GuestOrder newOne = new GuestOrder(newOrder);
        // waiter is now attached
        newOne.setWaiter(this);
        this.activeGuestOrders.add(newOne);
    }

答案 2 :(得分:1)

我遇到了类似的问题,所以我尝试了this 但是您将不得不处理无尽的JSON响应。 Maciej Kowalski是正确的,但是根据给定的参考,Waiter Class中有一些更改。

 public Waiter(String email, String password, String name, String surname,
              Date bithday, int shoeSize, int dressSize, Role role, Collection<GuestOrder> activeGuestOrders) {
    super(email, password, name, surname, bithday, shoeSize, dressSize, role );
    activeGuestOrders.forEach(x->x.setWaiter(this));
}

这样,您可以在控制器中单行声明它

Waiter newWork = new Waiter(email, password, name, surname, bithday, shoeSize, dressSize,new Role(role), activeGuestOrdersCollection );