commit
7a24c956b8
@ -0,0 +1,61 @@ |
||||
package mivan.model; |
||||
|
||||
import java.io.Serializable; |
||||
|
||||
import javax.persistence.Column; |
||||
import javax.persistence.Entity; |
||||
import javax.persistence.FetchType; |
||||
import javax.persistence.GeneratedValue; |
||||
import javax.persistence.GenerationType; |
||||
import javax.persistence.Id; |
||||
import javax.persistence.JoinColumn; |
||||
import javax.persistence.ManyToOne; |
||||
import javax.persistence.Table; |
||||
|
||||
@Entity |
||||
@Table(name="item") |
||||
public class Item implements Serializable{ |
||||
private static final long serialVersionUID = 1993L; |
||||
|
||||
@Id |
||||
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
@Column(name = "id", unique = true) |
||||
private long id; |
||||
|
||||
@ManyToOne(fetch=FetchType.LAZY) |
||||
@JoinColumn(name="isbn") |
||||
private Book book; |
||||
|
||||
@ManyToOne(fetch=FetchType.LAZY) |
||||
@JoinColumn(name="location") |
||||
private Location location; |
||||
|
||||
public Item () {} |
||||
public Item(Book book, Location location){ |
||||
super(); |
||||
this.book = book; |
||||
this.location = location; |
||||
} |
||||
|
||||
public long getId() { |
||||
return this.id; |
||||
} |
||||
|
||||
public Book getBook(){ |
||||
return this.book; |
||||
} |
||||
|
||||
public void setBook(Book book){ |
||||
this.book = book; |
||||
} |
||||
|
||||
public Location getLocation() { |
||||
return this.location; |
||||
} |
||||
|
||||
public void setLocation(Location location) { |
||||
this.location = location; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,13 @@ |
||||
package mivan.repository; |
||||
|
||||
import mivan.model.Item; |
||||
|
||||
public interface ItemRepository extends Repository<Item, Long> { |
||||
|
||||
public void deleteItemById(Long id); |
||||
|
||||
public void addItem(Item item); |
||||
|
||||
public int getSize(); |
||||
|
||||
} |
@ -0,0 +1,81 @@ |
||||
package mivan.repository; |
||||
import java.util.List; |
||||
import java.util.Optional; |
||||
|
||||
import javax.persistence.EntityManager; |
||||
import javax.persistence.EntityManagerFactory; |
||||
import javax.persistence.Persistence; |
||||
|
||||
import mivan.model.Item; |
||||
|
||||
public class ItemRepositoryImpl implements ItemRepository { |
||||
private EntityManagerFactory entityManagerFactory; |
||||
|
||||
public ItemRepositoryImpl() { |
||||
this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); |
||||
} |
||||
@Override |
||||
public Optional<Item> findById(Long id) { |
||||
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
||||
Item item = entityManager.find(Item.class, id); |
||||
entityManager.close(); |
||||
return Optional.ofNullable(item); |
||||
} |
||||
|
||||
@Override |
||||
public Iterable<Item> findAll() { |
||||
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
||||
List<Item> items = entityManager.createQuery("FROM Item", Item.class).getResultList(); |
||||
entityManager.close(); |
||||
return items; |
||||
} |
||||
|
||||
@Override |
||||
public void deleteItemById(Long id) { |
||||
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
||||
try { |
||||
if (!entityManager.getTransaction().isActive()) { |
||||
entityManager.getTransaction().begin(); |
||||
} |
||||
Item item = entityManager.find(Item.class, id); |
||||
entityManager.remove(item); |
||||
entityManager.getTransaction().commit(); |
||||
entityManager.close(); |
||||
} catch (Exception ex) { |
||||
entityManager.getTransaction().rollback(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void addItem(Item item) { |
||||
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
||||
try { |
||||
if (!entityManager.getTransaction().isActive()) { |
||||
entityManager.getTransaction().begin(); |
||||
} |
||||
entityManager.persist(item); |
||||
entityManager.getTransaction().commit(); |
||||
entityManager.close(); |
||||
|
||||
} catch (Exception ex) { |
||||
entityManager.getTransaction().rollback(); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public int getSize() { |
||||
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
||||
int size = 0; |
||||
try { |
||||
size = entityManager.createQuery("FROM Item").getResultList().size(); |
||||
|
||||
entityManager.close(); |
||||
} catch (Exception ex) { |
||||
size = 0; |
||||
} |
||||
return size; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,58 @@ |
||||
package mivan; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
|
||||
import java.util.Optional; |
||||
|
||||
import org.junit.jupiter.api.Order; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.TestMethodOrder; |
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; |
||||
|
||||
import mivan.model.Book; |
||||
import mivan.model.Item; |
||||
import mivan.model.Location; |
||||
import mivan.repository.BookRepository; |
||||
import mivan.repository.ItemRepository; |
||||
import mivan.repository.ItemRepositoryImpl; |
||||
|
||||
@TestMethodOrder(OrderAnnotation.class) |
||||
public class ItemTest { |
||||
private ItemRepository itemRepository = new ItemRepositoryImpl(); |
||||
|
||||
|
||||
@Test |
||||
@Order(1) |
||||
void testAddItem() { |
||||
Book book = null; |
||||
Location location = null; |
||||
Item item = new Item(book, location); |
||||
itemRepository.addItem(item); |
||||
int size = itemRepository.getSize(); |
||||
assertEquals(1, size); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
@Order(2) |
||||
void testRemoveItem() { |
||||
int startSize = itemRepository.getSize(); |
||||
Book book = null; |
||||
Location location = null; |
||||
Item item = new Item(book, location); |
||||
itemRepository.addItem(item); |
||||
int size = itemRepository.getSize(); |
||||
assertEquals(startSize + 1, size); |
||||
|
||||
Optional<Item> optItem2 = itemRepository.findById(item.getId()); |
||||
|
||||
Item item2 = optItem2.get(); |
||||
long ID = item2.getId(); |
||||
itemRepository.deleteItemById(ID); |
||||
|
||||
int finalSize = itemRepository.getSize(); |
||||
assertEquals(startSize, finalSize); |
||||
|
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue