Neuling zum Spring -Start hier. Ich möchte die gleiche Funktionalität. Ein Autor kann mehrere Bücher haben und auch ein Buch kann von mehr als einem Autoren gemeinsam geschrieben werden. Ich bin zu einem Ergebnis gekommen, in dem das Posten eines Autors wie beabsichtigt funktioniert, aber das Posten eines Buches aktualisiert die Join -Tabelle nicht, obwohl sowohl Benutzer als auch Büchertabellen aktualisiert werden. Meine Datenbank ist postgres.
// Buch mit Benutzer
// Einfügen in die Tabelle und Bücher in Benutzern einfügen, aber nicht in user_books table < /p>
{
"title":"The Da Vinci Code",
"releaseDate":"2003-03-18",
"content":"",
"genres":["Mystery", "detective fiction", "conspiracy fiction", "thriller"],
"authors":[
{
"password":"password",
"dateOfBirth":"2000-02-02",
"fullName":"Dan Brown",
"email": "[email protected]"
}
]
}
< /code>
Danke im Voraus!
(Java Version 18, Spring Boot 3.4.2) < /P.>
Neuling zum Spring -Start hier. Ich möchte die gleiche Funktionalität. Ein Autor kann mehrere Bücher haben und auch ein Buch kann von mehr als einem Autoren gemeinsam geschrieben werden. Ich bin zu einem Ergebnis gekommen, in dem das Posten eines Autors wie beabsichtigt funktioniert, aber das Posten eines Buches aktualisiert die Join -Tabelle nicht, obwohl sowohl Benutzer als auch Büchertabellen aktualisiert werden. Meine Datenbank ist postgres.[code]@Entity @Table @Getter @Setter @NoArgsConstructor public class Users {
public void addBooks(Set books){ if(this.role == Roles.AUTHOR){ for(Book book : books) try{ this.books.add(book); book.addAuthors(Set.of(this)); } catch(Exception e){ System.out.println("Wrong type of object for a List of Book"); continue; } } else { throw new IllegalArgumentException("Non author users cannot hold books"); } }
public void removeBooks(Set books){ for(Book book : books) for(Book user : this.books){ if (user.equals(book)) { this.books.remove(user); } else{ System.out.println("Book: "+book.getTitle()+ " is already not in set."); } } }
public void addAuthors(Set authors){ System.out.println("addAuthorsCalled"); for(Users author : authors) try{ this.authors.add(author); author.addBooks(Set.of(this)); author.setRole(Roles.AUTHOR); } catch(Exception e){ System.out.println("Wrong type of object for a List of Users"); } }
public void removeAuthors(Set authors){ if(authors.size() == 1){ System.out.println("Can't delete all authors for certain book"); return; } for(Users author : authors) for(Users user : this.authors){ if (user.equals(author)) { this.authors.remove(user); } else{ System.out.println("User: "+author.getFullName()+ " is already not in set."); } } } } < /code> Buchcontroller: < /p> @RestController @RequestMapping(path = "api/books") public class BookController {
private final BookService bookService; private UserService userService;
public void deleteUser(Long userId){ boolean exists = userRepository.existsById(userId); if(!exists){ throw new IllegalStateException("No user with Id: " + userId+ " in database"); } userRepository.deleteById(userId); }
@Transactional public void updateUser(Long userId, String password, LocalDate dateOfBirth, String fullName, String email, Set bookIds){ Users user = userRepository.findById(userId).orElseThrow(() -> new IllegalStateException("No user with id: "+userId+" in database")); if(password != null && password.length() > 0){ user.setPassword(password); } if(dateOfBirth!=null){ user.setDateOfBirth(dateOfBirth); } if(fullName!=null && fullName.length() > 0){ user.setFullName(fullName); } if(email!=null && email.length() > 0){ Optional userOptional = userRepository.findUserByEmail(email); if(userOptional.isPresent()){ throw new IllegalStateException("Email "+email+" already exists in database"); } user.setEmail(email); } if(bookIds != null){ Set booksToAdd = new HashSet(); for(Long bookId : bookIds){ booksToAdd.add(bookRepository.getReferenceById(bookId)); } user.addBooks(booksToAdd); } } } < /code> Buchdienst: < /p> @Service public class BookService {
private final BookRepository bookRepository;
public BookService(@Autowired BookRepository bookRepository){ this.bookRepository = bookRepository; }
public List getBooks(){ return bookRepository.findAll(); //List.of(new Book("kostas", LocalDate.of(2000, Month.APRIL, 12))); }
public void saveBook(Book book){
bookRepository.save(book); }
public void deleteBook(Long bookId){ boolean exists = bookRepository.existsById(bookId); if(!exists){ throw new IllegalStateException("Book with id: " + bookId + " does not exist"); } bookRepository.deleteById(bookId); }
@Transactional public void removeAuthors(Long bookId, Set authors){ Book book = bookRepository.findById(bookId). orElseThrow(() -> new IllegalStateException("No books with the id requested")); if(!authors.isEmpty()) book.removeAuthors(authors); }
@Transactional public void updateBook(Long bookId, String title, LocalDate releasDate, String content, Set authors){ Book book = bookRepository.findById(bookId). orElseThrow(() -> new IllegalStateException("No books with the id requested"));
// Buch mit Benutzer // Einfügen in die Tabelle und Bücher in Benutzern einfügen, aber nicht in user_books table < /p> { "title":"The Da Vinci Code", "releaseDate":"2003-03-18", "content":"", "genres":["Mystery", "detective fiction", "conspiracy fiction", "thriller"], "authors":[ { "password":"password", "dateOfBirth":"2000-02-02", "fullName":"Dan Brown", "email": "[email protected]" } ] } < /code> Danke im Voraus! (Java Version 18, Spring Boot 3.4.2) < /P.>
Ich arbeite an einem REST-Projekt mit Spring Boot und Hibernate und versuche derzeit herauszufinden, wie ich mit meiner JSON-Serialisierung umgehen kann. Image Beschreibung hier eingeben src =
Kann mir bitte jemand erklären, warum die Datenbank (kursiver und fetter Teil) nicht so funktioniert, wie sie sollte? Auch wenn ich die Daten per Postbote verschickt habe, gelangen meine Daten nicht...
Ich versuche, eine App mit C# EFCORE und SQL Server zu erstellen. Mein Problem ist es, einige grundlegende Konzepte rund um DB -Beziehungen zu definieren. Es wird meinen Mangel an Erfahrung und...