Change and delete objects before persisting in DB

0

I have a functionality that aims to register customers, in which within such a register I have a relationship as follows:

A customer can have several "Service", and each service can contain several destinations and / or related escorts.

That is, a customer can join my "Agency" (the system in which I am committed to developing is a CRM for travel agencies), and the employee opens a "Customer Service "for that client.

With the service open I will have the register of both "Escorts" (related to who the client will travel), as well as the "Destinations", in which the customer has a potential interest.

This registration is given by opening a modal where I will have some basic fields to fill and that will be passed via AJAX for my action, so this action will store the objects within a list that later when the user of the system finishes all the registration of the "Attendance" will save, persisting the data in the DB.

Ok, so I did not have any problems. The bug starts to pick up when I have to edit or even delete that object, since it is in a list with id's for identification and treatment. Another problem is that if I give " refresh " on the page my list gets "populated" with the data the user typed previously becoming inconsistent.

Another alternative that I was thinking of doing (and maybe the most correct) would be that every time a "destiny" or "escort" register was made when I made my AJAX strong> (by going to action the information), I would persist immediately, but this method brings another problem as well. What if the user did not give up on the service? Of course my bank would be left with orphaned records, not to mention that I want to persist a list and it does not make much sense for me to persist an object to every Request .

I do not know if I was able to illustrate exactly the problem, but the main question is what would be the correct way to update such objects, before the parent entity persists.

EDITION

Below are the three entities involved in the process:

First we have the Customer.java entity, in case it can contain several customerService .

@Entity
@Table(name = "customer")
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_customer")
    private Long idCustomer;

    @Column(name = "birth_date")
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @Temporal(TemporalType.DATE)
    @NotNull(message = "A data de nascimento não pode estar vazia!")
    private Date birthDate;

    @Column(name = "email")
    @NotEmpty(message = "O E-mail não pode estar vazio!")
    private String email;

    @Column(name = "first_name")
    @NotEmpty(message = "O Nome não pode ser vazio!")
    private String firstName;

    @Column(name = "gender")
    private char gender;

    @Column(name = "last_name")
    private String lastName;

    @OneToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "fk_document")
    private Document document;

    @OneToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "fk_customerPhone")
    private CustomerPhone customerPhone;

    @OneToMany(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "fk_customer")
    private Set<Passenger> passenger;

    @OneToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "fk_customerAddress")
    private CustomerAddress customerAddress;

    @Column(name = "observations")
    private String observations;

    //Um Cliente pode ter varios atendimentos.
    @OneToMany(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "fk_customer")
    private Set<CustomerService> customerService;

    public Customer() {
    }

    // Getter Setter

2nd CustomerService.java which in turn has a list of destinationRequested

@Entity
@Table(name = "customer_service")
public class CustomerService implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_customer_service")
    private Long id;

    @Column(name = "date_service")
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @Temporal(TemporalType.DATE)
    private Date date;

    @Column(name = "average_budget")
    private BigDecimal averageBudget;

    @Column(name="service_situation")
    private boolean situation;

    @OneToMany(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "fk_customer_service")
    private Set<DestinationRequested> destinationRequested;

    @OneToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "fk_history")
    private History history;

    @Column(name = "service_observatons")
    private String serviceObservations;

    // Getter Setter

3º Finally we have the RequestedDestination.java entity that is equivalent to the destinations negotiated in each service

@Entity
@Table(name="destination_requested")
public class DestinationRequested implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_destination_requested")
    private Long id;

    private String name;

    private Double price;

    @Enumerated(EnumType.STRING)
    @Column(name="sale_type")
    private SaleType saleType;

    @Column(name="departure_date")
    @DateTimeFormat(pattern="dd/MM/yyyy")
    @Temporal(TemporalType.DATE)
    private Date departureDate;

    @Column(name="arrival_date")
    @DateTimeFormat(pattern="dd/MM/yyyy")
    @Temporal(TemporalType.DATE)
    private Date arrivalDate;

    //Getter and Setter
    
asked by anonymous 26.11.2014 / 17:17

1 answer

1

Hello.

I checked the entities and contacted you that you do not have the ManyToOne relations from CustomerService to Customer and from DestinationRequested to CustomerService. I have changed the classes a bit and I am posting here with comment on the changes made.

The only ones that needed adjustments were these:

CustomerService

@Entity
@Table(name = "customer_service")
public class CustomerService {
     private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_customer_service")
    private Long id;

    @Column(name = "date_service")
    @Temporal(TemporalType.DATE)
    private Date date;

    @Column(name = "average_budget")
    private BigDecimal averageBudget;

    @Column(name="service_situation")
    private boolean situation;

    @OneToMany(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "fk_customer_service")
    private Set<DestinationRequested> destinationRequested;

    @Column(name = "service_observatons")
    private String serviceObservations;

    //VOCE DEVE RELACIONAR MUITOS PARA UM O CUSTOMER
    @ManyToOne
    @JoinColumn(name = "customer_id", referencedColumnName = "id_customer")
    private Customer customer;
}

DestinationRequested

@Entity
@Table(name="destination_requested")
public class DestinationRequested {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_destination_requested")
    private Long id;

    private String name;

    private Double price;


    @Column(name="departure_date")
    @Temporal(TemporalType.DATE)
    private Date departureDate;

    @Column(name="arrival_date")
    @Temporal(TemporalType.DATE)
    private Date arrivalDate;


    //VOCE DEVE RELACIONAR MUITOS PARA UM O CUSTOMERSERVICE
    @ManyToOne
    @JoinColumn(name = "customer_id", referencedColumnName = "id_customer_service")
    private CustomerService customerService;
}

After making the changes I tested them and they are working. Anything gives a shout out there. Hugs

    
27.11.2014 / 18:00