EntityTo establish a relationship inside, we mapped it using the @AnyToOne
annotation. For example:
@Entity
public class Book {
@Id
@GeneratedValue
private long id;
@Column
private String name;
@ManyToOne(fetch = FetchType.LAZY)
private Publisher publisher
// // getter & setter
}
@Entity
public class Publisher {
@Id
@GeneratedValue
private long id;
@Column
private String name;
// // getter & setter
}
In order to store the value of publisher
when each generated table name is book
, publisher
and PK
(=id) is 1, we tried to deliver the JONSODE as follows.
Controller:
@PutMapping(value = "/books")
public ResponseEntity createBook(@RequestBody Book book) {
bookRepository.save(book);
// Omitted
}
the body of the request :
{
"name" : "jpa in action",
"publisher" : "1"
}
I roughly summarized the contents and wrote it. The problem is that if you pass the body of the request as shown above, the Publisher
entity does not have a constructor with a single numeric type parameter and cannot process the request.
Is there a way to inquire and store the value automatically referenced by passing The actual database table contains a value of PK
(=id) in the body of the request, just as PK
(=id) using a pre-implemented function such as FindOne
? Or in the logic above, should I take a separate PK
(=id) value as a field, look up Publisher
directly, and manually work on Book
object like Foreign Key
which would be publisher_id
in the above situation. Therefore, even if "publisher_id" : 1,
was added to the request body, the value was not saved in the publisher_id
column of the actual table. What should I do?
When you save it, you have to do it with several strategies. according to the nature of one's work Decide which policy you want to take the cascade policy first.
Before saving by default in JPA, search for findOne to determine whether to update or save new content.
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
612 GDB gets version error when attempting to debug with the Presense SDK (IDE)
915 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.