Skip to content

Commit

Permalink
Use Quarkus application framework - step 5: use Panache and @transact…
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenWoltmann committed Oct 27, 2023
1 parent 7e931b5 commit d33f435
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package eu.happycoders.shop.adapter.out.persistence.jpa;

import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import jakarta.enterprise.context.ApplicationScoped;

/**
* Panache repository for {@link CartJpaEntity}.
*
* @author Sven Woltmann
*/
@ApplicationScoped
public class JpaCartPanacheRepository implements PanacheRepositoryBase<CartJpaEntity, Integer> {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import eu.happycoders.shop.model.customer.CustomerId;
import io.quarkus.arc.lookup.LookupIfProperty;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.transaction.Transactional;
import java.util.Optional;

/**
Expand All @@ -18,41 +17,28 @@
@ApplicationScoped
public class JpaCartRepository implements CartRepository {

private final EntityManagerFactory entityManagerFactory;
private final JpaCartPanacheRepository panacheRepository;

public JpaCartRepository(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
public JpaCartRepository(JpaCartPanacheRepository panacheRepository) {
this.panacheRepository = panacheRepository;
}

@Override
@Transactional
public void save(Cart cart) {
try (EntityManager entityManager = entityManagerFactory.createEntityManager()) {
entityManager.getTransaction().begin();
entityManager.merge(CartMapper.toJpaEntity(cart));
entityManager.getTransaction().commit();
}
panacheRepository.getEntityManager().merge(CartMapper.toJpaEntity(cart));
}

@Override
@Transactional
public Optional<Cart> findByCustomerId(CustomerId customerId) {
try (EntityManager entityManager = entityManagerFactory.createEntityManager()) {
CartJpaEntity cartJpaEntity = entityManager.find(CartJpaEntity.class, customerId.value());
return CartMapper.toModelEntityOptional(cartJpaEntity);
}
CartJpaEntity cartJpaEntity = panacheRepository.findById(customerId.value());
return CartMapper.toModelEntityOptional(cartJpaEntity);
}

@Override
@Transactional
public void deleteByCustomerId(CustomerId customerId) {
try (EntityManager entityManager = entityManagerFactory.createEntityManager()) {
entityManager.getTransaction().begin();

CartJpaEntity cartJpaEntity = entityManager.find(CartJpaEntity.class, customerId.value());

if (cartJpaEntity != null) {
entityManager.remove(cartJpaEntity);
}

entityManager.getTransaction().commit();
}
panacheRepository.deleteById(customerId.value());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package eu.happycoders.shop.adapter.out.persistence.jpa;

import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import jakarta.enterprise.context.ApplicationScoped;

/**
* Panache repository for {@link ProductJpaEntity}.
*
* @author Sven Woltmann
*/
@ApplicationScoped
public class JpaProductPanacheRepository
implements PanacheRepositoryBase<ProductJpaEntity, String> {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import eu.happycoders.shop.model.product.Product;
import eu.happycoders.shop.model.product.ProductId;
import io.quarkus.arc.lookup.LookupIfProperty;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.TypedQuery;
import jakarta.transaction.Transactional;
import java.util.List;
import java.util.Optional;

Expand All @@ -21,47 +20,38 @@
@ApplicationScoped
public class JpaProductRepository implements ProductRepository {

private final EntityManagerFactory entityManagerFactory;
private final JpaProductPanacheRepository panacheRepository;

public JpaProductRepository(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
createDemoProducts();
public JpaProductRepository(JpaProductPanacheRepository panacheRepository) {
this.panacheRepository = panacheRepository;
}

private void createDemoProducts() {
@PostConstruct
void createDemoProducts() {
DemoProducts.DEMO_PRODUCTS.forEach(this::save);
}

@Override
@Transactional
public void save(Product product) {
try (EntityManager entityManager = entityManagerFactory.createEntityManager()) {
entityManager.getTransaction().begin();
entityManager.merge(ProductMapper.toJpaEntity(product));
entityManager.getTransaction().commit();
}
panacheRepository.getEntityManager().merge(ProductMapper.toJpaEntity(product));
}

@Override
@Transactional
public Optional<Product> findById(ProductId productId) {
try (EntityManager entityManager = entityManagerFactory.createEntityManager()) {
ProductJpaEntity jpaEntity = entityManager.find(ProductJpaEntity.class, productId.value());
return ProductMapper.toModelEntityOptional(jpaEntity);
}
ProductJpaEntity jpaEntity = panacheRepository.findById(productId.value());
return ProductMapper.toModelEntityOptional(jpaEntity);
}

@Override
@Transactional
public List<Product> findByNameOrDescription(String queryString) {
try (EntityManager entityManager = entityManagerFactory.createEntityManager()) {
TypedQuery<ProductJpaEntity> query =
entityManager
.createQuery(
"from ProductJpaEntity where name like :query or description like :query",
ProductJpaEntity.class)
.setParameter("query", "%" + queryString + "%");
List<ProductJpaEntity> entities =
panacheRepository
.find("name like ?1 or description like ?1", "%" + queryString + "%")
.list();

List<ProductJpaEntity> entities = query.getResultList();

return ProductMapper.toModelEntities(entities);
}
return ProductMapper.toModelEntities(entities);
}
}

0 comments on commit d33f435

Please sign in to comment.