Change and delete objects before persisting in BD

Asked

Viewed 137 times

0

I have a feature that aims to register clients, in which within such registration I have a relationship as follows:

A customer may have multiple "Attendances", and each service may contain several related destinations and/or companions.

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

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

Such registration is provided by the opening of a modal in which I will have some basic fields to fill and that will be passed via AJAX for my action, soon this action will store the objects inside a list that later when the system user finishes the entire registration of "Service" will save, persisting the data in the BD.

Okay, so far, no problem at all. The bug starts to pick up when I have to edit or even delete such object, because the same is in a list without id’s for identification and treatment. Another problem is that "refresh" on the page my list becomes "populated" with the data in which the user previously typed becoming inconsistent.

Another alternative that I was thinking about doing (and perhaps the most correct one), would be that every time some registration of "destinations" or "escorts" was made when I made my call AJAX (going into the action the information), I would persist immediately, but this method brings another problem too. And if the user gave up on the service? Logically my bank would get orphaned records, not to mention I’m willing to persist a list and it doesn’t make much sense for me to keep persisting an object each Request.

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

EDITION

Below are the three entities involved in the process:

1st We have below the entity Customer.java, in the case it may 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

Last we have the entity of RequestedDestination.java which is equivalent to the destinations negotiated at 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
  • 2

    Good afternoon. Can you post the code of your Entities? I believe it only takes an adjustment in your relationships to solve your problem, but for that you need to put your entities. Can you do that? Thank you

  • Hi @Gilvanandré, I edited the question with the entities.

1 answer

1

Hello.

I have checked the entities and contacted that you do not have the Manytoone relations of Customerservice to Customer and Destinationrequested to Customerservice. I changed some classes and am posting here with comment on the changes made.

The only ones that needed adjusting 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. Something to scream about. Hugs

  • Hello. You’ve already solved your problem?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.