Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cascade save model not saving relationship

I have an Play2 application using eBean integrated with PostgreSQL database.

Basically I have a model TripRequest that as a list of Passengers . The use case is "A trip has several passengers".

For the TripRequest I have this model:

@Entity
public class TripRequest extends Model {

    enum Status {
        OPEN,
        PROCESSING,
        CLOSED,
        CANCELLED
    }

    @Id
    private UUID id;

    @ManyToOne
    @Column(nullable = false)
    private User owner;

    @OneToMany(mappedBy = "tripRequest")
    @JoinTable(name = "trip_passengers")
    private List<Passenger> passengers;
    (... other fields, getters and setters ...)

And this is my Passenger model:

@Entity
@Table(name = "trip_passenger")
public class Passenger extends Model {

    @ManyToOne(cascade = CascadeType.ALL)
    private TripRequest tripRequest;

    @Column(nullable = false)
    private String name;
    (... other fields, getters and setters)

So, I create a new instance of TripRequest, several instances of Passenger and set that list as the passengers of TripRequestinstance. Problem is that when I do tripRequest.save() everything in the main instance (TripRequest) gets saved but the relations are not (so the passengers list).

I read in the documentation and examples and everything is pointing me to the CascadeType.ALL in the @ManyToOne annotation but no "luck" at all

EDIT: if you think the complete classes are useful please let me know as I can post them. Just decided to keep the example short so I trimmed them.

like image 399
Miguel Ribeiro Avatar asked Oct 23 '15 21:10

Miguel Ribeiro


1 Answers

Your @OneToMany(mappedBy = "tripRequest") ... has no cascade = CascadeType.ALL (or CascadeType.PERSIST).

This means the save on TripRequest is not cascading on the list of passengers.

like image 106
Rob Bygrave Avatar answered Sep 23 '22 08:09

Rob Bygrave