Skip to content

Commit

Permalink
Merge pull request #41 from axonivy-market/bugfix/APS-156
Browse files Browse the repository at this point in the history
APS-156 added test but found no problem
  • Loading branch information
peters-axon authored Aug 18, 2023
2 parents 277e993 + 809f1dd commit d8171bb
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 4 deletions.
3 changes: 2 additions & 1 deletion persistence-utils-demo-test/config/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
<persistence-unit name="persistence_utils">
<non-jta-data-source>persistence-utils-test</non-jta-data-source>
<class>com.axonivy.utils.persistence.entities.Car</class>
<class>com.axonivy.utils.persistence.entities.Invoice</class>
<class>com.axonivy.utils.persistence.entities.InvoicePosition</class>
<class>com.axonivy.utils.persistence.entities.Producer</class>
<class>com.axonivy.utils.persistence.entities.Product</class>
<class>com.axonivy.utils.persistence.history.beans.History</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.enable_lazy_load_no_trans" value="true"/>
</properties>
</persistence-unit>
</persistence>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.axonivy.utils.persistence.daos;

import javax.persistence.criteria.Expression;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.MessageFormatMessageFactory;

import com.axonivy.utils.persistence.dao.CriteriaQueryContext;
import com.axonivy.utils.persistence.dao.GenericIdEntityDAO;
import com.axonivy.utils.persistence.entities.Invoice;
import com.axonivy.utils.persistence.entities.Invoice_;

public class InvoiceDAO extends GenericIdEntityDAO<Invoice_, Invoice> implements BaseDAO{
private static final Logger LOG = LogManager.getLogger(new MessageFormatMessageFactory());
private static final InvoiceDAO INSTANCE = new InvoiceDAO();

public static InvoiceDAO get() {
return INSTANCE;
}

@Override
protected Class<Invoice> getType() {
return Invoice.class;
}

public int countPositions(String invoiceId) {
int result = 0;

Invoice invoice = null;
try (CriteriaQueryContext<Invoice> ctx = initializeQuery()) {
Expression<String> idExpr = getExpression(null, ctx.r, Invoice_.id);
ctx.whereEq(idExpr, invoiceId);
invoice = forceSingleResult(findByCriteria(ctx));
// Do we have a session here?
LOG.info("Found invoice position: {0}", invoice.getPositions().get(0).getDescription());
result = invoice.getPositions().size();
}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.axonivy.utils.persistence.entities;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;

import com.axonivy.utils.persistence.beans.GenericIdEntity;

@Entity
public class Invoice extends GenericIdEntity {
private static final long serialVersionUID = 1L;

@Column
private String name;

@OneToMany(mappedBy = "invoice", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<InvoicePosition> positions;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<InvoicePosition> getPositions() {
return positions;
}

public void setPositions(List<InvoicePosition> positions) {
this.positions = positions;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.axonivy.utils.persistence.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;

import com.axonivy.utils.persistence.beans.GenericIdEntity;

@Entity
public class InvoicePosition extends GenericIdEntity {
private static final long serialVersionUID = 1L;

@Column
private String description;

@ManyToOne
private Invoice invoice;

public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Invoice getInvoice() {
return invoice;
}
public void setInvoice(Invoice invoice) {
this.invoice = invoice;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.axonivy.utils.persistence.test.dao;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.ArrayList;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.axonivy.utils.persistence.daos.InvoiceDAO;
import com.axonivy.utils.persistence.entities.Invoice;
import com.axonivy.utils.persistence.entities.InvoicePosition;

import ch.ivyteam.ivy.environment.IvyTest;

@IvyTest
public class Aps156Test {

private Invoice invoice;

@BeforeEach
public void prepare() {
invoice = new Invoice();
invoice.setName("Invoice 1");

ArrayList<InvoicePosition> positions = new ArrayList<>();

InvoicePosition ip1 = new InvoicePosition();
ip1.setDescription("Invoice Position 1");
ip1.setInvoice(invoice);
positions.add(ip1);

invoice.setPositions(positions);

invoice = InvoiceDAO.get().save(invoice);
}

@Test
public void test1() {
assertThat(invoice).as("Prepared test").isNotNull();

int count = InvoiceDAO.get().countPositions(invoice.getId());

assertThat(count).as("Found one").isEqualTo(1);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -810,11 +810,10 @@ public CriteriaQueryGenericContext<T, Tuple> initializeTupleQuery() {
* @param <U> the type of the represented object
* @return criteria query for combination of T and U
*/
public <U> CriteriaQueryGenericContext<T, U> initializeQuery(
Class<T> rootType, Class<U> queryType) {
public <U> CriteriaQueryGenericContext<T, U> initializeQuery(Class<T> rootType, Class<U> queryType) {

CriteriaBuilder cb = getEM().getCriteriaBuilder();
beginSession();// try(AutoCloseTransaction autoSession = beginSession()
beginSession();

CriteriaQuery<U> query = cb.createQuery(queryType);
Root<T> root = query.from(rootType);
Expand Down

0 comments on commit d8171bb

Please sign in to comment.