#Update an entity that had many relationships?

1 messages · Page 1 of 1 (latest)

tough glacier
#

Hello everyone.
I have this java class that's created from an XML file wich ends up being an entity with a lot of relationships (mostly OneToMany, OneToOne relaitonsships) and I need to save/update it in my DB. Saving it works fine but when I need to update it, I'm clueless (been a while since I've done backend stuff). Here's some part of the classes/entities

`@Data
@Entity
public class JourPresse {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqGenerator")
@SequenceGenerator(name = "seqGenerator", sequenceName = "seqNameJP", initialValue = 1, allocationSize = 1)
private Long id;

@JsonFormat
  (shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
@Column(name = "DATE_JP")
private LocalDate date;

private String chaine;
private Integer semaine;
private String editorialDeLaSemaine;
private String editorialNewsletter;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "JOUR_PRESSE_ID", referencedColumnName = "ID")
private Set<RecordPresse> recordsPresse;

}

@Data
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class RecordPresse {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqGenerator")
@SequenceGenerator(name = "seqGenerator", sequenceName = "seqNameRP", initialValue = 1, allocationSize = 1)
private Long id;

private Integer newsLetter;
private String layout;
private String signaletique;

@OneToOne(cascade = CascadeType.ALL)
private CodeEscort codeEscort;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "RECORD_PRESSE_ID", referencedColumnName = "ID")
private Set<Photo> photos;

}`

For the moment I'm doing the following in my JourPresseService (JourPresse being the root of my xml file:
(In my next message). So my question is how I should handle the update of such a class when the one in my project has maybe 30 different relations with other entities?

#

`public JourPresse saveJourPresse(JourPresse jourPresse) {
LocalDate date = jourPresse.getDate();
String chaine = jourPresse.getChaine();

    JourPresse jourPresseInDb = jourPresseRepository.findJourPresseByChaineAndDate(chaine, date);

    if (jourPresseInDb != null) {
        Long existingId = jourPresseInDb.getId();
        try {
            BeanUtils.copyProperties(jourPresseInDb, jourPresse);
            jourPresseInDb.setId(existingId);
            jourPresse = jourPresseInDb;
        } catch (ReflectiveOperationException e) {
            throw new RuntimeException("Error updating JourPresse", e);
        }
    }
    return jourPresseRepository.save(jourPresse);
}`
edgy plankBOT
#

<@&987246584574140416> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

ne, ManyToMany). Now I need to update this entity with new data from another XML file. What would be the best approach to do this?

One possible approach to update an entity with new data from another XML file is as follows:

  1. Parse the XML file and extract the relevant data.
  2. Retrieve the existing entity from the database based on some unique identifier (e.g., primary key).
  3. Update the fields of the entity with the new data obtained from the XML file.
  4. Handle any relationships that need to be updated.

For OneToMany relationships:

  • If there are new child entities in the XML file, create them and associate them with the parent entity.
  • If there are existing child entities in the database that are not present in the XML file, remove them from the parent entity's collection.

For OneToOne relationships:

  • If there is a new related entity in the XML file, create it and associate it with the parent entity.
  • If there is an existing related entity in the database that is not present in the XML file, remove its association with the parent entity.

For ManyToMany relationships:

  • Determine which related entities should be added or removed based on their presence in both the existing entity and XML data.
  • Add or remove associations accordingly.
  1. Save or update the modified entity back to the database.

It's important to handle cascading operations properly if they are defined for these relationships (e.g., cascade persist, merge, etc.). Additionally, consider using transactions to ensure atomicity and consistency during updates.

Remember to handle any potential exceptions or errors that may occur during parsing, retrieval, or updating of entities.