diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/OfficeEmployee.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/OfficeEmployee.java index 1a29182764..b020aec380 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/OfficeEmployee.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/OfficeEmployee.java @@ -34,7 +34,7 @@ public class OfficeEmployee { @AttributeOverride(name = "cityCode", column = @Column(name = "city_code")), @AttributeOverride(name = "number", column = @Column(name = "number")) }) - @Type(value = PhoneNumberType.class) + @CompositeType(value = PhoneNumberType.class) private PhoneNumber employeeNumber; @CompositeType(value = com.baeldung.hibernate.customtypes.AddressType.class) @@ -48,11 +48,7 @@ public class OfficeEmployee { private Address empAddress; @Type(value = com.baeldung.hibernate.customtypes.SalaryType.class, - parameters = {@Parameter(name = "currency", value = "USD")}) - @AttributeOverrides({ - @AttributeOverride(name = "amount", column = @Column(name = "amount")), - @AttributeOverride(name = "currency", column = @Column(name = "currency")) - }) + parameters = {@Parameter(name = "currency", value = "USD")}) private Salary salary; public Salary getSalary() { diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/PhoneNumberType.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/PhoneNumberType.java index 0f8df5f315..d1e46ad7fc 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/PhoneNumberType.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/PhoneNumberType.java @@ -1,6 +1,11 @@ package com.baeldung.hibernate.customtypes; +import org.hibernate.HibernateException; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; +import org.hibernate.metamodel.spi.ValueAccess; +import org.hibernate.type.CompositeType; +import org.hibernate.usertype.CompositeUserType; import org.hibernate.usertype.UserType; import java.io.Serializable; @@ -10,7 +15,7 @@ import java.sql.SQLException; import java.sql.Types; import java.util.Objects; -public class PhoneNumberType implements UserType { +public class PhoneNumberType implements UserType, CompositeUserType { @Override public int getSqlType() { @@ -90,4 +95,31 @@ public class PhoneNumberType implements UserType { public PhoneNumber replace(PhoneNumber detached, PhoneNumber managed, Object owner) { return detached; } + + @Override + public Object getPropertyValue(PhoneNumber component, int property) throws HibernateException { + switch (property) { + case 0: + return component.getCountryCode(); + case 1: + return component.getCityCode(); + case 2: + return component.getNumber(); + default: + throw new IllegalArgumentException(property + + " is an invalid property index for class type " + + component.getClass().getName()); + } + } + + @Override + public PhoneNumber instantiate(ValueAccess values, SessionFactoryImplementor sessionFactory) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Class embeddable() { + return PhoneNumber.class; + } } diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/Salary.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/Salary.java index f9a7ac5902..2402531869 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/Salary.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/Salary.java @@ -1,8 +1,9 @@ package com.baeldung.hibernate.customtypes; +import java.io.Serializable; import java.util.Objects; -public class Salary { +public class Salary implements Serializable { private Long amount; private String currency; diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/SalaryType.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/SalaryType.java index 69e34c1363..3ae0040ef0 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/SalaryType.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/SalaryType.java @@ -16,38 +16,13 @@ import java.sql.Types; import java.util.Objects; import java.util.Properties; -public class SalaryType implements UserType, CompositeUserType, DynamicParameterizedType { +public class SalaryType implements UserType, DynamicParameterizedType { private String localCurrency; - @Override - public Object getPropertyValue(Salary component, int property) throws HibernateException { - - switch (property) { - case 0: - return component.getAmount(); - case 1: - return component.getCurrency(); - default: - throw new IllegalArgumentException(property + - " is an invalid property index for class type " + - component.getClass().getName()); - } - } - - @Override - public Salary instantiate(ValueAccess values, SessionFactoryImplementor sessionFactory) { - return null; - } - - @Override - public Class embeddable() { - return Salary.class; - } - @Override public int getSqlType() { - return Types.BIGINT; + return Types.VARCHAR; } @Override @@ -73,13 +48,17 @@ public class SalaryType implements UserType, CompositeUserType, @Override public Salary nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { - Salary salary = new Salary(); - salary.setAmount(rs.getLong(position)); - + if (rs.wasNull()) return null; + + Salary salary = new Salary(); + + String salaryValue = rs.getString(position); + + salary.setAmount(Long.parseLong(salaryValue.split(" ")[1])); - salary.setCurrency(rs.getString(position)); + salary.setCurrency(salaryValue.split(" ")[1]); return salary; } @@ -87,13 +66,12 @@ public class SalaryType implements UserType, CompositeUserType, @Override public void nullSafeSet(PreparedStatement st, Salary value, int index, SharedSessionContractImplementor session) throws SQLException { if (Objects.isNull(value)) - st.setNull(index, Types.BIGINT); + st.setNull(index, Types.VARCHAR); else { - - st.setLong(index, SalaryCurrencyConvertor.convert( - value.getAmount(), - value.getCurrency(), localCurrency)); - st.setString(index + 1, value.getCurrency()); + Long salaryValue = SalaryCurrencyConvertor.convert( + value.getAmount(), + value.getCurrency(), localCurrency); + st.setString(index, value.getCurrency()+ " " + salaryValue); } } diff --git a/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/customtypes/HibernateCustomTypesIntegrationTest.java b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/customtypes/HibernateCustomTypesIntegrationTest.java index 9da3a90034..808da6aea4 100644 --- a/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/customtypes/HibernateCustomTypesIntegrationTest.java +++ b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/customtypes/HibernateCustomTypesIntegrationTest.java @@ -47,7 +47,7 @@ public class HibernateCustomTypesIntegrationTest { boolean contains = session.contains(e); assertTrue(contains); }); - + } @Test