|
|
|
@ -1,4 +1,5 @@ |
|
|
|
|
package mivan.repository; |
|
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
|
@ -9,30 +10,31 @@ import javax.persistence.Persistence; |
|
|
|
|
import mivan.model.Item; |
|
|
|
|
|
|
|
|
|
public class ItemRepositoryImpl implements ItemRepository { |
|
|
|
|
private EntityManagerFactory entityManagerFactory; |
|
|
|
|
private EntityManagerFactory entityManagerFactory; |
|
|
|
|
|
|
|
|
|
public ItemRepositoryImpl() { |
|
|
|
|
this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 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 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(); |
|
|
|
|
@Override |
|
|
|
|
public void deleteItemById(Long id) { |
|
|
|
|
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
|
|
|
|
try { |
|
|
|
|
if (!entityManager.getTransaction().isActive()) { |
|
|
|
|
entityManager.getTransaction().begin(); |
|
|
|
@ -44,11 +46,11 @@ public class ItemRepositoryImpl implements ItemRepository { |
|
|
|
|
} catch (Exception ex) { |
|
|
|
|
entityManager.getTransaction().rollback(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void addItem(Item item) { |
|
|
|
|
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
|
|
|
|
@Override |
|
|
|
|
public void addItem(Item item) { |
|
|
|
|
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
|
|
|
|
try { |
|
|
|
|
if (!entityManager.getTransaction().isActive()) { |
|
|
|
|
entityManager.getTransaction().begin(); |
|
|
|
@ -61,11 +63,11 @@ public class ItemRepositoryImpl implements ItemRepository { |
|
|
|
|
entityManager.getTransaction().rollback(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public int getSize() { |
|
|
|
|
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
|
|
|
|
@Override |
|
|
|
|
public int getSize() { |
|
|
|
|
final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); |
|
|
|
|
int size = 0; |
|
|
|
|
try { |
|
|
|
|
size = entityManager.createQuery("FROM Item").getResultList().size(); |
|
|
|
@ -75,7 +77,6 @@ public class ItemRepositoryImpl implements ItemRepository { |
|
|
|
|
size = 0; |
|
|
|
|
} |
|
|
|
|
return size; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |