Que je tableaux (livres et auteurs ont navire relation ManyToMany)Mise en veille prolongée: insérez ManyToMany, générer ID dans le tableau de la relation
LIVRES
id
book_name
AUTEURS
id
auteur_name
BOOKS_AUTHORS
id
book_id
AUTHOR_ID
I Carte Esta Tableaux sur les entités
class Books
{
@Id
long id;
@Column(name = "author_name")
String name;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "BOOKS_AUTHORS",
joinColumns = @JoinColumn(name = "book_id"),
inverseJoinColumns = @JoinColumn(name = "author_id"))
List<Authots> authors;
// setter, getters, adder
}
class Authors
{
@Id
long id;
@Column(name = "author_name")
String name;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "BOOKS_AUTHORS",
joinColumns = @JoinColumn(name = "author_id"),
inverseJoinColumns = @JoinColumn(name = "book_id"))
List<Books> books;
// setter, getters, adder
}
Maintenant, j'essaie faire ensuite
public void addAuthor(final String bookName, final Author author)
{
final Book book = // get book from database by bookName
if (book == null)
throw new DataNotFoundException("Book not found :(");
author.addBook(book);
book.addAuthor(author);
entityManager.persist(author);
}
1) Je reçois exception ID L'enregistrement BOOKS_AUTHORS ne doit pas être NULL
2) Comment je c Générer un ID (iste générateur de sequance) pour BOOKS_AUTHORS-table de relation
De cette façon, je peux générer un ID pour l'entité Auteur et pour l'entité Livre. Mais j'ai besoin de générer un ID pour la table BOOKS_AUTHORS, qui n'est pas mappée – Ilya
Voir la version éditée de ma réponse, book_authors a besoin de son incrément auto id au niveau db. – Bitmap