Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the JavaMoney (JSR 354) CurrencyUnit #473
- Loading branch information
1 parent
a571dbe
commit 00ad3ac
Showing
23 changed files
with
971 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/money/CurrencyUnitType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.vladmihalcea.hibernate.type.money; | ||
|
||
import com.vladmihalcea.hibernate.type.AbstractHibernateType; | ||
import com.vladmihalcea.hibernate.type.money.internal.CurrencyUnitTypeDescriptor; | ||
import com.vladmihalcea.hibernate.type.util.Configuration; | ||
import org.hibernate.type.descriptor.sql.VarcharTypeDescriptor; | ||
|
||
import javax.money.CurrencyUnit; | ||
|
||
/** | ||
* Maps a Java {@link CurrencyUnit} object to a {@code VARCHAR} column type. | ||
* | ||
* @author Piotr Olaszewski | ||
*/ | ||
public class CurrencyUnitType extends AbstractHibernateType<CurrencyUnit> { | ||
public static final CurrencyUnitType INSTANCE = new CurrencyUnitType(); | ||
|
||
public CurrencyUnitType() { | ||
super(VarcharTypeDescriptor.INSTANCE, CurrencyUnitTypeDescriptor.INSTANCE); | ||
} | ||
|
||
public CurrencyUnitType(Configuration configuration) { | ||
super( | ||
VarcharTypeDescriptor.INSTANCE, | ||
CurrencyUnitTypeDescriptor.INSTANCE, | ||
configuration | ||
); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "currency"; | ||
} | ||
|
||
@Override | ||
protected boolean registerUnderJavaType() { | ||
return true; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../main/java/com/vladmihalcea/hibernate/type/money/internal/CurrencyUnitTypeDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.vladmihalcea.hibernate.type.money.internal; | ||
|
||
import org.hibernate.type.descriptor.WrapperOptions; | ||
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor; | ||
|
||
import javax.money.CurrencyUnit; | ||
import javax.money.Monetary; | ||
|
||
/** | ||
* @author Piotr Olaszewski | ||
*/ | ||
public class CurrencyUnitTypeDescriptor extends AbstractTypeDescriptor<CurrencyUnit> { | ||
|
||
public static final CurrencyUnitTypeDescriptor INSTANCE = new CurrencyUnitTypeDescriptor(); | ||
|
||
public CurrencyUnitTypeDescriptor() { | ||
super(CurrencyUnit.class); | ||
} | ||
|
||
@Override | ||
public CurrencyUnit fromString(String string) { | ||
return Monetary.getCurrency(string); | ||
} | ||
|
||
@Override | ||
public <X> X unwrap(CurrencyUnit value, Class<X> type, WrapperOptions options) { | ||
if (value == null) { | ||
return null; | ||
} | ||
|
||
if (String.class.isAssignableFrom(type)) { | ||
return (X) value.getCurrencyCode(); | ||
} | ||
|
||
throw unknownUnwrap(type); | ||
} | ||
|
||
@Override | ||
public <X> CurrencyUnit wrap(X value, WrapperOptions options) { | ||
if (value == null) { | ||
return null; | ||
} | ||
|
||
if (value instanceof String) { | ||
return fromString((String) value); | ||
} | ||
|
||
throw unknownWrap(value.getClass()); | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
...pes-52/src/test/java/com/vladmihalcea/hibernate/type/money/MySQLCurrencyUnitTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.vladmihalcea.hibernate.type.money; | ||
|
||
import com.vladmihalcea.hibernate.util.AbstractMySQLIntegrationTest; | ||
import org.hibernate.annotations.TypeDef; | ||
import org.junit.Test; | ||
|
||
import javax.money.CurrencyUnit; | ||
import javax.persistence.*; | ||
|
||
import static javax.money.Monetary.getCurrency; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
/** | ||
* @author Piotr Olaszewski | ||
*/ | ||
public class MySQLCurrencyUnitTypeTest extends AbstractMySQLIntegrationTest { | ||
@Override | ||
protected Class<?>[] entities() { | ||
return new Class[]{ | ||
Order.class | ||
}; | ||
} | ||
|
||
@Test | ||
public void testPersistAndReadCurrency() { | ||
Order _order = doInJPA(entityManager -> { | ||
Order order = new Order(); | ||
order.setCurrency(getCurrency("EUR")); | ||
|
||
entityManager.persist(order); | ||
|
||
return order; | ||
}); | ||
|
||
doInJPA(entityManager -> { | ||
Order order = entityManager.find(Order.class, _order.getId()); | ||
|
||
assertEquals(order.getCurrency(), getCurrency("EUR")); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testSearchByCurrency() { | ||
doInJPA(entityManager -> { | ||
Order order1 = new Order(); | ||
order1.setCurrency(getCurrency("EUR")); | ||
entityManager.persist(order1); | ||
|
||
Order order2 = new Order(); | ||
order2.setCurrency(getCurrency("PLN")); | ||
entityManager.persist(order2); | ||
}); | ||
|
||
doInJPA(entityManager -> { | ||
CurrencyUnit currency = getCurrency("PLN"); | ||
Order order = entityManager.createQuery("select o from Order o where o.currency = :currency", Order.class) | ||
.setParameter("currency", currency) | ||
.getSingleResult(); | ||
|
||
assertEquals(Long.valueOf(2), order.getId()); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testReturnNullCurrency() { | ||
Long _id = doInJPA(entityManager -> { | ||
Order order = new Order(); | ||
entityManager.persist(order); | ||
|
||
return order.getId(); | ||
}); | ||
|
||
doInJPA(entityManager -> { | ||
Order order = entityManager.createQuery("select o from Order o where o.id = :id", Order.class) | ||
.setParameter("id", _id) | ||
.getSingleResult(); | ||
|
||
assertNull(order.getCurrency()); | ||
}); | ||
|
||
doInJPA(entityManager -> { | ||
CurrencyUnit currency = entityManager.createQuery("select o.currency from Order o where o.id = :id", CurrencyUnit.class) | ||
.setParameter("id", _id) | ||
.getSingleResult(); | ||
|
||
assertNull(currency); | ||
}); | ||
} | ||
|
||
@Entity(name = "Order") | ||
@Table(name = "orders") | ||
@TypeDef(typeClass = CurrencyUnitType.class, defaultForType = CurrencyUnit.class) | ||
public static class Order { | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@Column(name = "currency", columnDefinition = "char(3)") | ||
private CurrencyUnit currency; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public CurrencyUnit getCurrency() { | ||
return currency; | ||
} | ||
|
||
public void setCurrency(CurrencyUnit currency) { | ||
this.currency = currency; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.