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