Lupus
Lupus

Reputation: 33

Hibernate OneToMany bidirectional not recognizing foreign keys

I am working on a faculty project and we are using JPA for database. However, i got stuck on this one-to-many relationship. I am trying to make Waiter with an reference to multiple GuestOrders and GuestOrders with an reference to Waiter but so far unsuccessfully.

Waiter

@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;
    }
}

Part of code where the addition occurs:

    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);

What state I end up with is: GuestOrder Table Waiter Table

*Sorry for small pictures.

I have tried every other solution I managed to find by google but it didn't help me.

Upvotes: 1

Views: 83

Answers (3)

SQLProfiler
SQLProfiler

Reputation: 111

I was facing similar problem so I tried this But you will have to deal with endless JSON response. Maciej Kowalski is correct but here are some changes in Waiter Class according to given reference.

 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));
}

This way you can just declare it in controller in a single line

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

Upvotes: 1

Puppala Mounika
Puppala Mounika

Reputation: 123

The GuestOrder is not attached to waiter.Modify as below and try.

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

Upvotes: 1

Maciej Kowalski
Maciej Kowalski

Reputation: 26572

If you want to make this work using the cascade, on the @OneToMany, then you have to set the dependencies on both sides of the relationship:

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);

Otherwise the persistence provider will not consider this relationship as fully configured.

Upvotes: 1

Related Questions