@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) private EntityAuthor getAuthorByNameAndSurname(EntityAuthor entityAuthor) { Query query = em.createNamedQuery(EntityAuthor.FIND_BY_NAME_AND_SURNAME); query.setParameter("name", entityAuthor.getName()); query.setParameter("surname", entityAuthor.getSurname()); EntityAuthor author = null; try { author = (EntityAuthor) query.getSingleResult(); } catch (NoResultException exception) { } return author; }
@Override public void addBook(EntityBook book, EntityRelease release, EntityAuthor author) { // Autor se umisti do databaze. if (author.getId() != null) { author = em.merge(author); em.refresh(author); } else { em.persist(author); } // Propoji se autor s knihou. book.getAuthorCollection().add(author); author.getBookCollection().add(book); em.persist(book); em.persist(author); // Vytvori se nove vydani knihy. release.setBook(book); book.getReleasesCollection().add(release); em.persist(book); em.persist(release); // Ziska se uzivatel. Principal principal = sessionContext.getCallerPrincipal(); EntityUser user = beanSessionUser.getUserByEmail(principal.getName()); // Vytvori se novy vytisk knihy. EntityPrint print = new EntityPrint(); print.setRelease(release); release.getPrintsCollection().add(print); print.setOwnershipType(EntityPrint.EnumOwnershipType.PHYSICAL); print.setReadStatus(EntityPrint.EnumReadStatus.UNREAD); print.setUser(user); user.getPrintsCollection().add(print); em.persist(print); em.persist(release); em.persist(user); em.flush(); }