Test book fix

develop
Meliurwen 5 years ago
parent e9d766a54b
commit 708184146f
  1. 12
      mivan/src/main/java/mivan/model/Book.java
  2. 4
      mivan/src/main/java/mivan/repository/AuthorRepository.java
  3. 4
      mivan/src/main/java/mivan/repository/AuthorRepositoryImpl.java
  4. 9
      mivan/src/main/java/mivan/repository/BookRepository.java
  5. 13
      mivan/src/main/java/mivan/repository/BookRepositoryImpl.java

@ -17,11 +17,11 @@ import javax.persistence.Table;
@Table(name = "book") @Table(name = "book")
public class Book implements Serializable { public class Book implements Serializable {
private static final long serialVersionUID = -5124436115031696628L; private static final long serialVersionUID = 19L;
@Id @Id
@Column(name = "isbn", nullable=false) @Column(name = "isbn", nullable=false, unique=true)
private int isbn; private long isbn;
@Column(name = "title", nullable=false) @Column(name = "title", nullable=false)
private String title; private String title;
@ -39,14 +39,14 @@ public class Book implements Serializable {
public Book() { public Book() {
} }
public Book(int isbn, String title, Book prequel) { public Book(long isbn, String title, Book prequel) {
this.isbn = isbn; this.isbn = isbn;
this.title = title; this.title = title;
this.prequel = prequel; this.prequel = prequel;
this.authors = new ArrayList<Author>(); this.authors = new ArrayList<Author>();
} }
public int getIsbn() { public long getIsbn() {
return isbn; return isbn;
} }
@ -58,7 +58,7 @@ public class Book implements Serializable {
return prequel; return prequel;
} }
public void setIsbn(int isbn) { public void setIsbn(long isbn) {
this.isbn = isbn; this.isbn = isbn;
} }

@ -12,9 +12,9 @@ public interface AuthorRepository extends Repository<Author, Long> {
public Author searchAuthorByName(String name); public Author searchAuthorByName(String name);
public void addBook(int isbn, long id); public void addBook(Long isbn, Long id);
public void removeBook(int isbn, long id); public void removeBook(Long isbn, Long id);
public int getSize(); public int getSize();
} }

@ -115,7 +115,7 @@ public class AuthorRepositoryImpl implements AuthorRepository {
} }
@Override @Override
public void addBook(int isbn, long id) { public void addBook(Long isbn, Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try { try {
if (!entityManager.getTransaction().isActive()) { if (!entityManager.getTransaction().isActive()) {
@ -134,7 +134,7 @@ public class AuthorRepositoryImpl implements AuthorRepository {
} }
@Override @Override
public void removeBook(int isbn, long id) { public void removeBook(Long isbn, Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try { try {
if (!entityManager.getTransaction().isActive()) { if (!entityManager.getTransaction().isActive()) {

@ -1,23 +1,22 @@
package mivan.repository; package mivan.repository;
import mivan.model.Author;
import mivan.model.Book; import mivan.model.Book;
public interface BookRepository extends Repository<Book, Long>{ public interface BookRepository extends Repository<Book, Long>{
//public Optional<AddressUser> findUserAddressById(Long id); //public Optional<AddressUser> findUserAddressById(Long id);
public void deleteBookById(int isbn); public void deleteBookById(Long isbn);
public void addBook(Book book); public void addBook(Book book);
public void updateBook(int isbn, String title, Book prequel); public void updateBook(Long isbn, String title, Book prequel);
public Book searchBookByTitle(String title); public Book searchBookByTitle(String title);
public void addAuthor(int isbn, long id); public void addAuthor(Long isbn, Long id);
public void removeAuthor(int isbn, long id); public void removeAuthor(Long isbn, Long id);
public int getSize(); public int getSize();
} }

@ -5,6 +5,7 @@ import java.util.Optional;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import mivan.model.Author; import mivan.model.Author;
import mivan.model.Book; import mivan.model.Book;
@ -13,6 +14,10 @@ public class BookRepositoryImpl implements BookRepository {
private EntityManagerFactory entityManagerFactory; private EntityManagerFactory entityManagerFactory;
public BookRepositoryImpl(){
this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan");
}
@Override @Override
public Optional<Book> findById(Long id) { public Optional<Book> findById(Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
@ -30,7 +35,7 @@ public class BookRepositoryImpl implements BookRepository {
} }
@Override @Override
public void deleteBookById(int isbn) { public void deleteBookById(Long isbn) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try { try {
if (!entityManager.getTransaction().isActive()) { if (!entityManager.getTransaction().isActive()) {
@ -62,7 +67,7 @@ public class BookRepositoryImpl implements BookRepository {
} }
@Override @Override
public void updateBook(int isbn, String title, Book prequel) { public void updateBook(Long isbn, String title, Book prequel) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try { try {
if (!entityManager.getTransaction().isActive()) { if (!entityManager.getTransaction().isActive()) {
@ -115,7 +120,7 @@ public class BookRepositoryImpl implements BookRepository {
@Override @Override
public void addAuthor(int isbn, long id) { public void addAuthor(Long isbn, Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try { try {
if (!entityManager.getTransaction().isActive()) { if (!entityManager.getTransaction().isActive()) {
@ -134,7 +139,7 @@ public class BookRepositoryImpl implements BookRepository {
} }
@Override @Override
public void removeAuthor(int isbn, long id) { public void removeAuthor(Long isbn, Long id) {
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); final EntityManager entityManager = this.entityManagerFactory.createEntityManager();
try { try {
if (!entityManager.getTransaction().isActive()) { if (!entityManager.getTransaction().isActive()) {

Loading…
Cancel
Save