Encountering issues in inserting data with duplicate IDs in a many-to-many relationship

I currently have a situation where I have two entities, Movie and Actor, that are linked together in a many-to-many relationship.

Here is the definition of the Movie entity:

public class Movie {
    @Id
    private  String imdbId;
    @ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER, targetEntity = Actor.class)
    private Set<Actor> actor;
}

And this is how the Actor entity is defined:

public class Actor {
    @Id
    private  String actorId;
}

In my RESTful API, I am posting JSON data to my MovieController. The structure of the JSON data is as follows:

{
  "imdbId": "tt2395424",
  "actor" : [{"actorId" : "0001"}, {"actorId" : "0002"}]
}

This functionality works perfectly fine and the actors are populated automatically. However, when I try to insert a new movie with the same actorId, it throws an error saying that the actor already exists. I am quite confused because according to my understanding, Hibernate should compare the id before insertion and only insert if it's not present. Can you please help me understand where I might be going wrong?

Answer №1

The problem lies within the implementation of the CascadeType.ALL above the actor attribute in the Movie class. Essentially, when using CascadeType.ALL, any actions performed on the parent entity will also be applied to its embedded entities. In this case, attempting to save the movie may result in an error if the actor's id already exists in the database. To avoid this issue, it is recommended to utilize the saveOrUpdate operation instead of solely relying on the save method for the movie entity.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble retrieving a value from a .JSON file (likely related to a path issue)

My React component is connected to an API that returns data: class Item extends Component { constructor(props) { super(props); this.state = { output: {} } } componentDidMount() { fetch('http://localhost:3005/products/157963') ...

Guide to populating a full calendar using JSON information

Implementing the FUllCALENDAR CSS template for creating a meeting calendar has been my current project. The servlet class I am using is CalendarController. However, when running it, the output appears as follows: {"events":[{"id":1,"title":"1","s ...

substitute the euro symbol in a JSON file

Is there anyone who can assist me with this issue? I have a query and it only returns invalid JSON after adding the last element that is indexed against the Euro currency. $url = 'http://www.google.com/finance/info?client=ig&q=goog,yhoo,AMS:TOM2 ...

Utilize the Serde format in AWS Athena to parse and extract arrays and nested arrays present in a JSON file

In my current process, I am extracting data individually from an array and nested arrays. However, I am looking for a way to extract all data from an array with a single statement, similar to a 'SELECT *'. Here is an example of what I am currentl ...

Strip the Python dictionary from the JSON file output

After researching various resources, such as a post on Stack Overflow titled Remove python dict item from nested json file, I am still struggling to make my code work. My JSON data is quite complex, with nested dictionaries and lists scattered throughout. ...

Manipulating JSON data fetched through AJAX beyond the success callback

I'm facing an issue with storing JSON data received via AJAX in an external variable for future use. I came across this answer on Stack Overflow (load json into variable), which provided some basic insights, but it seems like I might be missing someth ...

Using Twitter Bootstrap Modal for Rails form submissions with AJAX/JSON

I'm encountering a challenge with integrating a Rails form_for form into a Bootstrap modal and updating a table using AJAX. I've attempted to follow the guidance provided at , but without success. I have also explored the suggestions in the resou ...

Generating a JSON array using PHP

Hey there, I'm currently working on creating an array for easier access to some data in my ESP32 project. The issue I'm facing is with the Json 6 library - specifically, I can't seem to access the contents of the array as intended. For examp ...

Exploring JSON Data with NativeScript

As a newcomer to NativeScript and JSON, I am currently facing challenges in accessing data from my JSON file. My main goal right now is to simply log some of the data for debugging purposes. Below is the code snippet from my view-model: var config = requ ...

Steps for logging in using Spring Boot and Angular 2

My Front End application is built with Angular 2 and runs on http:// localhost:5555. Meanwhile, my Back End application uses Spring Boot and runs on http://localhost:8080/. It provides a REST API for my Angular 2 application. Sending requests from http:/ ...

Generating JSON responses using Django REST framework to facilitate table visualizations on the front end

I am facing an issue with my django-rest-framework where I am trying to display a table using the Google Chart data drawing library. However, I keep encountering an error message saying "You called the draw() method with a String rather than a DataTable or ...

Is it possible that Mongodb's aggregate function produces a Circular Json Object, whereas in the shell it only returns a regular

Currently, I am developing a Node.js application that is designed to return an array of random documents. To accomplish this task, I am utilizing the aggregate function from MongoDB along with the $sample operator. When executing a query like db.factslist ...

What is the best way to decode a JSON object containing geo:point data in PHP?

Similar Question: How to fetch object property with a negative sign? I am attempting to extract information from geo:point / geo:lat but I'm encountering difficulties. Below is the code snippet that I've developed so far $content = get_data ...

What is the best way to handle two different types of JSON within a single class?

The following is the structure of a JSON data: [{"trace":{"details":{"date":"[28-02-2016 11:04:26.856573]","type":"[info]","message":"[system done.]"},"context":{"context":[[{"ID":"john dillinger"}]]}}},{"trace":{"details":{"date":"[28-02-2016 11:04:26.85 ...

Show information from a JSON file in a tooltip on a Highcharts' pie chart

I have a pie chart that shows two percentages. I am looking to update the tooltip content to display information from my JSON data. Here is an example of how my JSON data looks: {"object1":{"percentage": 0.7, "numberOfObject": 167}, "object2":{"percentage ...

How come my associative array is being transformed into an object in PHP?

As I am working on creating an associative array, the following code snippet is utilized: public function getEnumFlag(){ $enums = Category::getPossibleEnumValues('flag'); $enumArray = array(); foreach($enums as $enum){ $enum ...

Using C# in ASP.NET to read a JSON file

I am seeking assistance on how to read a JSON file. I have created a class file and would like to save the JSON data into those class properties. { "header": { "hid": "95845-5bhj-j908-o987-hhg5665", "datestamp&q ...

Does the functionality of JSON.parse include recursion?

After receiving a JSON string in response, I parse it as follows: ring = JSON.parse(response); However, although ring becomes an object, the property ring.stones is only a string when it should also be an object. To address this issue, if I execute: ri ...

How can nodejs var be utilized as a json object declaration?

Can you help me understand how to incorporate a Node.js variable within a JSON statement? Here is a simplified version of my code: test.json: { "test1":["test1.1", "test1.2"], "test2":["test2.1", "tes ...

Having trouble importing from JSON in Python?

There is an issue with my code where one class dumps a list of strings into a .json file, and another class is supposed to load this file. However, I am encountering strange errors that are difficult for me to comprehend. The content of the .json file look ...