diff --git a/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeElementCollectionTest.java b/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeElementCollectionTest.java new file mode 100644 index 000000000..e2f1eb383 --- /dev/null +++ b/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeElementCollectionTest.java @@ -0,0 +1,129 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import org.hibernate.annotations.Columns; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import javax.persistence.*; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class MySQLMonetaryAmountTypeElementCollectionTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testPersistAndReadMoney() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getComponents().get(0).getValue(), Money.of(new BigDecimal("10.23"), "USD")); + }); + } + + @Test + public void testSearchByMoneyInElementCollection() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("20.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("30.23"), "EUR"))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s join s.components sc where sc.value = :salary", Salary.class) + .setParameter("salary", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @ElementCollection + private List components = new ArrayList<>(); + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public List getComponents() { + return components; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + @TypeDef(name = "monetary-amount-currency", typeClass = MonetaryAmountType.class, defaultForType = MonetaryAmount.class) + public static class SalaryComponent { + @Columns(columns = { + @Column(name = "salary_amount"), + @Column(name = "salary_currency") + }) + @Type(type = "monetary-amount-currency") + private MonetaryAmount value; + + public SalaryComponent(MonetaryAmount value) { + this.value = value; + } + + public SalaryComponent() { + + } + + public MonetaryAmount getValue() { + return value; + } + + public void setValue(MonetaryAmount value) { + this.value = value; + } + } +} diff --git a/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeEmbeddedTest.java b/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeEmbeddedTest.java new file mode 100644 index 000000000..f7f02d650 --- /dev/null +++ b/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeEmbeddedTest.java @@ -0,0 +1,176 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import org.hibernate.annotations.Columns; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import javax.persistence.*; +import java.math.BigDecimal; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class MySQLMonetaryAmountTypeEmbeddedTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testReadAndWrite() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getEmbeddedSalary().amount, Money.of(new BigDecimal("10.23"), "USD")); + }); + + Salary salary = doInJPA(entityManager -> { + return entityManager.find(Salary.class, _salary.getId()); + }); + + salary.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.49"), "USD"))); + + doInJPA(entityManager -> { + entityManager.merge(salary); + }); + } + + @Test + public void testSearchByMoney() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s where s" + + ".embeddedSalary.amount = :amount", Salary.class) + .setParameter("amount", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Test + public void testSearchByComponents() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("10.23"), "USD")))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + BigDecimal amount = BigDecimal.TEN; + List salaries = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.amount >= :amount", Salary.class) + .setParameter("amount", amount) + .getResultList(); + + + assertEquals(1L, salaries.get(0).getId()); + assertEquals(2L, salaries.get(1).getId()); + }); + + doInJPA(entityManager -> { + String currency = "USD"; + Salary salary = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.currency = :currency", Salary.class) + .setParameter("currency", currency) + .getSingleResult(); + + assertEquals(1L, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @Embedded + private EmbeddableMonetaryAmount embeddedSalary; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public EmbeddableMonetaryAmount getEmbeddedSalary() { + return embeddedSalary; + } + + public void setEmbeddedSalary(EmbeddableMonetaryAmount embeddedSalary) { + this.embeddedSalary = embeddedSalary; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + @TypeDef(name = "monetary-amount-currency", typeClass = MonetaryAmountType.class, defaultForType = MonetaryAmount.class) + public static class EmbeddableMonetaryAmount { + @Columns(columns = { + @Column(name = "salary_amount"), + @Column(name = "salary_currency") + }) + @Type(type = "monetary-amount-currency") + private MonetaryAmount amount; + + public EmbeddableMonetaryAmount(MonetaryAmount amount) { + this.amount = amount; + } + + public EmbeddableMonetaryAmount() { + + } + + + public MonetaryAmount getAmount() { + return amount; + } + + public void setAmount(MonetaryAmount amount) { + this.amount = amount; + } + } +} diff --git a/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeElementCollectionTest.java b/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeElementCollectionTest.java new file mode 100644 index 000000000..6fc6dd7b6 --- /dev/null +++ b/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeElementCollectionTest.java @@ -0,0 +1,129 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import org.hibernate.annotations.Columns; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import javax.persistence.*; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class PostgreSQLMonetaryAmountTypeElementCollectionTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testPersistAndReadMoney() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getComponents().get(0).getValue(), Money.of(new BigDecimal("10.23"), "USD")); + }); + } + + @Test + public void testSearchByMoneyInElementCollection() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("20.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("30.23"), "EUR"))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s join s.components sc where sc.value = :salary", Salary.class) + .setParameter("salary", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @ElementCollection + private List components = new ArrayList<>(); + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public List getComponents() { + return components; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + @TypeDef(name = "monetary-amount-currency", typeClass = MonetaryAmountType.class, defaultForType = MonetaryAmount.class) + public static class SalaryComponent { + @Columns(columns = { + @Column(name = "salary_amount"), + @Column(name = "salary_currency") + }) + @Type(type = "monetary-amount-currency") + private MonetaryAmount value; + + public SalaryComponent(MonetaryAmount value) { + this.value = value; + } + + public SalaryComponent() { + + } + + public MonetaryAmount getValue() { + return value; + } + + public void setValue(MonetaryAmount value) { + this.value = value; + } + } +} diff --git a/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeEmbeddedTest.java b/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeEmbeddedTest.java new file mode 100644 index 000000000..d91eaddc1 --- /dev/null +++ b/hypersistence-utils-hibernate-52/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeEmbeddedTest.java @@ -0,0 +1,166 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import org.hibernate.annotations.Columns; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import javax.persistence.*; +import java.math.BigDecimal; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class PostgreSQLMonetaryAmountTypeEmbeddedTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testPersistAndReadMoney() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getEmbeddedSalary().amount, Money.of(new BigDecimal("10.23"), "USD")); + }); + } + + @Test + public void testSearchByMoney() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s where s" + + ".embeddedSalary.amount = :amount", Salary.class) + .setParameter("amount", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Test + public void testSearchByComponents() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("10.23"), "USD")))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + BigDecimal amount = BigDecimal.TEN; + List salaries = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.amount >= :amount", Salary.class) + .setParameter("amount", amount) + .getResultList(); + + + assertEquals(1L, salaries.get(0).getId()); + assertEquals(2L, salaries.get(1).getId()); + }); + + doInJPA(entityManager -> { + String currency = "USD"; + Salary salary = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.currency = :currency", Salary.class) + .setParameter("currency", currency) + .getSingleResult(); + + assertEquals(1L, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @Embedded + private EmbeddableMonetaryAmount embeddedSalary; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public EmbeddableMonetaryAmount getEmbeddedSalary() { + return embeddedSalary; + } + + public void setEmbeddedSalary(EmbeddableMonetaryAmount embeddedSalary) { + this.embeddedSalary = embeddedSalary; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + @TypeDef(name = "monetary-amount-currency", typeClass = MonetaryAmountType.class, defaultForType = MonetaryAmount.class) + public static class EmbeddableMonetaryAmount { + @Columns(columns = { + @Column(name = "salary_amount"), + @Column(name = "salary_currency") + }) + @Type(type = "monetary-amount-currency") + private MonetaryAmount amount; + + public EmbeddableMonetaryAmount(MonetaryAmount amount) { + this.amount = amount; + } + + public EmbeddableMonetaryAmount() { + + } + + + public MonetaryAmount getAmount() { + return amount; + } + + public void setAmount(MonetaryAmount amount) { + this.amount = amount; + } + } +} diff --git a/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeElementCollectionTest.java b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeElementCollectionTest.java new file mode 100644 index 000000000..e2f1eb383 --- /dev/null +++ b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeElementCollectionTest.java @@ -0,0 +1,129 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import org.hibernate.annotations.Columns; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import javax.persistence.*; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class MySQLMonetaryAmountTypeElementCollectionTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testPersistAndReadMoney() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getComponents().get(0).getValue(), Money.of(new BigDecimal("10.23"), "USD")); + }); + } + + @Test + public void testSearchByMoneyInElementCollection() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("20.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("30.23"), "EUR"))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s join s.components sc where sc.value = :salary", Salary.class) + .setParameter("salary", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @ElementCollection + private List components = new ArrayList<>(); + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public List getComponents() { + return components; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + @TypeDef(name = "monetary-amount-currency", typeClass = MonetaryAmountType.class, defaultForType = MonetaryAmount.class) + public static class SalaryComponent { + @Columns(columns = { + @Column(name = "salary_amount"), + @Column(name = "salary_currency") + }) + @Type(type = "monetary-amount-currency") + private MonetaryAmount value; + + public SalaryComponent(MonetaryAmount value) { + this.value = value; + } + + public SalaryComponent() { + + } + + public MonetaryAmount getValue() { + return value; + } + + public void setValue(MonetaryAmount value) { + this.value = value; + } + } +} diff --git a/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeEmbeddedTest.java b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeEmbeddedTest.java new file mode 100644 index 000000000..f7f02d650 --- /dev/null +++ b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeEmbeddedTest.java @@ -0,0 +1,176 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import org.hibernate.annotations.Columns; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import javax.persistence.*; +import java.math.BigDecimal; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class MySQLMonetaryAmountTypeEmbeddedTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testReadAndWrite() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getEmbeddedSalary().amount, Money.of(new BigDecimal("10.23"), "USD")); + }); + + Salary salary = doInJPA(entityManager -> { + return entityManager.find(Salary.class, _salary.getId()); + }); + + salary.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.49"), "USD"))); + + doInJPA(entityManager -> { + entityManager.merge(salary); + }); + } + + @Test + public void testSearchByMoney() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s where s" + + ".embeddedSalary.amount = :amount", Salary.class) + .setParameter("amount", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Test + public void testSearchByComponents() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("10.23"), "USD")))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + BigDecimal amount = BigDecimal.TEN; + List salaries = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.amount >= :amount", Salary.class) + .setParameter("amount", amount) + .getResultList(); + + + assertEquals(1L, salaries.get(0).getId()); + assertEquals(2L, salaries.get(1).getId()); + }); + + doInJPA(entityManager -> { + String currency = "USD"; + Salary salary = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.currency = :currency", Salary.class) + .setParameter("currency", currency) + .getSingleResult(); + + assertEquals(1L, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @Embedded + private EmbeddableMonetaryAmount embeddedSalary; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public EmbeddableMonetaryAmount getEmbeddedSalary() { + return embeddedSalary; + } + + public void setEmbeddedSalary(EmbeddableMonetaryAmount embeddedSalary) { + this.embeddedSalary = embeddedSalary; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + @TypeDef(name = "monetary-amount-currency", typeClass = MonetaryAmountType.class, defaultForType = MonetaryAmount.class) + public static class EmbeddableMonetaryAmount { + @Columns(columns = { + @Column(name = "salary_amount"), + @Column(name = "salary_currency") + }) + @Type(type = "monetary-amount-currency") + private MonetaryAmount amount; + + public EmbeddableMonetaryAmount(MonetaryAmount amount) { + this.amount = amount; + } + + public EmbeddableMonetaryAmount() { + + } + + + public MonetaryAmount getAmount() { + return amount; + } + + public void setAmount(MonetaryAmount amount) { + this.amount = amount; + } + } +} diff --git a/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeElementCollectionTest.java b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeElementCollectionTest.java new file mode 100644 index 000000000..6fc6dd7b6 --- /dev/null +++ b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeElementCollectionTest.java @@ -0,0 +1,129 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import org.hibernate.annotations.Columns; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import javax.persistence.*; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class PostgreSQLMonetaryAmountTypeElementCollectionTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testPersistAndReadMoney() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getComponents().get(0).getValue(), Money.of(new BigDecimal("10.23"), "USD")); + }); + } + + @Test + public void testSearchByMoneyInElementCollection() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("20.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("30.23"), "EUR"))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s join s.components sc where sc.value = :salary", Salary.class) + .setParameter("salary", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @ElementCollection + private List components = new ArrayList<>(); + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public List getComponents() { + return components; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + @TypeDef(name = "monetary-amount-currency", typeClass = MonetaryAmountType.class, defaultForType = MonetaryAmount.class) + public static class SalaryComponent { + @Columns(columns = { + @Column(name = "salary_amount"), + @Column(name = "salary_currency") + }) + @Type(type = "monetary-amount-currency") + private MonetaryAmount value; + + public SalaryComponent(MonetaryAmount value) { + this.value = value; + } + + public SalaryComponent() { + + } + + public MonetaryAmount getValue() { + return value; + } + + public void setValue(MonetaryAmount value) { + this.value = value; + } + } +} diff --git a/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeEmbeddedTest.java b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeEmbeddedTest.java new file mode 100644 index 000000000..d91eaddc1 --- /dev/null +++ b/hypersistence-utils-hibernate-55/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeEmbeddedTest.java @@ -0,0 +1,166 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import org.hibernate.annotations.Columns; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import javax.persistence.*; +import java.math.BigDecimal; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class PostgreSQLMonetaryAmountTypeEmbeddedTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testPersistAndReadMoney() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getEmbeddedSalary().amount, Money.of(new BigDecimal("10.23"), "USD")); + }); + } + + @Test + public void testSearchByMoney() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s where s" + + ".embeddedSalary.amount = :amount", Salary.class) + .setParameter("amount", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Test + public void testSearchByComponents() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("10.23"), "USD")))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + BigDecimal amount = BigDecimal.TEN; + List salaries = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.amount >= :amount", Salary.class) + .setParameter("amount", amount) + .getResultList(); + + + assertEquals(1L, salaries.get(0).getId()); + assertEquals(2L, salaries.get(1).getId()); + }); + + doInJPA(entityManager -> { + String currency = "USD"; + Salary salary = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.currency = :currency", Salary.class) + .setParameter("currency", currency) + .getSingleResult(); + + assertEquals(1L, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @Embedded + private EmbeddableMonetaryAmount embeddedSalary; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public EmbeddableMonetaryAmount getEmbeddedSalary() { + return embeddedSalary; + } + + public void setEmbeddedSalary(EmbeddableMonetaryAmount embeddedSalary) { + this.embeddedSalary = embeddedSalary; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + @TypeDef(name = "monetary-amount-currency", typeClass = MonetaryAmountType.class, defaultForType = MonetaryAmount.class) + public static class EmbeddableMonetaryAmount { + @Columns(columns = { + @Column(name = "salary_amount"), + @Column(name = "salary_currency") + }) + @Type(type = "monetary-amount-currency") + private MonetaryAmount amount; + + public EmbeddableMonetaryAmount(MonetaryAmount amount) { + this.amount = amount; + } + + public EmbeddableMonetaryAmount() { + + } + + + public MonetaryAmount getAmount() { + return amount; + } + + public void setAmount(MonetaryAmount amount) { + this.amount = amount; + } + } +} diff --git a/hypersistence-utils-hibernate-60/src/main/java/io/hypersistence/utils/hibernate/type/money/MonetaryAmountType.java b/hypersistence-utils-hibernate-60/src/main/java/io/hypersistence/utils/hibernate/type/money/MonetaryAmountType.java index 164f41bca..aa3bdcc6e 100644 --- a/hypersistence-utils-hibernate-60/src/main/java/io/hypersistence/utils/hibernate/type/money/MonetaryAmountType.java +++ b/hypersistence-utils-hibernate-60/src/main/java/io/hypersistence/utils/hibernate/type/money/MonetaryAmountType.java @@ -26,6 +26,22 @@ public class MonetaryAmountType implements CompositeUserType public static class MonetaryAmountMapper { BigDecimal amount; String currency; + + public BigDecimal getAmount() { + return amount; + } + + public void setAmount(BigDecimal amount) { + this.amount = amount; + } + + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } } public MonetaryAmountType() { diff --git a/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeElementCollectionTest.java b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeElementCollectionTest.java new file mode 100644 index 000000000..d4e57f4ee --- /dev/null +++ b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeElementCollectionTest.java @@ -0,0 +1,124 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import jakarta.persistence.*; +import org.hibernate.annotations.CompositeType; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class MySQLMonetaryAmountTypeElementCollectionTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testPersistAndReadMoney() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getComponents().get(0).getValue(), Money.of(new BigDecimal("10.23"), "USD")); + }); + } + + @Test + public void testSearchByMoneyInElementCollection() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("20.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("30.23"), "EUR"))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s join s.components sc where sc.value = :salary", Salary.class) + .setParameter("salary", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @ElementCollection + private List components = new ArrayList<>(); + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public List getComponents() { + return components; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + public static class SalaryComponent { + @AttributeOverride(name = "amount", column = @Column(name = "salary_amount")) + @AttributeOverride(name = "currency", column = @Column(name = "salary_currency")) + @CompositeType(MonetaryAmountType.class) + private MonetaryAmount value; + + public SalaryComponent(MonetaryAmount value) { + this.value = value; + } + + public SalaryComponent() { + + } + + public MonetaryAmount getValue() { + return value; + } + + public void setValue(MonetaryAmount value) { + this.value = value; + } + } +} diff --git a/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeEmbeddedTest.java b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeEmbeddedTest.java new file mode 100644 index 000000000..1b3c9a832 --- /dev/null +++ b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeEmbeddedTest.java @@ -0,0 +1,171 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import jakarta.persistence.*; +import org.hibernate.annotations.CompositeType; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import java.math.BigDecimal; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class MySQLMonetaryAmountTypeEmbeddedTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testReadAndWrite() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getEmbeddedSalary().amount, Money.of(new BigDecimal("10.23"), "USD")); + }); + + Salary salary = doInJPA(entityManager -> { + return entityManager.find(Salary.class, _salary.getId()); + }); + + salary.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.49"), "USD"))); + + doInJPA(entityManager -> { + entityManager.merge(salary); + }); + } + + @Test + public void testSearchByMoney() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s where s" + + ".embeddedSalary.amount = :amount", Salary.class) + .setParameter("amount", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Test + public void testSearchByComponents() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("10.23"), "USD")))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + BigDecimal amount = BigDecimal.TEN; + List salaries = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.amount >= :amount", Salary.class) + .setParameter("amount", amount) + .getResultList(); + + + assertEquals(1L, salaries.get(0).getId()); + assertEquals(2L, salaries.get(1).getId()); + }); + + doInJPA(entityManager -> { + String currency = "USD"; + Salary salary = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.currency = :currency", Salary.class) + .setParameter("currency", currency) + .getSingleResult(); + + assertEquals(1L, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @Embedded + private EmbeddableMonetaryAmount embeddedSalary; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public EmbeddableMonetaryAmount getEmbeddedSalary() { + return embeddedSalary; + } + + public void setEmbeddedSalary(EmbeddableMonetaryAmount embeddedSalary) { + this.embeddedSalary = embeddedSalary; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + public static class EmbeddableMonetaryAmount { + @AttributeOverride(name = "amount", column = @Column(name = "salary_amount")) + @AttributeOverride(name = "currency", column = @Column(name = "salary_currency")) + @CompositeType(MonetaryAmountType.class) + private MonetaryAmount amount; + + public EmbeddableMonetaryAmount(MonetaryAmount amount) { + this.amount = amount; + } + + public EmbeddableMonetaryAmount() { + + } + + + public MonetaryAmount getAmount() { + return amount; + } + + public void setAmount(MonetaryAmount amount) { + this.amount = amount; + } + } +} diff --git a/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeElementCollectionTest.java b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeElementCollectionTest.java new file mode 100644 index 000000000..194fc8666 --- /dev/null +++ b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeElementCollectionTest.java @@ -0,0 +1,124 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import jakarta.persistence.*; +import org.hibernate.annotations.CompositeType; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class PostgreSQLMonetaryAmountTypeElementCollectionTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testPersistAndReadMoney() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getComponents().get(0).getValue(), Money.of(new BigDecimal("10.23"), "USD")); + }); + } + + @Test + public void testSearchByMoneyInElementCollection() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("20.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("30.23"), "EUR"))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s join s.components sc where sc.value = :salary", Salary.class) + .setParameter("salary", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @ElementCollection + private List components = new ArrayList<>(); + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public List getComponents() { + return components; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + public static class SalaryComponent { + @AttributeOverride(name = "amount", column = @Column(name = "salary_amount")) + @AttributeOverride(name = "currency", column = @Column(name = "salary_currency")) + @CompositeType(MonetaryAmountType.class) + private MonetaryAmount value; + + public SalaryComponent(MonetaryAmount value) { + this.value = value; + } + + public SalaryComponent() { + + } + + public MonetaryAmount getValue() { + return value; + } + + public void setValue(MonetaryAmount value) { + this.value = value; + } + } +} diff --git a/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeEmbeddedTest.java b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeEmbeddedTest.java new file mode 100644 index 000000000..48e8fdf54 --- /dev/null +++ b/hypersistence-utils-hibernate-60/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeEmbeddedTest.java @@ -0,0 +1,161 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import jakarta.persistence.*; +import org.hibernate.annotations.CompositeType; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import java.math.BigDecimal; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class PostgreSQLMonetaryAmountTypeEmbeddedTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testPersistAndReadMoney() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getEmbeddedSalary().amount, Money.of(new BigDecimal("10.23"), "USD")); + }); + } + + @Test + public void testSearchByMoney() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s where s" + + ".embeddedSalary.amount = :amount", Salary.class) + .setParameter("amount", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Test + public void testSearchByComponents() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("10.23"), "USD")))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + BigDecimal amount = BigDecimal.TEN; + List salaries = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.amount >= :amount", Salary.class) + .setParameter("amount", amount) + .getResultList(); + + + assertEquals(1L, salaries.get(0).getId()); + assertEquals(2L, salaries.get(1).getId()); + }); + + doInJPA(entityManager -> { + String currency = "USD"; + Salary salary = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.currency = :currency", Salary.class) + .setParameter("currency", currency) + .getSingleResult(); + + assertEquals(1L, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @Embedded + private EmbeddableMonetaryAmount embeddedSalary; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public EmbeddableMonetaryAmount getEmbeddedSalary() { + return embeddedSalary; + } + + public void setEmbeddedSalary(EmbeddableMonetaryAmount embeddedSalary) { + this.embeddedSalary = embeddedSalary; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + public static class EmbeddableMonetaryAmount { + @AttributeOverride(name = "amount", column = @Column(name = "salary_amount")) + @AttributeOverride(name = "currency", column = @Column(name = "salary_currency")) + @CompositeType(MonetaryAmountType.class) + private MonetaryAmount amount; + + public EmbeddableMonetaryAmount(MonetaryAmount amount) { + this.amount = amount; + } + + public EmbeddableMonetaryAmount() { + + } + + + public MonetaryAmount getAmount() { + return amount; + } + + public void setAmount(MonetaryAmount amount) { + this.amount = amount; + } + } +} diff --git a/hypersistence-utils-hibernate-62/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeElementCollectionTest.java b/hypersistence-utils-hibernate-62/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeElementCollectionTest.java new file mode 100644 index 000000000..d4e57f4ee --- /dev/null +++ b/hypersistence-utils-hibernate-62/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeElementCollectionTest.java @@ -0,0 +1,124 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import jakarta.persistence.*; +import org.hibernate.annotations.CompositeType; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class MySQLMonetaryAmountTypeElementCollectionTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testPersistAndReadMoney() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getComponents().get(0).getValue(), Money.of(new BigDecimal("10.23"), "USD")); + }); + } + + @Test + public void testSearchByMoneyInElementCollection() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("20.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("30.23"), "EUR"))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s join s.components sc where sc.value = :salary", Salary.class) + .setParameter("salary", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @ElementCollection + private List components = new ArrayList<>(); + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public List getComponents() { + return components; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + public static class SalaryComponent { + @AttributeOverride(name = "amount", column = @Column(name = "salary_amount")) + @AttributeOverride(name = "currency", column = @Column(name = "salary_currency")) + @CompositeType(MonetaryAmountType.class) + private MonetaryAmount value; + + public SalaryComponent(MonetaryAmount value) { + this.value = value; + } + + public SalaryComponent() { + + } + + public MonetaryAmount getValue() { + return value; + } + + public void setValue(MonetaryAmount value) { + this.value = value; + } + } +} diff --git a/hypersistence-utils-hibernate-62/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeEmbeddedTest.java b/hypersistence-utils-hibernate-62/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeEmbeddedTest.java new file mode 100644 index 000000000..1b3c9a832 --- /dev/null +++ b/hypersistence-utils-hibernate-62/src/test/java/io/hypersistence/utils/hibernate/type/money/MySQLMonetaryAmountTypeEmbeddedTest.java @@ -0,0 +1,171 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import jakarta.persistence.*; +import org.hibernate.annotations.CompositeType; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import java.math.BigDecimal; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class MySQLMonetaryAmountTypeEmbeddedTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testReadAndWrite() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getEmbeddedSalary().amount, Money.of(new BigDecimal("10.23"), "USD")); + }); + + Salary salary = doInJPA(entityManager -> { + return entityManager.find(Salary.class, _salary.getId()); + }); + + salary.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.49"), "USD"))); + + doInJPA(entityManager -> { + entityManager.merge(salary); + }); + } + + @Test + public void testSearchByMoney() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s where s" + + ".embeddedSalary.amount = :amount", Salary.class) + .setParameter("amount", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Test + public void testSearchByComponents() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("10.23"), "USD")))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + BigDecimal amount = BigDecimal.TEN; + List salaries = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.amount >= :amount", Salary.class) + .setParameter("amount", amount) + .getResultList(); + + + assertEquals(1L, salaries.get(0).getId()); + assertEquals(2L, salaries.get(1).getId()); + }); + + doInJPA(entityManager -> { + String currency = "USD"; + Salary salary = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.currency = :currency", Salary.class) + .setParameter("currency", currency) + .getSingleResult(); + + assertEquals(1L, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @Embedded + private EmbeddableMonetaryAmount embeddedSalary; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public EmbeddableMonetaryAmount getEmbeddedSalary() { + return embeddedSalary; + } + + public void setEmbeddedSalary(EmbeddableMonetaryAmount embeddedSalary) { + this.embeddedSalary = embeddedSalary; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + public static class EmbeddableMonetaryAmount { + @AttributeOverride(name = "amount", column = @Column(name = "salary_amount")) + @AttributeOverride(name = "currency", column = @Column(name = "salary_currency")) + @CompositeType(MonetaryAmountType.class) + private MonetaryAmount amount; + + public EmbeddableMonetaryAmount(MonetaryAmount amount) { + this.amount = amount; + } + + public EmbeddableMonetaryAmount() { + + } + + + public MonetaryAmount getAmount() { + return amount; + } + + public void setAmount(MonetaryAmount amount) { + this.amount = amount; + } + } +} diff --git a/hypersistence-utils-hibernate-62/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeElementCollectionTest.java b/hypersistence-utils-hibernate-62/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeElementCollectionTest.java new file mode 100644 index 000000000..194fc8666 --- /dev/null +++ b/hypersistence-utils-hibernate-62/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeElementCollectionTest.java @@ -0,0 +1,124 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import jakarta.persistence.*; +import org.hibernate.annotations.CompositeType; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class PostgreSQLMonetaryAmountTypeElementCollectionTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testPersistAndReadMoney() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getComponents().get(0).getValue(), Money.of(new BigDecimal("10.23"), "USD")); + }); + } + + @Test + public void testSearchByMoneyInElementCollection() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("10.23"), "USD"))); + salary1.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("20.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.getComponents().add(new SalaryComponent(Money.of(new BigDecimal("30.23"), "EUR"))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s join s.components sc where sc.value = :salary", Salary.class) + .setParameter("salary", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @ElementCollection + private List components = new ArrayList<>(); + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public List getComponents() { + return components; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + public static class SalaryComponent { + @AttributeOverride(name = "amount", column = @Column(name = "salary_amount")) + @AttributeOverride(name = "currency", column = @Column(name = "salary_currency")) + @CompositeType(MonetaryAmountType.class) + private MonetaryAmount value; + + public SalaryComponent(MonetaryAmount value) { + this.value = value; + } + + public SalaryComponent() { + + } + + public MonetaryAmount getValue() { + return value; + } + + public void setValue(MonetaryAmount value) { + this.value = value; + } + } +} diff --git a/hypersistence-utils-hibernate-62/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeEmbeddedTest.java b/hypersistence-utils-hibernate-62/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeEmbeddedTest.java new file mode 100644 index 000000000..48e8fdf54 --- /dev/null +++ b/hypersistence-utils-hibernate-62/src/test/java/io/hypersistence/utils/hibernate/type/money/PostgreSQLMonetaryAmountTypeEmbeddedTest.java @@ -0,0 +1,161 @@ +package io.hypersistence.utils.hibernate.type.money; + +import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest; +import jakarta.persistence.*; +import org.hibernate.annotations.CompositeType; +import org.javamoney.moneta.Money; +import org.junit.Test; + +import javax.money.MonetaryAmount; +import java.math.BigDecimal; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * @author Nikola Malenic + */ +public class PostgreSQLMonetaryAmountTypeEmbeddedTest extends AbstractPostgreSQLIntegrationTest { + @Override + protected Class[] entities() { + return new Class[]{ + Salary.class + }; + } + + @Test + public void testPersistAndReadMoney() { + Salary _salary = doInJPA(entityManager -> { + Salary salary = new Salary(); + salary.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + + entityManager.persist(salary); + + return salary; + }); + + doInJPA(entityManager -> { + Salary salary = entityManager.find(Salary.class, _salary.getId()); + + assertEquals(salary.getEmbeddedSalary().amount, Money.of(new BigDecimal("10.23"), "USD")); + }); + } + + @Test + public void testSearchByMoney() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount(Money.of(new BigDecimal("10.23"), "USD"))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + Money money = Money.of(new BigDecimal("10.23"), "USD"); + Salary salary = entityManager.createQuery("select s from Salary s where s" + + ".embeddedSalary.amount = :amount", Salary.class) + .setParameter("amount", money) + .getSingleResult(); + + assertEquals(1, salary.getId()); + }); + } + + @Test + public void testSearchByComponents() { + doInJPA(entityManager -> { + Salary salary1 = new Salary(); + salary1.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("10.23"), "USD")))); + entityManager.persist(salary1); + + Salary salary2 = new Salary(); + salary2.setEmbeddedSalary(new EmbeddableMonetaryAmount((Money.of(new BigDecimal("20.23"), "EUR")))); + entityManager.persist(salary2); + }); + + doInJPA(entityManager -> { + BigDecimal amount = BigDecimal.TEN; + List salaries = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.amount >= :amount", Salary.class) + .setParameter("amount", amount) + .getResultList(); + + + assertEquals(1L, salaries.get(0).getId()); + assertEquals(2L, salaries.get(1).getId()); + }); + + doInJPA(entityManager -> { + String currency = "USD"; + Salary salary = entityManager.createQuery("select s from Salary s where s.embeddedSalary.amount.currency = :currency", Salary.class) + .setParameter("currency", currency) + .getSingleResult(); + + assertEquals(1L, salary.getId()); + }); + } + + @Entity(name = "Salary") + @Table(name = "salary") + public static class Salary { + @Id + @GeneratedValue + private long id; + + private String other; + + @Embedded + private EmbeddableMonetaryAmount embeddedSalary; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public EmbeddableMonetaryAmount getEmbeddedSalary() { + return embeddedSalary; + } + + public void setEmbeddedSalary(EmbeddableMonetaryAmount embeddedSalary) { + this.embeddedSalary = embeddedSalary; + } + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + } + + @Embeddable + public static class EmbeddableMonetaryAmount { + @AttributeOverride(name = "amount", column = @Column(name = "salary_amount")) + @AttributeOverride(name = "currency", column = @Column(name = "salary_currency")) + @CompositeType(MonetaryAmountType.class) + private MonetaryAmount amount; + + public EmbeddableMonetaryAmount(MonetaryAmount amount) { + this.amount = amount; + } + + public EmbeddableMonetaryAmount() { + + } + + + public MonetaryAmount getAmount() { + return amount; + } + + public void setAmount(MonetaryAmount amount) { + this.amount = amount; + } + } +}